---
title: "CWE-761: Free of Pointer not at Start of Buffer"
url: https://radicalnotion.ai/cwe/cwe-761
type: cwe
description: "The product calls free() on a pointer to a memory resource that was allocated on the heap, but the pointer is not at the start 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-761: Free of Pointer not at Start of Buffer

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

## Description

The product calls free() on a pointer to a memory resource that was allocated on the heap, but the pointer is not at the start of the buffer.

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | This weakness often occurs when the memory is allocated explicitly on the heap with one of the malloc() family functions and free() is called, but pointer arithmetic has caused the pointer to be in the interior or end of the buffer. |

## Applicable platforms

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

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Integrity, Availability, Confidentiality | Modify Memory, DoS: Crash, Exit, or Restart, Execute Unauthorized Code or Commands | This can cause the product to crash, or in some cases, modify critical program variables or execute code. |

## Potential mitigations

### Implementation

When utilizing pointer arithmetic to traverse a buffer, use a separate variable to track progress through memory and preserve the originally allocated address for later freeing.

### Implementation

When programming in C++, consider using smart pointers provided by the boost library to help correctly and consistently manage memory.

### Architecture and Design (Libraries or Frameworks)

Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid.

For example, glibc in Linux provides protection against free of invalid pointers.

### Architecture and Design

Use a language that provides abstractions for memory allocation and deallocation.

## Detection methods

### 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\] or valgrind \[REF-480\].

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 this example, the programmer dynamically allocates a buffer to hold a string and then searches for a specific character. After completing the search, the programmer attempts to release the allocated memory and return SUCCESS or FAILURE to the caller. Note: for simplification, this example uses a hard-coded "Search Me!" string and a constant string length of 20.

Bad code:

```c
#define SUCCESS (1)
#define FAILURE (0)

int contains_char(char c){
    char *str;
    str = (char*)malloc(20*sizeof(char));
    strcpy(str, "Search Me!");
    while( *str != NULL){
        if( *str == c ){

            /* matched char, free string and return success */
            free(str);
            return SUCCESS;
        }
        /* didn't match yet, increment pointer and try next char */

        str = str + 1;
    }
    /* we did not match the char in the string, free mem and return failure */

    free(str);
    return FAILURE;
}
```

Good code:

```c
#define SUCCESS (1)
#define FAILURE (0)

int contains_char(char c){
    char *str;
    int i = 0;
    str = (char*)malloc(20*sizeof(char));
    strcpy(str, "Search Me!");
    while( i < strlen(str) ){
        if( str[i] == c ){

            /* matched char, free string and return success */
            free(str);
            return SUCCESS;
        }
        /* didn't match yet, increment pointer and try next char */

        i = i + 1;
    }
    /* we did not match the char in the string, free mem and return failure */

    free(str);
    return FAILURE;
}
```

### Example 2

This code attempts to tokenize a string and place it into an array using the strsep function, which inserts a \\0 byte in place of whitespace or a tab character. After finishing the loop, each string in the AP array points to a location within the input string.

Bad code:

```c
char **ap, *argv[10], *inputstring;
for (ap = argv; (*ap = strsep(&inputstring, " \t")) != NULL;)
    if (**ap != '\0')
        if (++ap >= &argv[10])
            break;

/.../
free(ap[4]);
```

Since strsep is not allocating any new memory, freeing an element in the middle of the array is equivalent to free a pointer in the middle of inputstring.

### Example 3

Consider the following code in the context of a parsing application to extract commands out of user data. The intent is to parse each command and add it to a queue of commands to be executed, discarding each malformed entry.

Bad code:

```c
//hardcode input length for simplicity
char* input = (char*) malloc(40*sizeof(char));
char *tok;
char* sep = " \t";

get_user_input( input );

/* The following loop will parse and process each token in the input string */

tok = strtok( input, sep);
while( NULL != tok ){
    if( isMalformed( tok ) ){

        /* ignore and discard bad data */
        free( tok );
    }
    else{
        add_to_command_queue( tok );
    }
    tok = strtok( NULL, sep));
}
```

Good code:

```c
//hardcode input length for simplicity
char* input = (char*) malloc(40*sizeof(char));
char *tok, *command;
char* sep = " \t";

get_user_input( input );

/* The following loop will parse and process each token in the input string */

tok = strtok( input, sep);
while( NULL != tok ){
    if( !isMalformed( command ) ){

        /* copy and enqueue good data */
        command = (char*) malloc( (strlen(tok) + 1) * sizeof(char) );
        strcpy( command, tok );
        add_to_command_queue( command );
    }
    tok = strtok( NULL, sep));
}

free( input )
```

## Observed examples

| CVE | Description |
| --- | --- |
| CVE-2019-11930 | function "internally calls 'calloc' and returns a pointer at an index... inside the allocated buffer. This led to freeing invalid memory." |

## Real CVEs with this weakness

| CVE | Title | Severity | CVSS | EPSS | CISA KEV | Published |
| --- | --- | --- | --- | --- | --- | --- |
| CVE-2025-47749 | - | high | 8.4 | - | No | 2025-05-19 |
| CVE-2024-31413 | - | medium | 5.9 | - | No | 2024-05-01 |

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-404](https://radicalnotion.ai/cwe/cwe-404) | Improper Resource Shutdown or Release |
| Parent | [CWE-763](https://radicalnotion.ai/cwe/cwe-763) | Release of Invalid Pointer or Reference |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| Software Fault Patterns | Faulty Memory Release | SFP12 | - |

## Frequently asked questions

### What is CWE-761?

The product calls free() on a pointer to a memory resource that was allocated on the heap, but the pointer is not at the start of the buffer.

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

2 recorded CVEs are attributed to CWE-761, including CVE-2025-47749 and CVE-2024-31413.

### How do you prevent CWE-761?

When utilizing pointer arithmetic to traverse a buffer, use a separate variable to track progress through memory and preserve the originally allocated address for later freeing.

### How is CWE-761 detected?

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++ or valgrind.

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

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

### Is CWE-761 actively exploited?

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

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

---

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