Off-by-One Error
Also known as: OBOE, Fencepost error
Common programming mistake in which a loop or calculation goes one step too far or stops one step short, often at array boundaries.
An off-by-one error is a logic mistake in which a count, index or boundary is wrong by exactly one. A loop meant to visit ten items might run nine or eleven times, or code might read one element past the end of an array. Such errors are common partly because programming languages differ in their conventions: many, including C, Python and JavaScript, number array positions from zero, so a ten-item array has positions 0 through 9, while others start from one. Confusing less-than with less-than-or-equal in a loop condition is a classic cause.
A closely related mistake is the fencepost error, named after a simple puzzle: a straight fence 100 meters long with posts every 10 meters needs 11 posts, not 10, because there is one more post than there are gaps. The same pattern appears when counting the days between dates, the pages in a range or the elements between two indexes. Conventions help. Half-open ranges, which include the start but exclude the end, as in Python's range function, make lengths easy to compute, and Edsger Dijkstra argued in a 1982 note that numbering should start at zero partly for this reason.
Off-by-one errors range from harmless to severe. Writing one byte past the end of a buffer in a language without bounds checking, such as C, can corrupt memory and create exploitable security vulnerabilities. Defenses include iterating with constructs that avoid manual indexes, such as for-each loops, testing boundary cases like empty collections and single elements in unit tests and using memory-safe languages that check array bounds. The error is common enough to feature in a well-known programming joke listing the two hard problems in computer science as cache invalidation, naming things and off-by-one errors.
Key points
- An off-by-one error makes a loop, index or count wrong by exactly one.
- Zero-based indexing and inclusive versus exclusive boundaries are frequent sources of confusion.
- The fencepost error, counting gaps instead of posts, is a classic variant of the mistake.
- In languages without bounds checking, off-by-one errors can corrupt memory and create security vulnerabilities.
Related entries
Debugging
Process of finding, diagnosing and fixing defects in software or hardware that cause it to behave incorrectly or unexpectedly.
Unit Testing
Practice of verifying that small, individual pieces of code, such as single functions or classes, behave correctly when tested in isolation.
Infinite Recursion
Programming error in which a recursive function never reaches a base case and keeps calling itself until the program crashes or exhausts memory.
Edsger Dijkstra
Dutch computer scientist known for his shortest path algorithm, structured programming and foundational work on concurrency and program correctness.
C
General-purpose programming language created by Dennis Ritchie at Bell Labs in the early 1970s, foundational for operating systems and embedded software.
Vulnerability
A weakness in software, hardware, configuration or process that an attacker could exploit to compromise confidentiality, integrity or availability.
Rubber Duck Debugging
Debugging method in which a programmer explains code line by line to an inanimate object, often a rubber duck, to expose the source of a bug.