CATEGORY: MITRE CWE-193: Off-by-one error. REFERENCE: http://cwe.mitre.org/data/definitions/193.html. EXPLANATION: When a color different to the previous color terminates the current series, the variable currentSeriesLength is incorrectly reset to 0. It should be set to 1 as the new color already constitues a series of length 1. CONSEQUENCES: All series following the first series will have a length that is short by one. BUGFIX: The most straightforward fix is to set currentSeriesLength to 1 when a spin yields a new color: // New color, series terminated. } else { // Keep track of how often a particular series length (of previous // color) occurred. ++seriesMaps[prevColor][currentSeriesLength]; // Current series is over. currentSeriesLength = 1 // New color has already appeared once. } REMARKS: Off-by-one errors are among the most frequently encountered errors in software programs; they usually appear at the boundaries of ranges. Even though this bug looks obvious in hindsight, it is somewhat shadowed by the fact that currentSeriesLength is initially -- and correctly -- set to 0 upon the first loop iteration: // Initialize upon first spin. if (!initialized) { prevColor = newColor currentSeriesLength = 0 initialized = true }