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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.