Most common reason for glibc – detected:-
- When we are trying to free some memory which is logically not available for “free ()” .
- When we are trying to free some memory which is already free.
- When we are trying to use multiple “free()” for the same variable.
- When we are trying to free pointers.
- When we are trying to free the variable from outside the scope of variable.
- Using an uninitialized pointer.
- Writing outside your array bounds.
To avoid or solve glibc detection follows some basic and common rule:-
- Set pointer to NULL after free
- Check for NULL before freeing.
- Initialize pointer to NULL in the start.
- Always allocate storage blocks with calloc or some other means of initializing every block to known all-zeros.
- Write test-scripts to independently exercise all major pieces of the program in isolation to the others. Run these tests all the time to catch regressions.
- Use debugging-tools that add “eyecatchers” around storage-blocks to detect “off-by-one” overwrites and other similar problems. Consider leaving them in, in the production(!) code.
Example:-
|
Solution- s=”hello”;
Here we are assigning another address to s, to a statically allocated memory. Freeing it is not correct. Also, since we are doing this, we are basically leaking the memory you have allocated here:
. char* s =(char*) malloc(sizeof(char)*10);
#include <stdio.h> #include <string.h>
#include<stdlib.h>
int main(){
static const size_t kBufferSize = 10;
char* s = (char*)malloc(sizeof(char)*kBufferSize);
strncpy(s,”hello”, kBufferSize); // better than strcpy, you are protecting
// yourself from a buffer overflow
free(s);
}
Best Open Source Business Intelligence Software Helical Insight is Here