[Forgot Password]
Login  Register Subscribe












Paid content will be excluded from the download.

Download | Alert*
view XML

Off-by-one Error

ID: 193Date: (C)2012-05-14   (M)2020-02-26
Type: weaknessStatus: DRAFT
Abstraction Type: Base


A product calculates or uses an incorrect maximum or minimum value that is 1 more, or 1 less, than the correct value.

Applicable Platforms
Language Class: All

Time Of Introduction

  • Implementation

Common Consequences

ScopeTechnical ImpactNotes
DoS: crash / exit / restart
DoS: resource consumption (CPU)
DoS: resource consumption (memory)
DoS: instability
This weakness will generally lead to undefined behavior and therefore crashes. In the case of overflows involving loop index variables, the likelihood of infinite loops is also high.
Modify memory
If the value in question is important to data (as opposed to flow), simple data corruption has occurred. Also, if the wrap around results in other conditions such as buffer overflows, further memory corruption may occur.
Execute unauthorized code or commands
Bypass protection mechanism
This weakness can sometimes trigger buffer overflows which can be used to execute arbitrary code. This is usually outside the scope of a program's implicit security policy.

Detection Methods

Potential Mitigations

 When copying character arrays or using character manipulation methods, the correct size parameter must be used to account for the null terminator that needs to be added at the end of the array. Some examples of functions susceptible to this weakness in C include strcpy(), strncpy(), strcat(), strncat(), printf(), sprintf(), scanf() and sscanf().

This is not always a buffer overflow. For example, an off-by-one error could be a factor in a partial comparison, a read from the wrong memory location, an incorrect conditional, etc.

Related CWETypeViewChain
CWE-193 ChildOf CWE-907 Category CWE-888  

Demonstrative Examples   (Details)

  1. As another example the Off-by-one error can occur when using the sprintf library function to copy a string variable to a formatted string variable and the original string variable comes from an untrusted source. As in the following example where a local function, setFilename is used to store the value of a filename to a database but first uses sprintf to format the filename. The setFilename function includes an input parameter with the name of the file that is used as the copy source in the sprintf function. The sprintf function will copy the file name to a char array of size 20 and specifies the format of the new variable as 16 characters followed by the file extension .dat.
  2. Similarly, this example uses the strncat and snprintf functions incorrectly. The code does not account for the null character that is added by the second strncat function call, one byte beyond the end of the name buffer.
  3. The Off-by-one error can also be manifested when reading characters of a character array using a for loop that has the incorrect size as a continuation condition and attempts to read beyond the end of the buffer for the character array as shown in the following example.
  4. The following C/C++ example demonstrates the Off-by-one error in the main method of a pattern matching utility that looks for a specific pattern within a specific file. The main method uses the string copy method, strncpy, to copy the command line user input file name and pattern to the Filename and Pattern character arrays respectively.
  5. The following code allocates memory for a maximum number of widgets. It then gets a user-specified number of widgets, making sure that the user does not request too many. It then initializes the elements of the array using InitializeWidget(). Because the number of widgets can vary for each request, the code inserts a NULL pointer to signify the location of the last widget. (Demonstrative Example Id DX-20)

Observed Examples

  1. CVE-2003-0252 : Off-by-one error allows remote attackers to cause a denial of service and possibly execute arbitrary code via requests that do not contain newlines.
  2. CVE-2001-1391 : Off-by-one vulnerability in driver allows users to modify kernel memory.
  3. CVE-2002-0083 : Off-by-one error allows local users or remote malicious servers to gain privileges.
  4. CVE-2002-0653 : Off-by-one buffer overflow in function usd by server allows local users to execute arbitrary code as the server user via .htaccess files with long entries.
  5. CVE-2002-0844 : Off-by-one buffer overflow in version control system allows local users to execute arbitrary code.
  6. CVE-1999-1568 : Off-by-one error in FTP server allows a remote attacker to cause a denial of service (crash) via a long PORT command.
  7. CVE-2004-0346 : Off-by-one buffer overflow in FTP server allows local users to gain privileges via a 1024 byte RETR command.
  8. CVE-2004-0005 : Multiple buffer overflows in chat client allow remote attackers to cause a denial of service and possibly execute arbitrary code.
  9. CVE-2003-0356 : Multiple off-by-one vulnerabilities in product allow remote attackers to cause a denial of service and possibly execute arbitrary code.
  10. CVE-2001-1496 : Off-by-one buffer overflow in server allows remote attackers to cause a denial of service and possibly execute arbitrary code.
  11. CVE-2004-0342 : This is an interesting example that might not be an off-by-one.
  12. CVE-2001-0609 : An off-by-one enables a terminating null to be overwritten, which causes 2 strings to be merged and enable a format string.
  13. CVE-2002-1745 : Off-by-one error allows source code disclosure of files with 4 letter extensions that match an accepted 3-letter extension.
  14. CVE-2002-1816 : Off-by-one buffer overflow.
  15. CVE-2002-1721 : Off-by-one error causes an snprintf call to overwrite a critical internal variable with a null value.
  16. CVE-2003-0466 : Off-by-one error in function used in many products leads to a buffer overflow during pathname management, as demonstrated using multiple commands in an FTP server.
  17. CVE-2003-0625 : Off-by-one error allows read of sensitive memory via a malformed request.
  18. CVE-2006-4574 : Chain: security monitoring product has an off-by-one error that leads to unexpected length values, triggering an assertion.

For more examples, refer to CVE relations in the bottom box.

White Box Definitions

Black Box Definitions

Taxynomy Mappings

PLOVER  Off-by-one Error
CERT C Secure Coding STR31-C
Guarantee that storage for strings has sufficient space for character data and the null terminator
CERT C++ Secure Coding STR31-CPP
Guarantee that storage for character arrays has sufficient space for character data and the null terminator


  1. Halvar Flake .Third Generation Exploits. presentation at Black Hat Europe 2001.
  2. Steve Christey .Off-by-one errors: a brief explanation. Secprog and SC-L mailing list posts. 2004-05-05.
  3. klog .The Frame Pointer Overwrite. Phrack Issue 55, Chapter 8. 1999-09-09.
  4. G. Hoglund G. McGraw .Exploiting Software: How to Break Code (The buffer overflow chapter). Addison-Wesley. Published on February 2004.
  5. Michael Howard David LeBlanc John Viega .24 Deadly Sins of Software Security. McGraw-Hill. Section:'"Sin 5: Buffer Overruns." Page 89'. Published on 2010.
  6. Mark Dowd John McDonald Justin Schuh .The Art of Software Security Assessment 1st Edition. Addison Wesley. Section:'Chapter 5, "Off-by-One Errors", Page 180.'. Published on 2006.
CVE    24

© SecPod Technologies