Look for Known Gotchas

If you have split the code into sections with goals and identified the real meaning of each variable, and nothing has jumped out as being incorrect, you can proceed to choosing inputs and walking through the code. First, however, you can quickly scan the code for a few “gotchas” without getting into the nitty-gritty details.

Loop Counters

Loop counters are often used to index into arrays. In languages that have zero-based arrays, notice if the check to exit a loop uses <= in the comparison, as opposed to <. Code such as the following

for (index = 0; index <= MAX_COUNT; index++) {
    j = array[index];
}

might be correct, but the comparison index <= MAX_COUNT is suspicious. Normally, with a zero-based array, it should be ...

Get Find the Bug A Book of Incorrect Programs now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.