All pastes #747621 Raw Edit

Bug in finddw.c

public text v1 · immutable
·published 2007-10-24 06:05 UTC
rendered paste body
There is a bug in the finddw program:
---
em@sam:/tmp> cat hi
Hello Hello file file
Hello
em@sam:/tmp> ./finddw hi
In file hi, repeated word: Hello Hello
Context:  file file

Re-run program after fixing!!!
 In file hi, repeated word: Hello Hello
Context:

Re-run program after fixing!!!   
---
This bug has to do with line 59:
  59                                  fgets( Buffer, MAX_BUF, fp );

When a double word is found, the rest of the line is used as context. Using fgets for this advances our location in the file to the beginning of the next line. This means any other double words in that line are skipped. This isn't particularly serious because it is justifiable to say behaviour of finddw is undefined for a line where a mistake was already found. A bigger problem, however, is that the word upon which we had failed (the second "Hello" in our example) is still copied into Word by strncopy, and the next word is read into NextWord from the beginning of the next line, where fgets left off. This means that finddw begins comparing the second "Hello" on the first line to the first "Hello" on the second line despite words in-between, resulting in a false positive.

I realised this when re-reading the slides for the midterm and thought you might like to know.