---
title: "CWE-789: Memory Allocation with Excessive Size Value"
url: https://radicalnotion.ai/cwe/cwe-789
type: cwe
description: "The product allocates memory based on an untrusted, large size value, but it does not ensure that the size is within expected limits, allowing arbitrary amounts of memory to be allocated."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-789: Memory Allocation with Excessive Size Value

- **Abstraction:** Variant
- **Structure:** Simple
- **Status:** Draft
- **CVEs with this as primary weakness:** 310
- **Of those, in CISA KEV:** 0
- **New in 2026:** 236
- **CWE catalog version:** 4.21

## Description

The product allocates memory based on an untrusted, large size value, but it does not ensure that the size is within expected limits, allowing arbitrary amounts of memory to be allocated.

## Alternate terms

- **Stack Exhaustion:** When a weakness allocates excessive memory on the stack, it is often described as "stack exhaustion," which is a technical impact of the weakness. This technical impact is often encountered as a consequence of CWE-789 and/or CWE-1325.

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | - |

## Applicable platforms

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

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Availability | DoS: Resource Consumption (Memory) | Not controlling memory allocation can result in a request for too much system memory, possibly leading to a crash of the application due to out-of-memory conditions, or the consumption of a large amount of memory on the system. |

## Potential mitigations

### Implementation, Architecture and Design

Perform adequate input validation against any value that influences the amount of memory that is allocated. Define an appropriate strategy for handling requests that exceed the limit, and consider supporting a configuration option so that the administrator can extend the amount of memory to be used if necessary.

### Operation

Run your program using system-provided resource limits for memory. This might still cause the program to crash or exit, but the impact to the rest of the system will be minimized.

## Detection methods

### Fuzzing

Fuzz testing (fuzzing) is a powerful technique for generating large numbers of diverse inputs - either randomly or algorithmically - and dynamically invoking the code with those inputs. Even with random inputs, it is often capable of generating unexpected results such as crashes, memory corruption, or resource consumption. Fuzzing effectively produces repeatable test cases that clearly indicate bugs, which helps developers to diagnose the issues.

Effectiveness: High

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

### Automated Dynamic Analysis

Use tools that are integrated during compilation to insert runtime error-checking mechanisms related to memory safety errors, such as AddressSanitizer (ASan) for C/C++ \[REF-1518\].

Effectiveness: Moderate. Crafted inputs are necessary to reach the code containing the error, such as generated by fuzzers. Also, these tools may reduce performance, and they only report the error condition - not the original mistake that led to the error.

## Demonstrative examples

### Example 1

Consider the following code, which accepts an untrusted size value and allocates a buffer to contain a string of the given size.

Bad code:

```c
unsigned int size = GetUntrustedInt();
/* ignore integer overflow (CWE-190) for this example */

unsigned int totBytes = size * sizeof(char);
char *string = (char *)malloc(totBytes);
InitializeString(string);
```

### Example 2

Consider the following code, which accepts an untrusted size value and uses the size as an initial capacity for a HashMap.

Bad code:

```java
unsigned int size = GetUntrustedInt();
HashMap list = new HashMap(size);
```

The HashMap constructor will verify that the initial capacity is not negative, however there is no check in place to verify that sufficient memory is present. If the attacker provides a large enough value, the application will run into an OutOfMemoryError.

### Example 3

This code performs a stack allocation based on a length calculation.

Bad code:

```c
int a = 5, b = 6;
size_t len = a - b;
char buf[len];    // Just blows up the stack
}
```

### Example 4

This example shows a typical attempt to parse a string with an error resulting from a difference in assumptions between the caller to a function and the function's action.

Bad code:

```c
int proc_msg(char *s, int msg_len)
{
// Note space at the end of the string - assume all strings have preamble with space
int pre_len = sizeof("preamble: ");
char buf[pre_len - msg_len];
... Do processing here if we get this far
}
char *s = "preamble: message\n";
char *sl = strchr(s, ':');        // Number of characters up to ':' (not including space)
int jnklen = sl == NULL ? 0 : sl - s;    // If undefined pointer, use zero length
int ret_val = proc_msg ("s",  jnklen);    // Violate assumption of preamble length, end up with negative value, blow out stack
```

The buffer length ends up being -1, resulting in a blown out stack. The space character after the colon is included in the function calculation, but not in the caller's calculation. This, unfortunately, is not usually so obvious but exists in an obtuse series of calculations.

### Example 5

The following code obtains an untrusted number that is used as an index into an array of messages.

Bad code:

```perl
my $num = GetUntrustedNumber();
my @messages = ();

$messages[$num] = "Hello World";
```

### Example 6

This example shows a typical attempt to parse a string with an error resulting from a difference in assumptions between the caller to a function and the function's action. The buffer length ends up being -1 resulting in a blown out stack. The space character after the colon is included in the function calculation, but not in the caller's calculation. This, unfortunately, is not usually so obvious but exists in an obtuse series of calculations.

Bad code:

```c
int proc_msg(char *s, int msg_len)
{
    int pre_len = sizeof("preamble: ");    // Note space at the end of the string - assume all strings have preamble with space
    char buf[pre_len - msg_len];
    ... Do processing here and set status
    return status;
}
char *s = "preamble: message\n";
char *sl = strchr(s, ':');        // Number of characters up to ':' (not including space)
int jnklen = sl == NULL ? 0 : sl - s;    // If undefined pointer, use zero length
int ret_val = proc_msg ("s",  jnklen);    // Violate assumption of preamble length, end up with negative value, blow out stack
```

Good code:

```c
int proc_msg(char *s, int msg_len)
{
    int pre_len = sizeof("preamble: ");    // Note space at the end of the string - assume all strings have preamble with space
    if (pre_len <= msg_len) { // Log error; return error_code; }
    char buf[pre_len - msg_len];
    ... Do processing here and set status
    return status;
}
char *s = "preamble: message\n";
char *sl = strchr(s, ':');        // Number of characters up to ':' (not including space)
int jnklen = sl == NULL ? 0 : sl - s;    // If undefined pointer, use zero length
int ret_val = proc_msg ("s",  jnklen);    // Violate assumption of preamble length, end up with negative value, blow out stack
```

## Observed examples

| CVE | Description |
| --- | --- |
| CVE-2023-2253 | Query capability for API endpoint allows a large value for number of records to return, leading to allocation of a large array |
| CVE-2019-19911 | Chain: Python library does not limit the resources used to process images that specify a very large number of bands (CWE-1284), leading to excessive memory consumption (CWE-789) or an integer overflow (CWE-190). |
| CVE-2010-3701 | program uses ::alloca() for encoding messages, but large messages trigger segfault |
| CVE-2008-1708 | memory consumption and daemon exit by specifying a large value in a length field |
| CVE-2008-0977 | large value in a length field leads to memory consumption and crash when no more memory is available |
| CVE-2006-3791 | large key size in game program triggers crash when a resizing function cannot allocate enough memory |
| CVE-2004-2589 | large Content-Length HTTP header value triggers application crash in instant messaging application due to failure in memory allocation |

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

| CVE | Title | Severity | CVSS | EPSS | CISA KEV | Published |
| --- | --- | --- | --- | --- | --- | --- |
| CVE-2023-43632 | Freely Allocate Buffer on The Stack With Data From Socket | critical | 9.9 | - | No | 2023-09-21 |
| CVE-2026-82435 | Apache Storm Worker: Unauthenticated Remote Memory Exhaustion in the Worker Messaging Decoder | critical | 9.8 | - | No | 2026-09-14 |
| CVE-2023-24201 | - | critical | 9.8 | - | No | 2023-02-06 |
| CVE-2026-61845 | Crass: Large numeric exponents cause CPU and memory denial of service | high | 8.9 | - | No | 2026-10-30 |
| CVE-2026-77410 | RabbitMQ amqp091-go: Resource Exhaustion (OOM) via Unbounded Body Buffer Allocation | high | 8.9 | - | No | 2026-09-16 |
| CVE-2022-36078 | Slice Memory Allocation with Excessive Size Value in binary | high | 8.8 | - | No | 2022-09-02 |
| CVE-2021-34869 | Parallels Desktop Toolgate Uncontrolled Memory Allocation Privilege Escalation Vulnerability | high | 8.8 | - | No | 2022-01-25 |
| CVE-2021-34868 | Parallels Desktop Toolgate Uncontrolled Memory Allocation Privilege Escalation Vulnerability | high | 8.8 | - | No | 2022-01-25 |
| CVE-2026-103025 | bep/imagemeta: Unbounded memory allocation decoding PNG \`zTXt\` "Raw profile type iptc" chunks | high | 8.7 | - | No | 2026-11-19 |
| CVE-2026-133234 | Stoatchat before 0.16.5 Memory Exhaustion via January /proxy JXL Decoding | high | 8.7 | - | No | 2026-11-19 |
| CVE-2026-123616 | imgproxy 3.3.0 before 4.0.16 Memory Exhaustion DoS via ICO Loader | high | 8.7 | - | No | 2026-11-08 |
| CVE-2026-123563 | Devalue before 5.9.3 Typed-Array Allocation Validation Bypass via parse | high | 8.7 | - | No | 2026-11-08 |

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-770](https://radicalnotion.ai/cwe/cwe-770) | Allocation of Resources Without Limits or Throttling |
| Peer | [CWE-1325](https://radicalnotion.ai/cwe/cwe-1325) | Improperly Controlled Sequential Memory Allocation |
| Can precede | [CWE-476](https://radicalnotion.ai/cwe/cwe-476) | NULL Pointer Dereference |
| Related | [CWE-129](https://radicalnotion.ai/cwe/cwe-129) | Improper Validation of Array Index |
| Related | [CWE-1284](https://radicalnotion.ai/cwe/cwe-1284) | Improper Validation of Specified Quantity in Input |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| WASC | SOAP Array Abuse | 35 | - |
| CERT C Secure Coding | Allocate sufficient memory for an object | MEM35-C | Imprecise |
| SEI CERT Perl Coding Standard | Validate any integer that is used as an array index | IDS32-PL | Imprecise |
| OMG ASCSM | - | ASCSM-CWE-789 | - |

## Frequently asked questions

### What is CWE-789?

The product allocates memory based on an untrusted, large size value, but it does not ensure that the size is within expected limits, allowing arbitrary amounts of memory to be allocated.

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

310 recorded CVEs are attributed to CWE-789, including CVE-2023-43632, CVE-2026-82435 and CVE-2023-24201.

### How do you prevent CWE-789?

Perform adequate input validation against any value that influences the amount of memory that is allocated. Define an appropriate strategy for handling requests that exceed the limit, and consider supporting a configuration option so that the administrator can extend the amount of memory to be used if necessary.

### How is CWE-789 detected?

Fuzzing: Fuzz testing (fuzzing) is a powerful technique for generating large numbers of diverse inputs - either randomly or algorithmically - and dynamically invoking the code with those inputs. Even with random inputs, it is often capable of generating unexpected results such as crashes, memory corruption, or resource consumption. Fuzzing effectively produces repeatable test cases that clearly indicate bugs, which helps developers to diagnose the issues.

### What are the consequences of CWE-789?

Exploiting CWE-789 can lead to: DoS: Resource Consumption (Memory).

### Is CWE-789 actively exploited?

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

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

---

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