{
  "id": "enwiki-001377474245",
  "slug": "off-by-one-error",
  "title": "Off-by-one error",
  "qid": "",
  "lead": "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. An off-by-one error can sometimes appear in a mathematical context. 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. Off-by-one errors also stem from confusion over zero-based numbering.",
  "body": "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. An off-by-one error can sometimes appear in a mathematical context. 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) . 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 . 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. At that point, becomes 5, so is false and the loop ends. 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. On some systems (little endian architectures in particular) this behavior can result in the overwriting of the least significant byte of the frame pointer. This can cause an exploitable condition where an attacker can hijack the local variables for the calling routine. 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. 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. (In the code example above, calling strlcat(buf, s, sizeof(buf)) instead would remove the bug.)",
  "categories": [
    "22943154",
    "22944463",
    "23118434",
    "23603337",
    "23837581",
    "24043274",
    "26748027",
    "33048645",
    "37160764",
    "98195298"
  ],
  "license": "CC BY-SA 4.0",
  "source_uri": "https://en.wikipedia.org/w/index.php?oldid=1377474245",
  "content_hash": "",
  "created_at": null,
  "updated_at": 1790689506.0,
  "claims": [],
  "contradictions": [],
  "confidence": 0.6,
  "csl": {
    "id": "enwiki-001377474245",
    "type": "webpage",
    "title": "Off-by-one error",
    "author": [
      {
        "family": "Anticloud FZ LLE",
        "literal": "Anticloud FZ LLE"
      }
    ],
    "container-title": "encyclopegia.org",
    "publisher": "Anticloud FZ LLE",
    "publisher-place": "Dubai, United Arab Emirates",
    "URL": "https://encyclopegia.org/a/off-by-one-error",
    "issued": {
      "date-parts": [
        [
          2026,
          11,
          20
        ]
      ]
    },
    "accessed": {
      "date-parts": [
        [
          2026,
          11,
          20
        ]
      ]
    },
    "license": "CC BY-SA 4.0",
    "DOI": "eo1:enwiki-001377474245",
    "isBasedOn": "https://en.wikipedia.org/w/index.php?oldid=1377474245",
    "note": "Immutable snapshot enwiki-001377474245"
  }
}