CWE-761: Free of Pointer not at Start of Buffer

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.

Extended Description:
This can cause the product 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.

Affected languages: Memory-Unsafe, C
Parent weaknesses: CWE-763, CWE-404

Consequences:
  - Integrity: Modify Memory

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 

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] 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.

Demonstrative examples:
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.
#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){ 
                             

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.
char **ap, *argv[10], *inputstring; for (ap = argv; (*ap = strsep(&inputstring, " \t")) != NULL;) 
                         if (**ap != '\0') if (++ap >= &argv[10]) break; 
                            
                         
                      
                      /.../ free(ap[4]);

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