encyclopegia.org

Off-by-one error

An off-by-one error or off-by-one bug (known by acronyms OBOE, OBOB, OBO, and OB1) is a logic error that involves a number that differs from its intended value by 1.[src] An off-by-one error can sometimes appear in a mathematical context.[src] It also often occurs in computer programming when a loop iterates one time too many or too few, usually caused by the use of non-strict inequality (≤) as the terminating condition where strict inequality (<) should have been used, or vice versa.[src] Off-by-one errors also stem from confusion over zero-based numbering.[src]

An off-by-one error or off-by-one bug (known by acronyms OBOE, OBOB, OBO, and OB1) is a logic error that involves a number that differs from its intended value by 1.[src] An off-by-one error can sometimes appear in a mathematical context.[src] It also often occurs in computer programming when a loop iterates one time too many or too few, usually caused by the use of non-strict inequality (≤) as the terminating condition where strict inequality ( linspace() linear interpolation function, whose parameters are (lower value, upper value, number of values) and not (lower value, upper value, number of increments) .[src] A programmer who misunderstands the third parameter to be the number of increments might hope that linspace(0,10,5) would achieve a sequence but instead would get .[src] for (index = 0; index The loop body is executed first of all with equal to 0; then becomes 1, 2, 3, and finally 4 on successive iterations.[src] At that point, becomes 5, so is false and the loop ends.[src] However, if the comparison used were void foo (char *s) { char buf[15]; memset(buf, 0, sizeof(buf)); strncat(buf, s, sizeof(buf)); // Final parameter should be: sizeof(buf)-1 } Off-by-one errors can occur when using the C library because it is not consistent with respect to whether one needs to subtract 1 byte; functions like fgets() and strncpy will never write past the length given to them ( fgets() subtracts 1 itself, and only retrieves bytes), whereas others, like strncat , will write past the length given to them.[src] On some systems (little endian architectures in particular) this behavior can result in the overwriting of the least significant byte of the frame pointer.[src] This can cause an exploitable condition where an attacker can hijack the local variables for the calling routine.[src] One approach that often helps avoid such problems is to use variants of these functions that calculate how much to write based on the total length of the buffer, rather than the maximum number of characters to write.[src] Such functions include strlcat and strlcpy , and are often considered "safer" because they make it easier to avoid accidentally writing past the end of a buffer.[src] (In the code example above, calling strlcat(buf, s, sizeof(buf)) instead would remove the bug.)[src]

16 sentences · 16 cited inline · 1 references stored

References

  1. De Architectura . · retrieved 2026-09-28 · rev 1377474245
    Cite this reference

    MLA "De Architectura ." 2026-09-28. https://en.wikipedia.org/w/index.php?oldid=1377474245. Accessed 25 November 2026.

    Chicago "De Architectura ." 2026-09-28. https://en.wikipedia.org/w/index.php?oldid=1377474245.

    APA encyclopegia.org. (2026). De Architectura . Retrieved November 25, 2026, from https://en.wikipedia.org/w/index.php?oldid=1377474245

Cite this article

MLA

"Off-by-one error." encyclopegia.org, 2026, https://encyclopegia.org/wiki/off-by-one-error. Accessed 25 November 2026.

Chicago

"Off-by-one error." encyclopegia.org. Accessed November 25, 2026. https://encyclopegia.org/wiki/off-by-one-error.

APA

encyclopegia.org. (2026). Off-by-one error. Retrieved November 25, 2026, from https://encyclopegia.org/wiki/off-by-one-error

Accessed November 25, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

BAC One-Eleven

stub · references available

Half-Life 2: Episode One

stub · references available

Star One

stub · references available

One Touch

stub · references available

One Day in Your Life

stub · references available

Love One.

stub · references available

1952 Formula One season

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1377474245 · 0.5 · snapshot enwiki-001377474245

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.