---
title: "CWE-839: Numeric Range Comparison Without Minimum Check"
url: https://radicalnotion.ai/cwe/cwe-839
type: cwe
description: "The product checks a value to ensure that it is less than or equal to a maximum, but it does not also verify that the value is greater than or equal to the minimum."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-839: Numeric Range Comparison Without Minimum Check

- **Abstraction:** Base
- **Structure:** Simple
- **Status:** Incomplete
- **CVEs with this as primary weakness:** 20
- **Of those, in CISA KEV:** 0
- **New in 2026:** 9
- **CWE catalog version:** 4.21

## Description

The product checks a value to ensure that it is less than or equal to a maximum, but it does not also verify that the value is greater than or equal to the minimum.

## Alternate terms

- **Signed comparison:** The "signed comparison" term is often used to describe when the product uses a signed variable and checks it to ensure that it is less than a maximum value (typically a maximum buffer size), but does not verify that it is greater than 0.

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | Some products use signed integers or floats even when their values are only expected to be positive or 0. An input validation check might assume that the value is positive, and only check for the maximum value. |

## Applicable platforms

**Languages:** C (Often), C++ (Often)

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Confidentiality, Integrity | Modify Memory, Read Memory | If a negative value is used to access memory, buffers, or other indexable structures, it could access memory outside the bounds of the buffer. |
| Availability | DoS: Resource Consumption (Memory), DoS: Resource Consumption (Other) | In some contexts, a negative value could lead to resource consumption, e.g., if the negative value is used for memory allocation. |
| Integrity, Confidentiality, Availability | Modify Application Data, Execute Unauthorized Code or Commands | An attacker could modify the structure of the message or data being sent to the downstream component, possibly injecting commands. |
| Integrity | Varies by Context, Unexpected State | The use of a negative number in a positive-only context could have security implications for other types of resources. For example, a shopping cart might check that the user is not requesting more than 10 items, but a request for -3 items could cause the application to calculate a negative price and credit the attacker's account. |

## Potential mitigations

### Implementation (Enforcement by Conversion)

If the number to be used is always expected to be positive, change the variable type from signed to unsigned or size\_t.

### Implementation (Input Validation)

If the number to be used could have a negative value based on the specification (thus requiring a signed value), but the number should only be positive to preserve code correctness, then include a check to ensure that the value is positive.

## Detection methods

### 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.)

## Demonstrative examples

### Example 1

The following code is intended to read an incoming packet from a socket and extract one or more headers.

Bad code:

```c
DataPacket *packet;
int numHeaders;
PacketHeader *headers;

sock=AcceptSocketConnection();
ReadPacket(packet, sock);
numHeaders =packet->headers;

if (numHeaders > 100) {
    ExitError("too many headers!");
}
headers = malloc(numHeaders * sizeof(PacketHeader);
ParsePacketHeaders(packet, headers);
```

The code performs a check to make sure that the packet does not contain too many headers. However, numHeaders is defined as a signed int, so it could be negative. If the incoming packet specifies a value such as -3, then the malloc calculation will generate a negative number (say, -300 if each header can be a maximum of 100 bytes). When this result is provided to malloc(), it is first converted to a size\_t type. This conversion then produces a large value such as 4294966996, which may cause malloc() to fail or to allocate an extremely large amount of memory (CWE-195). With the appropriate negative numbers, an attacker could trick malloc() into using a very small positive number, which then allocates a buffer that is much smaller than expected, potentially leading to a buffer overflow.

### Example 2

The following code reads a maximum size and performs a sanity check on that size. It then performs a strncpy, assuming it will not exceed the boundaries of the array. While the use of "short s" is forced in this particular example, short int's are frequently used within real-world code, such as code that processes structured data.

Bad code:

```c
int GetUntrustedInt () {
    return(0x0000FFFF);
}

void main (int argc, char **argv) {
    char path[256];
    char *input;
    int i;
    short s;
    unsigned int sz;

    i = GetUntrustedInt();
    s = i;
    /* s is -1 so it passes the safety check - CWE-697 */
    if (s > 256) {
        DiePainfully("go away!\n");
    }

    /* s is sign-extended and saved in sz */
    sz = s;

    /* output: i=65535, s=-1, sz=4294967295 - your mileage may vary */
    printf("i=%d, s=%d, sz=%u\n", i, s, sz);

    input = GetUserInput("Enter pathname:");

    /* strncpy interprets s as unsigned int, so it's treated as MAX_INT
    (CWE-195), enabling buffer overflow (CWE-119) */
    strncpy(path, input, s);
    path[255] = '\0'; /* don't want CWE-170 */
    printf("Path is: %s\n", path);
}
```

This code first exhibits an example of CWE-839, allowing "s" to be a negative number. When the negative short "s" is converted to an unsigned integer, it becomes an extremely large positive integer. When this converted integer is used by strncpy() it will lead to a buffer overflow (CWE-119).

### Example 3

In the following code, the method retrieves a value from an array at a specific array index location that is given as an input parameter to the method

Bad code:

```c
int getValueFromArray(int *array, int len, int index) {

    int value;

    // check that the array index is less than the maximum

    // length of the array
    if (index < len) {

        // get the value at the specified index of the array
        value = array[index];
    }
    // if array index is invalid then output error message

    // and return value indicating error
    else {
        printf("Value is: %d\n", array[index]);
        value = -1;
    }

    return value;
}
```

Good code:

```c
...

// check that the array index is within the correct

// range of values for the array
if (index >= 0 && index < len) {

...
```

However, this method only verifies that the given array index is less than the maximum length of the array but does not check for the minimum value (CWE-839). This will allow a negative value to be accepted as the input array index, which will result in reading data before the beginning of the buffer (CWE-127) and may allow access to sensitive memory. The input array index should be checked to verify that is within the maximum and minimum range required for the array (CWE-129). In this example the if statement should be modified to include a minimum range check, as shown below.

### Example 4

The following code shows a simple BankAccount class with deposit and withdraw methods.

Bad code:

```java
public class BankAccount {

    public final int MAXIMUM_WITHDRAWAL_LIMIT = 350;

    // variable for bank account balance
    private double accountBalance;

    // constructor for BankAccount
    public BankAccount() {
        accountBalance = 0;
    }

    // method to deposit amount into BankAccount
    public void deposit(double depositAmount) {...}

    // method to withdraw amount from BankAccount
    public void withdraw(double withdrawAmount) {

        if (withdrawAmount < MAXIMUM_WITHDRAWAL_LIMIT) {

            double newBalance = accountBalance - withdrawAmount;
            accountBalance = newBalance;
        }
        else {
            System.err.println("Withdrawal amount exceeds the maximum limit allowed, please try again...");
            ...
        }
    }

    // other methods for accessing the BankAccount object
    ...
}
```

Good code:

```java
public class BankAccount {

    public final int MINIMUM_WITHDRAWAL_LIMIT = 0;
    public final int MAXIMUM_WITHDRAWAL_LIMIT = 350;

    ...

    // method to withdraw amount from BankAccount
    public void withdraw(double withdrawAmount) {

        if (withdrawAmount < MAXIMUM_WITHDRAWAL_LIMIT &&
        withdrawAmount > MINIMUM_WITHDRAWAL_LIMIT) {

            ...
```

## Observed examples

| CVE | Description |
| --- | --- |
| CVE-2010-1866 | Chain: integer overflow (CWE-190) causes a negative signed value, which later bypasses a maximum-only check (CWE-839), leading to heap-based buffer overflow (CWE-122). |
| CVE-2009-1099 | Chain: 16-bit counter can be interpreted as a negative value, compared to a 32-bit maximum value, leading to buffer under-write. |
| CVE-2011-0521 | Chain: kernel's lack of a check for a negative value leads to memory corruption. |
| CVE-2010-3704 | Chain: parser uses atoi() but does not check for a negative value, which can happen on some platforms, leading to buffer under-write. |
| CVE-2010-2530 | Chain: Negative value stored in an int bypasses a size check and causes allocation of large amounts of memory. |
| CVE-2009-3080 | Chain: negative offset value to IOCTL bypasses check for maximum index, then used as an array index for buffer under-read. |
| CVE-2008-6393 | Chain: file transfer client performs signed comparison, leading to integer overflow and heap-based buffer overflow. |
| CVE-2008-4558 | Chain: negative ID in media player bypasses check for maximum index, then used as an array index for buffer under-read. |

## Real CVEs with this weakness (sample of 12 of 20)

| CVE | Title | Severity | CVSS | EPSS | CISA KEV | Published |
| --- | --- | --- | --- | --- | --- | --- |
| CVE-2026-68406 | wifi: cfg80211: validate PMSR FTM preamble range | critical | 9.8 | - | No | 2026-08-10 |
| CVE-2026-53176 | IB/isert: Reject login PDUs shorter than ISER\_HEADERS\_LEN | critical | 9.8 | - | No | 2026-06-25 |
| CVE-2026-93228 | svcrdma: Reject Write/Reply chunks with segcount 0 | critical | 9.1 | - | No | 2026-09-24 |
| CVE-2023-22854 | - | critical | 9.1 | - | No | 2023-02-13 |
| CVE-2023-0425 | Buffer overflow in global memory region | high | 8.6 | - | No | 2023-08-07 |
| CVE-2025-40169 | bpf: Reject negative offsets for ALU ops | high | 7.8 | - | No | 2025-11-12 |
| CVE-2026-57186 | Asterisk: CVE-2022-37325 fix is absent from current chan\_ooh323 Q.931 party-number parser | high | 7.5 | - | No | 2026-10-27 |
| CVE-2019-20925 | Denial of service via malformed network packet | high | 7.5 | - | No | 2020-11-24 |
| CVE-2023-53726 | arm64: csum: Fix OoB access in IP checksum code for negative lengths | high | 7.1 | - | No | 2025-10-22 |
| CVE-2026-118384 | mtools: mtools: Denial of Service via out-of-bounds write in VFAT parsing | medium | 6.1 | - | No | 2026-10-28 |
| CVE-2026-53320 | nilfs2: reject zero bd\_oblocknr in nilfs\_ioctl\_mark\_blocks\_dirty() | medium | 5.5 | - | No | 2026-06-26 |
| CVE-2022-50781 | amdgpu/pm: prevent array underflow in vega20\_odn\_edit\_dpm\_table() | medium | 5.5 | - | No | 2025-12-24 |

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-1023](https://radicalnotion.ai/cwe/cwe-1023) | Incomplete Comparison with Missing Factors |
| Parent | [CWE-1284](https://radicalnotion.ai/cwe/cwe-1284) | Improper Validation of Specified Quantity in Input |
| Can precede | [CWE-119](https://radicalnotion.ai/cwe/cwe-119) | Improper Restriction of Operations within the Bounds of a Memory Buffer |
| Can precede | [CWE-124](https://radicalnotion.ai/cwe/cwe-124) | Buffer Underwrite |
| Can precede | [CWE-195](https://radicalnotion.ai/cwe/cwe-195) | Signed to Unsigned Conversion Error |
| Can precede | [CWE-682](https://radicalnotion.ai/cwe/cwe-682) | Incorrect Calculation |

## Frequently asked questions

### What is CWE-839?

The product checks a value to ensure that it is less than or equal to a maximum, but it does not also verify that the value is greater than or equal to the minimum.

### What CVEs are caused by CWE-839?

20 recorded CVEs are attributed to CWE-839, including CVE-2026-68406, CVE-2026-53176 and CVE-2026-93228.

### How do you prevent CWE-839?

If the number to be used is always expected to be positive, change the variable type from signed to unsigned or size\_t.

### How is CWE-839 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-839?

Exploiting CWE-839 can lead to: Modify Memory, Read Memory, DoS: Resource Consumption (Memory), DoS: Resource Consumption (Other), Modify Application Data, Execute Unauthorized Code or Commands.

### Is CWE-839 actively exploited?

20 recorded CVEs are caused by CWE-839; none are currently in CISA's KEV catalog of actively exploited flaws.

Primary source: [MITRE CWE](https://cwe.mitre.org/data/definitions/839.html)

---

Source: [RadicalNotion.AI](https://radicalnotion.ai/cwe/cwe-839). Licensed CC BY-NC 4.0; attribute RadicalNotion.AI and link the source page when you reuse this data.
