CWE-763: Release of Invalid Pointer or Reference
Description
The product attempts to return a memory resource to the system, but it calls the wrong release function or calls the appropriate release function incorrectly.
Submission Date :
May 8, 2009, midnight
Modification Date :
2023-10-26 00:00:00+00:00
Organization :
MITRE
Extended Description
This weakness can take several forms, such as:
- The memory was allocated, explicitly or implicitly, via one memory management method and deallocated using a different, non-compatible function (CWE-762).
- The function calls or memory management routines chosen are appropriate, however they are used incorrectly, such as in CWE-761.
Example - 1
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.
break;if (++ap >= &argv[10])if (**ap != '\0')
char **ap, *argv[10], *inputstring;for (ap = argv; (*ap = strsep(&inputstring, " \t")) != NULL;)/.../free(ap[4]);
Example - 2
This example allocates a BarObj object using the new operator in C++, however, the programmer then deallocates the object using free(), which may lead to unexpected behavior. Instead, the programmer should have either created the object with one of the malloc family functions, or else deleted the object with the delete operator.
// /* do some work with ptr here *///
BarObj *ptr = new BarObj()...free(ptr);void foo(){}
// /* do some work with ptr here *///
BarObj *ptr = new BarObj()...delete ptr;void foo(){}
Example - 3
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.
// /* matched char, free string and return success *///
free(str);return SUCCESS;
// /* didn't match yet, increment pointer and try next char *///
if( *str == c ){}str = str + 1;
// /* we did not match the char in the string, free mem and return failure *///
char *str;str = (char*)malloc(20*sizeof(char));strcpy(str, "Search Me!");while( *str != NULL){}free(str);return FAILURE;#define SUCCESS (1)#define FAILURE (0)int contains_char(char c){}
// /* matched char, free string and return success *///
free(str);return SUCCESS;
// /* didn't match yet, increment pointer and try next char *///
if( str[i] == c ){}i = i + 1;
// /* we did not match the char in the string, free mem and return failure *///
char *str;int i = 0;str = (char*)malloc(20*sizeof(char));strcpy(str, "Search Me!");while( i < strlen(str) ){}free(str);return FAILURE;#define SUCCESS (1)#define FAILURE (0)int cointains_char(char c){}
Example - 4
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.
// //hardcode input length for simplicity//
// /* The following loop will parse and process each token in the input string *///
// /* ignore and discard bad data *///
free( tok );
add_to_command_queue( tok );
if( isMalformed( tok ) ){}else{}tok = strtok( NULL, sep));char* input = (char*) malloc(40*sizeof(char));char *tok;char* sep = " \t";get_user_input( input );tok = strtok( input, sep);while( NULL != tok ){}
// //hardcode input length for simplicity//
// /* The following loop will parse and process each token in the input string *///
// /* copy and enqueue good data *///
command = (char*) malloc( (strlen(tok) + 1) * sizeof(char) );strcpy( command, tok );add_to_command_queue( command );
if( !isMalformed( command ) ){}tok = strtok( NULL, sep));
char* input = (char*) malloc(40*sizeof(char));char *tok, *command;char* sep = " \t";get_user_input( input );tok = strtok( input, sep);while( NULL != tok ){}free( input )
Related Weaknesses
This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.
Visit http://cwe.mitre.org/ for more details.