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
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
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.
253 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. 103 new CWE-193 CVEs have been recorded so far in 2026 (22 in 2025).
Sudo Heap-Based Buffer Overflow Vulnerability
iperf3: iperf Heap Buffer Overflow
Synology DiskStation DS1823xs+ Replication Service Out-Of-Bounds Write Remote Code Execution Vulnerability
Java SE: 2D - takeover
JSON::XS versions from 3.04 before 4.05 for Perl write one byte past a heap buffer when appending a one-byte chunk in incr_parse
qede: fix off-by-one in BD ring consumption on build_skb failure
ocfs2/dlm: fix off-by-one in dlm_match_regions() region comparison
net: bcmgenet: fix off-by-one in bcmgenet_put_txcb
bpf, arm64: Fix off-by-one in check_imm signed range check
fastnetmon: off-by-one heap-based buffer overflow in the dynamic_binary_buffer_t class
XML::Parser versions through 2.47 for Perl has an off-by-one heap buffer overflow in st_serial_stack
i40e: fix input validation logic for action_meta
Showing 12 of 253 recorded CWE-193 CVEs. Get an alert for each new one, with AI-written analysis and fixes.
Monitor these CVEsWhat 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.
Typically introduced during these phases of the software lifecycle.
Languages
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().
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
MITRE examples of the weakness 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;
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.
Vulnerable example
char firstname[20];
char lastname[20];
char fullname[40];
fullname[0] = '\0';
strncat(fullname, firstname, 20);
strncat(fullname, lastname, 20);Safe example
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.
Vulnerable example
#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;
}
}Safe 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) {
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.
Real CVEs that MITRE cites as examples of this weakness.
Common questions about CWE-193.
Weakness data is sourced from the MITRE CWE catalog (v4.21). CVE associations are aggregated and kept current by RadicalNotion.AI.
Get an alert when a new CWE-193 CVE affects your products, with AI-written analysis and remediation steps.