---
title: "CWE-786: Access of Memory Location Before Start of Buffer"
url: https://radicalnotion.ai/cwe/cwe-786
type: cwe
description: "The product reads or writes to a buffer using an index or pointer that references a memory location prior to the beginning of the buffer."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-786: Access of Memory Location Before Start of Buffer

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

## Description

The product reads or writes to a buffer using an index or pointer that references a memory location prior to the beginning of the buffer.

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | This typically occurs when a pointer or its index is decremented to a position before the buffer, when pointer arithmetic results in a position before the beginning of the valid memory location, or when a negative index is used. |

## Applicable platforms

**Languages:** Memory-Unsafe (Often), C (Often), C++ (Often)

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Confidentiality | Read Memory | For an out-of-bounds read, the attacker may have access to sensitive information. If the sensitive information contains system details, such as the current buffer's position in memory, this knowledge can be used to craft further attacks, possibly with more severe consequences. |
| Integrity, Availability | Modify Memory, DoS: Crash, Exit, or Restart | Out of bounds memory access will very likely result in the corruption of relevant memory, and perhaps instructions, possibly leading to a crash. |
| Integrity | Modify Memory, Execute Unauthorized Code or Commands | If the corrupted memory can be effectively controlled, it may be possible to execute arbitrary code. If the corrupted memory is data rather than instructions, the system will continue to function with improper changes, possibly in violation of an implicit or explicit policy. |

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

In the following C/C++ example, a utility function is used to trim trailing whitespace from a character string. The function copies the input string to a local character string and uses a while statement to remove the trailing whitespace by moving backward through the string and overwriting whitespace with a NUL character.

Bad code:

```c
char* trimTrailingWhitespace(char *strMessage, int length) {
    char *retMessage;
    char *message = malloc(sizeof(char)*(length+1));

    // copy input string to a temporary string
    char message[length+1];
    int index;
    for (index = 0; index < length; index++) {
        message[index] = strMessage[index];
    }
    message[index] = '\0';

    // trim trailing whitespace
    int len = index-1;
    while (isspace(message[len])) {
        message[len] = '\0';
        len--;
    }

    // return string without trailing whitespace
    retMessage = message;
    return retMessage;
}
```

However, this function can cause a buffer underwrite if the input character string contains all whitespace. On some systems the while statement will move backwards past the beginning of a character string and will call the isspace() function on an address outside of the bounds of the local buffer.

### Example 2

The following example asks a user for an offset into an array to select an item.

Bad code:

```c
int main (int argc, char **argv) {
    char *items[] = {"boat", "car", "truck", "train"};
    int index = GetUntrustedOffset();
    printf("You selected %s\n", items[index-1]);
}
```

The programmer allows the user to specify which element in the list to select, however an attacker can provide an out-of-bounds offset, resulting in a buffer over-read (CWE-126).

### Example 3

The following is an example of code that may result in a buffer underwrite. This code is attempting to replace the substring "Replace Me" in destBuf with the string stored in srcBuf. It does so by using the function strstr(), which returns a pointer to the found substring in destBuf. Using pointer arithmetic, the starting index of the substring is found.

Bad code:

```c
int main() {
    ...
    char *result = strstr(destBuf, "Replace Me");
    int idx = result - destBuf;
    strcpy(&destBuf[idx], srcBuf);
    ...
}
```

In the case where the substring is not found in destBuf, strstr() will return NULL, causing the pointer arithmetic to be undefined, potentially setting the value of idx to a negative number. If idx is negative, this will result in a buffer underwrite of destBuf.

## Observed examples

| CVE | Description |
| --- | --- |
| CVE-2002-2227 | Unchecked length of SSLv2 challenge value leads to buffer underflow. |
| CVE-2007-4580 | Buffer underflow from a small size value with a large buffer (length parameter inconsistency, CWE-130) |
| CVE-2007-1584 | Buffer underflow from an all-whitespace string, which causes a counter to be decremented before the buffer while looking for a non-whitespace character. |
| CVE-2007-0886 | Buffer underflow resultant from encoded data that triggers an integer overflow. |
| CVE-2006-6171 | Product sets an incorrect buffer size limit, leading to "off-by-two" buffer underflow. |
| CVE-2006-4024 | Negative value is used in a memcpy() operation, leading to buffer underflow. |
| CVE-2004-2620 | Buffer underflow due to mishandled special characters |

## Real CVEs with this weakness

| CVE | Title | Severity | CVSS | EPSS | CISA KEV | Published |
| --- | --- | --- | --- | --- | --- | --- |
| CVE-2024-27808 | Safari: WebKit - Processing web content may lead to arbitrary code execution | high | 8.8 | - | No | 2024-06-10 |
| CVE-2024-27831 | iOS and iPadOS: CoreMedia - Processing a file may lead to unexpected app termination or arbitrary code execution | high | 7.8 | - | No | 2024-06-10 |
| CVE-2024-27828 | tvOS: IOSurface - An app may be able to execute arbitrary code with kernel privileges | high | 7.8 | - | No | 2024-06-10 |
| CVE-2022-0351 | Access of Memory Location Before Start of Buffer in vim/vim | high | 7.8 | - | No | 2022-01-25 |
| CVE-2024-27840 | iOS and iPadOS: Kernel - An attacker that has already achieved kernel code execution may be able to bypass kernel memory protections | high | 7.5 | - | No | 2024-06-10 |
| CVE-2022-0522 | Access of Memory Location Before Start of Buffer in radareorg/radare2 | high | 7.1 | - | No | 2022-02-08 |
| CVE-2024-5700 | Mozilla: Memory safety bugs fixed in Firefox 127, Firefox ESR 115.12, and Thunderbird 115.12 | high | 7.0 | - | No | 2024-06-11 |
| CVE-2026-20058 | Cisco Secure Firewall Threat Defense Software Snort 3 Visual Basic for Application Denial of Service Vulnerability | medium | 5.8 | - | No | 2026-03-04 |

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-119](https://radicalnotion.ai/cwe/cwe-119) | Improper Restriction of Operations within the Bounds of a Memory Buffer |
| Child | [CWE-124](https://radicalnotion.ai/cwe/cwe-124) | Buffer Underwrite |
| Child | [CWE-127](https://radicalnotion.ai/cwe/cwe-127) | Buffer Under-read |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| CERT C Secure Coding | Do not form or use out-of-bounds pointers or array subscripts | ARR30-C | CWE More Specific |

## Frequently asked questions

### What is CWE-786?

The product reads or writes to a buffer using an index or pointer that references a memory location prior to the beginning of the buffer.

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

8 recorded CVEs are attributed to CWE-786, including CVE-2024-27808, CVE-2024-27831 and CVE-2024-27828.

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

Exploiting CWE-786 can lead to: Read Memory, Modify Memory, DoS: Crash, Exit, or Restart, Execute Unauthorized Code or Commands.

### Is CWE-786 actively exploited?

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

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

---

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