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

Primary

off-by-five
An "off-by-five" error was reported for sudo in 2002 (CVE-2002-0184), but that is more like a "length calculation" error.

Implementation

Availability
DoS: Crash, Exit, or 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.

Integrity
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.

Confidentiality
Availability
Access Control
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.

Automated Static Analysis
Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
High

Implementation
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().

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.

int i;unsigned int numWidgets;Widget **WidgetList;
numWidgets = GetUntrustedSizeValue();if ((numWidgets == 0) || (numWidgets > MAX_NUM_WIDGETS)) {ExitError("Incorrect number of widgets requested!");}WidgetList = (Widget **)malloc(numWidgets * sizeof(Widget *));printf("WidgetList ptr=%p\n", WidgetList);for(i=0; i<numWidgets; i++) {WidgetList[i] = InitializeWidget();}WidgetList[numWidgets] = NULL;showWidgets(WidgetList);

However, this code contains an off-by-one calculation error (CWE-193). It allocates exactly enough space to contain the specified number of widgets, but it does not include the space for the NULL pointer. As a result, the allocated buffer is smaller than it is supposed to be (CWE-131). So if the user ever requests MAX_NUM_WIDGETS, there is an out-of-bounds write (CWE-787) when the NULL is assigned. Depending on the environment and compilation settings, this could cause memory corruption.

In this example, the code does not account for the terminating null character, and it writes one byte beyond the end of the buffer.
The first call to strncat() appends up to 20 characters plus a terminating null character to fullname[]. There is plenty of allocated space for this, and there is no weakness associated with this first call. However, the second call to strncat() potentially appends another 20 characters. The code does not account for the terminating null character that is automatically added by strncat(). This terminating null character would be written one byte beyond the end of the fullname[] buffer. Therefore an off-by-one error exists with the second strncat() call, as the third argument should be 19.

char firstname[20];char lastname[20];char fullname[40];fullname[0] = '\0';strncat(fullname, firstname, 20);strncat(fullname, lastname, 20);

When using a function like strncat() one must leave a free byte at the end of the buffer for a terminating null character, thus avoiding the off-by-one weakness. Additionally, the last argument to strncat() is the number of characters to append, which must be less than the remaining space in the buffer. Be careful not to just use the total size of the buffer.

char firstname[20];char lastname[20];char fullname[40];fullname[0] = '\0';strncat(fullname, firstname, sizeof(fullname)-strlen(fullname)-1);strncat(fullname, lastname, sizeof(fullname)-strlen(fullname)-1);

The Off-by-one error can also be manifested when reading characters from a character array within a for loop that has an incorrect continuation condition.

#define PATH_SIZE 60
char filename[PATH_SIZE];
for(i=0; i<=PATH_SIZE; i++) {

char c = fgetc(stdin);
if (c == EOF) {

filename[i] = '\0';

}
else {

filename[i] = c;

}

}

If i reaches PATH_SIZE, then the loop continues. However, filename[PATH_SIZE] is actually out of bounds, since the valid index range is from 0 to PATH_SIZE-1.
In this case, the correct continuation condition is shown below.

for(i=0; i<PATH_SIZE; i++) {...

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.

int setFilename(char *filename) {char name[20];sprintf(name, "%16s.dat", filename);int success = saveFormattedFilenameToDB(name);return success;}

However this will cause an Off-by-one error if the original filename is exactly 16 characters or larger because the format of 16 characters with the file extension is exactly 20 characters and does not take into account the required null terminator that will be placed at the end of the string.

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.
https://www.cve.org/CVERecord?id=CVE-2003-0252

CVE-2001-1391
Off-by-one vulnerability in driver allows users to modify kernel memory.
https://www.cve.org/CVERecord?id=CVE-2001-1391

CVE-2002-0083
Off-by-one error allows local users or remote malicious servers to gain privileges.
https://www.cve.org/CVERecord?id=CVE-2002-0083

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.
https://www.cve.org/CVERecord?id=CVE-2002-0653

CVE-2002-0844
Off-by-one buffer overflow in version control system allows local users to execute arbitrary code.
https://www.cve.org/CVERecord?id=CVE-2002-0844

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.
https://www.cve.org/CVERecord?id=CVE-1999-1568

CVE-2004-0346
Off-by-one buffer overflow in FTP server allows local users to gain privileges via a 1024 byte RETR command.
https://www.cve.org/CVERecord?id=CVE-2004-0346

CVE-2004-0005
Multiple buffer overflows in chat client allow remote attackers to cause a denial of service and possibly execute arbitrary code.
https://www.cve.org/CVERecord?id=CVE-2004-0005

CVE-2003-0356
Multiple off-by-one vulnerabilities in product allow remote attackers to cause a denial of service and possibly execute arbitrary code.
https://www.cve.org/CVERecord?id=CVE-2003-0356

CVE-2001-1496
Off-by-one buffer overflow in server allows remote attackers to cause a denial of service and possibly execute arbitrary code.
https://www.cve.org/CVERecord?id=CVE-2001-1496

CVE-2004-0342
This is an interesting example that might not be an off-by-one.
https://www.cve.org/CVERecord?id=CVE-2004-0342

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.
https://www.cve.org/CVERecord?id=CVE-2001-0609

CVE-2002-1745
Off-by-one error allows source code disclosure of files with 4 letter extensions that match an accepted 3-letter extension.
https://www.cve.org/CVERecord?id=CVE-2002-1745

CVE-2002-1816
Off-by-one buffer overflow.
https://www.cve.org/CVERecord?id=CVE-2002-1816

CVE-2002-1721
Off-by-one error causes an snprintf call to overwrite a critical internal variable with a null value.
https://www.cve.org/CVERecord?id=CVE-2002-1721

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.
https://www.cve.org/CVERecord?id=CVE-2003-0466

CVE-2003-0625
Off-by-one error allows read of sensitive memory via a malformed request.
https://www.cve.org/CVERecord?id=CVE-2003-0625

CVE-2006-4574
Chain: security monitoring product has an off-by-one error that leads to unexpected length values, triggering an assertion.
https://www.cve.org/CVERecord?id=CVE-2006-4574

Off-by-one Error

STR31-C
Guarantee that storage for strings has sufficient space for character data and the null terminator

Allowed
This CWE entry is at the Base level of abstraction, which is a preferred level of abstraction for mapping to the root causes of vulnerabilities.
Carefully read both the name and description to ensure that this mapping is an appropriate fit. Do not try to 'force' a mapping to a lower-level Base/Variant simply to comply with this preferred level of abstraction.

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.

PLOVER
2006-07-19
Draft 3
2006-07-19

CWE Content Team
MITRE
2008-09-08
updated Alternate_Terms, Common_Consequences, Relationships, Observed_Example, Relationship_Notes, Taxonomy_Mappings

CWE Content Team
MITRE
2008-11-24
updated Relationships, Taxonomy_Mappings

CWE Content Team
MITRE
2009-12-28
updated Demonstrative_Examples, Potential_Mitigations

CWE Content Team
MITRE
2010-02-16
updated Demonstrative_Examples

CWE Content Team
MITRE
2010-12-13
updated Demonstrative_Examples

CWE Content Team
MITRE
2011-06-01
updated Common_Consequences

CWE Content Team
MITRE
2011-06-27
updated Common_Consequences

CWE Content Team
MITRE
2011-09-13
updated Relationships, Taxonomy_Mappings

CWE Content Team
MITRE
2012-05-11
updated Common_Consequences, Observed_Examples, References, Relationships

CWE Content Team
MITRE
2014-06-23
updated References

CWE Content Team
MITRE
2014-07-30
updated Demonstrative_Examples, Relationships

CWE Content Team
MITRE
2017-05-03
updated Demonstrative_Examples

CWE Content Team
MITRE
2017-11-08
updated Applicable_Platforms, References, Taxonomy_Mappings

CWE Content Team
MITRE
2018-03-27
updated Demonstrative_Examples

CWE Content Team
MITRE
2019-06-20
updated Demonstrative_Examples, Relationships

CWE Content Team
MITRE
2020-02-24
updated Relationships

CWE Content Team
MITRE
2021-03-15
updated Demonstrative_Examples

CWE Content Team
MITRE
2022-04-28
updated Research_Gaps

CWE Content Team
MITRE
2023-04-27
updated Detection_Factors, References, Relationships

CWE Content Team
MITRE
2023-06-29
updated Mapping_Notes

CWE Content Team
MITRE
2025-04-03
4.17
2025-04-03
updated Applicable_Platforms, Demonstrative_Examples

CWE Content Team
MITRE
2025-09-09
4.18
2025-09-09
updated References

CWE Content Team
MITRE
2025-12-11
4.19
2025-12-11
updated Weakness_Ordinalities

Jason Xu
2024-07-20
4.17
2025-04-03
Reported compilation error with demonstrative example.