Off-by-one errors: counting the posts, not the gaps
The most ordinary bug in programming is also one of the most persistent. It hides in loop conditions, slices and buffer sizes, and it usually shows up right at the edge.
Wrongfor (i = 0; i <= arr.length; i++)
Rightfor (i = 0; i < arr.length; i++)
What people get wrong#
You're building a 100-foot fence with a post every 10 feet. How many posts? The quick answer is 10. The right answer is 11, because ten gaps need a post at each end. That's the fencepost problem, and when it shows up in code it's called an off-by-one error.
MITRE's Common Weakness Enumeration, the industry catalog of bug classes, lists it as CWE-193: a program "calculates or uses an incorrect maximum or minimum value that is 1 more, or 1 less, than the correct value." One of its examples is the classic:
for (i = 0; i <= PATH_SIZE; i++) /* one past the end */
The loop runs one extra time and touches an element that doesn't exist. In JavaScript you get undefined. In Python you get an IndexError. In C you may silently overwrite whatever sits next in memory, which is why the CWE entry lists real CVEs caused by exactly this mistake.
Why it happens#
Because there are two numbers that sound alike: how many things there are, and the position of the last one. MDN describes an array's length as "always numerically greater than the highest index". With indexes starting at zero, a five-element array ends at index 4. Mix up "count" and "last index" and you're off by one.
The other cause is inconsistent ranges. Does "from 2 to 12" include the 12? People say it both ways. In 1982 Edsger Dijkstra wrote a one-page note, "Why numbering should start at zero", comparing the four ways to write the range 2 through 12. He preferred 2 ≤ i < 13: include the lower bound, exclude the upper. With that convention, the difference between the bounds equals the length, and adjacent ranges share a boundary without overlapping. He cited experience with the Mesa language at Xerox PARC, where the other three notations had been "a constant source of clumsiness and mistakes."
The fix#
for (let i = 0; i <= items.length; i++)
for (let i = 0; i < items.length; i++)
Indexes run from 0 to length minus 1, so the loop stops before length.
Python is built on Dijkstra's half-open convention. range(10) yields 0 through 9, and the official tutorial says of slices: "the start is always included, and the end always excluded." As a result, s[:i] + s[i:] is always the whole string, and len(s[a:b]) is just b - a.
How to avoid it#
- Use half-open ranges everywhere. Write
start ≤ i < end, storeendrather than "last", and length becomesend - startwith no+ 1. - Don't index if you don't have to.
for (const x of items),for x in itemsanditems.map()have no bounds for you to get wrong. - Test at the edges. Off-by-one bugs live at boundaries, so test an empty input, a single element, exactly the limit, and one past it.
- Be suspicious of a stray
+ 1or- 1. Each one should have a reason you could explain in a comment. If you can't, it's probably compensating for a mistake somewhere else. - Size buffers for the terminator. In C, a string of n characters needs n + 1 bytes. CWE-193's
strncatexample fails because it forgot exactly that.
There's an old joke that the two hardest problems in computing are naming things, cache invalidation and off-by-one errors. It survives because nearly every programmer has made the third one.
Sources
Every factual claim on this page links to one of these. Checked November 9, 2026. See how we source.
- MITRE CWE: CWE-193 Off-by-one Error cwe.mitre.org
- MDN Web Docs: Array.prototype.length developer.mozilla.org
- Python documentation: An Informal Introduction to Python (slicing) docs.python.org
- E.W. Dijkstra Archive: Why numbering should start at zero (EWD 831) cs.utexas.edu