CWE-193: Off-by-one Error
Also known as: off-by-five
A product calculates or uses an incorrect maximum or minimum value that is 1 more, or 1 less, than the correct value.
Last updated
Overview
CWE-193 (Off-by-one Error) is a base-level software weakness catalogued by MITRE in the Common Weakness Enumeration (CWE). It describes a recurring type of mistake that can lead to exploitable security vulnerabilities.
Real-world CVEs
94 recorded CVEs are caused by CWE-193 (Off-by-one Error), including 1 in CISA's KEV (Known Exploited Vulnerabilities) catalog. KEVs are shown first. 37 new CWE-193 CVEs have been recorded so far in 2026 (15 in 2025).
- CVE-2021-3156CISA KEVCritical · CVSS 9.3 · EPSS 100th2021-01-26
- CVE-2025-54349Critical · CVSS 10.0 · EPSS 30th2025-08-03
- CVE-2024-10442Critical · CVSS 10.0 · EPSS 68th2025-03-19
- CVE-2026-48689Critical · CVSS 9.8 · EPSS 48th2026-05-26
- CVE-2006-10003
XML::Parser versions through 2.47 for Perl has an off-by-one heap buffer overflow in st_serial_stack
Critical · CVSS 9.8 · EPSS 42th2026-03-19 - CVE-2025-53014
ImageMagick has Heap Buffer Overflow in InterpretImageFilename
Critical · CVSS 9.8 · EPSS 46th2025-07-14 - CVE-2025-43973Critical · CVSS 9.8 · EPSS 40th2025-04-21
- CVE-2024-38441Critical · CVSS 9.8 · EPSS 56th2024-06-16
- CVE-2021-21938Critical · CVSS 9.8 · EPSS 76th2022-04-14
- CVE-2020-14510Critical · CVSS 9.8 · EPSS 83th2020-08-25
- CVE-2020-14508Critical · CVSS 9.8 · EPSS 79th2020-08-25
- CVE-2020-10062Critical · CVSS 9.8 · EPSS 85th2020-06-05
Showing 12 of 94 recorded CWE-193 CVEs. Track new ones as they are published and get AI-written analysis and fixes.
Monitor CWE-193 vulnerabilitiesCommon consequences
What can happen when CWE-193 is exploited.
DoS: Crash, Exit, or Restart, DoS: Resource Consumption (CPU), DoS: Resource Consumption (Memory), DoS: Instability
Affects: 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
Affects: 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
Affects: 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.
How it happens
When it is introduced
Typically introduced during these phases of the software lifecycle.
Applies to
Languages
How to prevent it
Practical mitigations for CWE-193, grouped by where in the lifecycle they apply.
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().
How to detect it
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
Code examples
Illustrative examples from MITRE showing how the weakness appears in code.
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.
Vulnerable example
int i;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.
Vulnerable example
char firstname[20];Safe example
char firstname[20];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.
Vulnerable example
#define PATH_SIZE 60Safe example
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.
Vulnerable example
int setFilename(char *filename) {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.
Illustrative examples
Real CVEs that MITRE cites as examples of this weakness.
- 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.
- CVE-2001-1391 — Off-by-one vulnerability in driver allows users to modify kernel memory.
- CVE-2002-0083 — Off-by-one error allows local users or remote malicious servers to gain privileges.
- 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.
- CVE-2002-0844 — Off-by-one buffer overflow in version control system allows local users to execute arbitrary code.
- 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.
- CVE-2004-0346 — Off-by-one buffer overflow in FTP server allows local users to gain privileges via a 1024 byte RETR command.
- CVE-2004-0005 — Multiple buffer overflows in chat client allow remote attackers to cause a denial of service and possibly execute arbitrary code.
- CVE-2003-0356 — Multiple off-by-one vulnerabilities in product allow remote attackers to cause a denial of service and possibly execute arbitrary code.
- CVE-2001-1496 — Off-by-one buffer overflow in server allows remote attackers to cause a denial of service and possibly execute arbitrary code.
- CVE-2004-0342 — This is an interesting example that might not be an off-by-one.
- 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.
- CVE-2002-1745 — Off-by-one error allows source code disclosure of files with 4 letter extensions that match an accepted 3-letter extension.
- CVE-2002-1816 — Off-by-one buffer overflow.
- CVE-2002-1721 — Off-by-one error causes an snprintf call to overwrite a critical internal variable with a null value.
- 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.
- CVE-2003-0625 — Off-by-one error allows read of sensitive memory via a malformed request.
- CVE-2006-4574 — Chain: security monitoring product has an off-by-one error that leads to unexpected length values, triggering an assertion.
Terminology & mappings
Alternate terms
- 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.
Mapped taxonomies
- PLOVER: Off-by-one Error
- CERT C Secure Coding: Guarantee that storage for strings has sufficient space for character data and the null terminator (STR31-C)
Frequently asked questions
Common questions about CWE-193.
- What is CWE-193?
- A product calculates or uses an incorrect maximum or minimum value that is 1 more, or 1 less, than the correct value.
- What CVEs are caused by CWE-193?
- 94 recorded CVEs are attributed to CWE-193, including CVE-2021-3156, CVE-2025-54349, CVE-2024-10442. 1 are listed in CISA's Known Exploited Vulnerabilities (KEV) catalog.
- How do you prevent CWE-193?
- 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().
- How is CWE-193 detected?
- 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.)
- What are the consequences of CWE-193?
- Exploiting CWE-193 can lead to: DoS: Crash, Exit, or Restart, DoS: Resource Consumption (CPU), DoS: Resource Consumption (Memory), DoS: Instability, Modify Memory, Execute Unauthorized Code or Commands.
- Is CWE-193 actively exploited?
- Yes. 1 CWE-193 vulnerabilities are in CISA's KEV catalog of actively exploited flaws, out of 94 recorded CVEs.
References
- MITRE CWE definition (CWE-193) (opens in a new tab)
- CWE-193 vulnerabilities on NVD (opens in a new tab)
- Learn: What is a CWE?
Weakness data is sourced from the MITRE CWE catalog (v4.20). CVE associations are aggregated and kept current by RadicalNotion.AI.
Stay ahead of CWE-193
Get alerted the moment a new CWE-193 vulnerability affects your stack, with AI-written analysis, severity context, and remediation guidance.