Impact | Details |
---|---|
DoS: Crash, Exit, or Restart; DoS: Resource Consumption (CPU); DoS: Resource Consumption (Memory); DoS: Instability |
Scope: Availability
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 |
Scope: Integrity
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 |
Scope: Confidentiality, Availability, Access Control
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.
|
Phase(s) | Mitigation |
---|---|
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().
|
Nature | Type | ID | Name |
---|---|---|---|
ChildOf | Pillar Pillar - a weakness that is the most abstract type of weakness and represents a theme for all class/base/variant weaknesses related to it. A Pillar is different from a Category as a Pillar is still technically a type of weakness that describes a mistake, while a Category represents a common characteristic used to group related things. | 682 | Incorrect Calculation |
CanPrecede | Class Class - a weakness that is described in a very abstract fashion, typically independent of any specific language or technology. More specific than a Pillar Weakness, but more general than a Base Weakness. Class level weaknesses typically describe issues in terms of 1 or 2 of the following dimensions: behavior, property, and resource. | 119 | Improper Restriction of Operations within the Bounds of a Memory Buffer |
CanPrecede | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 170 | Improper Null Termination |
CanPrecede | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 617 | Reachable Assertion |
Nature | Type | ID | Name |
---|---|---|---|
MemberOf | Category Category - a CWE entry that contains a set of other entries that share a common characteristic. | 189 | Numeric Errors |
Nature | Type | ID | Name |
---|---|---|---|
ChildOf | Pillar Pillar - a weakness that is the most abstract type of weakness and represents a theme for all class/base/variant weaknesses related to it. A Pillar is different from a Category as a Pillar is still technically a type of weakness that describes a mistake, while a Category represents a common characteristic used to group related things. | 682 | Incorrect Calculation |
Phase | Note |
---|---|
Implementation |
C (Undetermined Prevalence)
Class: Not Language-Specific (Undetermined Prevalence)
Example 1
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.
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.
Example 2
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.
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.
Example 3
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.
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.
Example 4
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.
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.
Note: this is a curated list of examples for users to understand the variety of ways in which this weakness can be introduced. It is not a complete list of all CVEs that are related to this CWE entry.
Reference | Description |
---|---|
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.
|
|
Off-by-one vulnerability in driver allows users to modify kernel memory.
|
|
Off-by-one error allows local users or remote malicious servers to gain privileges.
|
|
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.
|
|
Off-by-one buffer overflow in version control system allows local users to execute arbitrary code.
|
|
Off-by-one error in FTP server allows a remote attacker to cause a denial of service (crash) via a long PORT command.
|
|
Off-by-one buffer overflow in FTP server allows local users to gain privileges via a 1024 byte RETR command.
|
|
Multiple buffer overflows in chat client allow remote attackers to cause a denial of service and possibly execute arbitrary code.
|
|
Multiple off-by-one vulnerabilities in product allow remote attackers to cause a denial of service and possibly execute arbitrary code.
|
|
Off-by-one buffer overflow in server allows remote attackers to cause a denial of service and possibly execute arbitrary code.
|
|
This is an interesting example that might not be an off-by-one.
|
|
An off-by-one enables a terminating null to be overwritten, which causes 2 strings to be merged and enable a format string.
|
|
Off-by-one error allows source code disclosure of files with 4 letter extensions that match an accepted 3-letter extension.
|
|
Off-by-one buffer overflow.
|
|
Off-by-one error causes an snprintf call to overwrite a critical internal variable with a null value.
|
|
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.
|
|
Off-by-one error allows read of sensitive memory via a malformed request.
|
|
Chain: security monitoring product has an off-by-one error that leads to unexpected length values, triggering an assertion.
|
Method | Details |
---|---|
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.)
Effectiveness: High |
Nature | Type | ID | Name |
---|---|---|---|
MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 741 | CERT C Secure Coding Standard (2008) Chapter 8 - Characters and Strings (STR) |
MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 875 | CERT C++ Secure Coding Section 07 - Characters and Strings (STR) |
MemberOf | ViewView - a subset of CWE entries that provides a way of examining CWE content. The two main view structures are Slices (flat lists) and Graphs (containing relationships between entries). | 884 | CWE Cross-section |
MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 977 | SFP Secondary Cluster: Design |
MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 1408 | Comprehensive Categorization: Incorrect Calculation |
Rationale
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.Comments
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.Relationship
Mapped Taxonomy Name | Node ID | Fit | Mapped Node Name |
---|---|---|---|
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 |
Submissions | ||
---|---|---|
Submission Date | Submitter | Organization |
2006年07月19日
(CWE Draft 3, 2006年07月19日) |
PLOVER | |
Contributions | ||
Contribution Date | Contributor | Organization |
2024年07月20日
(CWE 4.17, 2025年04月03日) |
Jason Xu | |
Reported compilation error with demonstrative example. | ||
Modifications | ||
Modification Date | Modifier | Organization |
2025年09月09日
(CWE 4.18, 2025年09月09日) |
CWE Content Team | MITRE |
updated References | ||
2025年04月03日
(CWE 4.17, 2025年04月03日) |
CWE Content Team | MITRE |
updated Applicable_Platforms, Demonstrative_Examples | ||
2023年06月29日 | CWE Content Team | MITRE |
updated Mapping_Notes | ||
2023年04月27日 | CWE Content Team | MITRE |
updated Detection_Factors, References, Relationships | ||
2022年04月28日 | CWE Content Team | MITRE |
updated Research_Gaps | ||
2021年03月15日 | CWE Content Team | MITRE |
updated Demonstrative_Examples | ||
2020年02月24日 | CWE Content Team | MITRE |
updated Relationships | ||
2019年06月20日 | CWE Content Team | MITRE |
updated Demonstrative_Examples, Relationships | ||
2018年03月27日 | CWE Content Team | MITRE |
updated Demonstrative_Examples | ||
2017年11月08日 | CWE Content Team | MITRE |
updated Applicable_Platforms, References, Taxonomy_Mappings | ||
2017年05月03日 | CWE Content Team | MITRE |
updated Demonstrative_Examples | ||
2014年07月30日 | CWE Content Team | MITRE |
updated Demonstrative_Examples, Relationships | ||
2014年06月23日 | CWE Content Team | MITRE |
updated References | ||
2012年05月11日 | CWE Content Team | MITRE |
updated Common_Consequences, Observed_Examples, References, Relationships | ||
2011年09月13日 | CWE Content Team | MITRE |
updated Relationships, Taxonomy_Mappings | ||
2011年06月27日 | CWE Content Team | MITRE |
updated Common_Consequences | ||
2011年06月01日 | CWE Content Team | MITRE |
updated Common_Consequences | ||
2010年12月13日 | CWE Content Team | MITRE |
updated Demonstrative_Examples | ||
2010年02月16日 | CWE Content Team | MITRE |
updated Demonstrative_Examples | ||
2009年12月28日 | CWE Content Team | MITRE |
updated Demonstrative_Examples, Potential_Mitigations | ||
2008年11月24日 | CWE Content Team | MITRE |
updated Relationships, Taxonomy_Mappings | ||
2008年09月08日 | CWE Content Team | MITRE |
updated Alternate_Terms, Common_Consequences, Relationships, Observed_Example, Relationship_Notes, Taxonomy_Mappings |
Use of the Common Weakness Enumeration (CWE™) and the associated references from this website are subject to the Terms of Use. CWE is sponsored by the U.S. Department of Homeland Security (DHS) Cybersecurity and Infrastructure Security Agency (CISA) and managed by the Homeland Security Systems Engineering and Development Institute (HSSEDI) which is operated by The MITRE Corporation (MITRE). Copyright © 2006–2025, The MITRE Corporation. CWE, CWSS, CWRAF, and the CWE logo are trademarks of The MITRE Corporation.