Free of Pointer not at Start of Buffer |
Weakness ID: 761 (Weakness Variant) | Status: Incomplete |
Description Summary
Extended Description
This can cause the application to crash, or in some cases, modify critical program variables or execute code.
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.
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.
However, if the character is not at the beginning of the string, or if it is not in the string at all, then the pointer will not be at the start of the buffer when the programmer frees it.
Instead of freeing the pointer in the middle of the buffer, the programmer can use an indexing pointer to step through the memory or abstract the memory calculations by using array indexing.
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.
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.
While the above code attempts to free memory associated with bad commands, since the memory was all allocated in one chunk, it must all be freed together.
One way to fix this problem would be to copy the commands into a new memory location before placing them in the queue. Then, after all commands have been processed, the memory can safely be freed.
Phase: 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. |
Phase: Implementation When programming in C++, consider using smart pointers provided by the boost library to help correctly and consistently manage memory. |
Phases: Implementation; Operation Use a library that contains built-in protection against free of invalid pointers, such as glibc. |
Phase: Architecture and Design Use a language that provides abstractions for memory allocation and deallocation. |
Phase: Testing Use a tool that dynamically detects memory management problems, such as valgrind. |
Nature | Type | ID | Name | View(s) this relationship pertains to![]() |
---|---|---|---|---|
ChildOf | ![]() | 399 | Resource Management Errors | Development Concepts (primary)699 |
ChildOf | ![]() | 763 | Release of Invalid Pointer or Reference | Research Concepts (primary)1000 |
"boost C++ Library Smart Pointers". <http://www.boost.org/doc/libs/1_38_0/libs/smart_ptr/smart_ptr.htm>. |
"Valgrind". <http://valgrind.org/>. |
Currently, CWE-763 is the parent, however it may be desirable to have an intermediate parent which is not function-specific, similar to how CWE-762 is an intermediate parent between CWE-763 and CWE-590. |