Malloc: *** Error: Incorrect Checksum for Freed Object - Object Was Probably Modified After Being Freed

Incorrect checksum for freed object on malloc

In read_response, you are probably overwriting the end of the buffer pointed to by buf.

The problem is that buf is a pointer, so sizeof(buf) will return the size of a pointer (probably 4 or 8 depending on your CPU). You are using sizeof as if buf were an array, which is not really the same thing as a pointer in C although they seem interchangeable in some contexts.

Instead of using sizeof, you need to be keeping track of the last size that you allocated for buf, and add BUF_SIZE to that each time you enlarge the buffer.

You should also consider that the read operation may be returning considerably fewer characters than BUF_SIZE on each call, so doing a realloc on buf in each iteration may be overkill. That probably won't cause any problems for you in terms of correctness, though; it will just use more memory than it needs to.

I would do something more like the code below.

#define MIN_BUF_SPACE_THRESHOLD (BUF_SIZE / 2)

char *read_response(int sock) {
int bytes_read;
char *buf = (char*)malloc(BUF_SIZE);
int cur_position = 0;
int space_left = BUF_SIZE;

if (buf == NULL) {
exit(1); /* or try to cope with out-of-memory situation */
}

while ((bytes_read = read(sock, buf + cur_position, space_left)) > 0) {
cur_position += bytes_read;
space_left -= bytes_read;
if (space_left < MIN_BUF_SPACE_THRESHOLD) {
buf = realloc(buf, cur_position + space_left + BUF_SIZE);
if (buf == NULL) {
exit(1); /* or try to cope with out-of-memory situation */
}
space_left += BUF_SIZE;
}
}

This version has the advantage of not trying to allocate more space if the read call comes back with only a few bytes of data.

malloc: Incorrect checksum for freed object

malloc((hi - lo + 1) * sizeof(int)) allocates space for elements indexed from 0 to hi-lo, but for (int k = lo; k <= hi; k++) … aux[k] = … accesses the elements with indices from lo to hi, thus writing outside the allocated memory.

incorrect checksum for freed object - object was probably modified after being freed. how can I fix it?

The problem is you are allocating size of searchTerm but copying the path.

The chances of length of path and length of searchTerm is same is less. Thus accessing out of bound for str3 and invoking undefined behavior.

         str3 = malloc(1+strlen("/") + strlen(searchTerm)+ strlen(dir->d_name) );
if (!str3){
return;
}

strcpy(str3, path); //Here
strcat(str3, "/");
strcat(str3, dir->d_name);

To solve allocate the memory with length of path.

str3 = malloc(1+strlen("/") + strlen(path)+ strlen(dir->d_name) );

How to fix malloc error that object probably modified after being freed

Probably the error is caused by that in tmp[k++] it has exceeded the length of tmp.

malloc error: incorrect checksum for freed object in struct for reading file

This is probably not what you want to do:

source_t * temp = (source_t *) malloc(sizeof(source_t *));

That allocates space for a pointer to a source_t object, nor for a source_t object.

Also, in C you shouldn't cast the return value of malloc. Doing so can hide errors, and make them very hard to find.

malloc: Incorrect checksum for freed object 0x7fd4f4c8bbd0: probably modified after being freed

There are 2 surprising lines in your code:

  • char *path = strcat(_resource_path, filename);

    strcat does not concatenate 2 strings into a third allocated one. It copies the second string at the end of the first. Depending on how _resource_path is allocated, it is quite possible that this very line corrupt the malloc() internal data and ultimately produces the problem. You should write a specific function like this:

    char *concat(const char *s1, const char *s2) {
    size_t len1 = strlen(s1);
    size_t len2 = strlen(s2);
    char *p = malloc(len1 + len2 + 1);
    if (p) {
    memcpy(p, s1, len1);
    memcpy(p + len1, s2, len2 + 1);
    }
    return p;
    }

    And you would free the string returned by char *path = concat(_resource_path, filename); after use.

  • printf("Buffer size at line %d : %d\n", index, sizeof(buffer));

    The buffer size is constant, sizeof(buffer) always evaluates to the value length had at the point buffer was defined (1024). Furthermore, you should use %zu for a size_t argument, not %d, which expects an int, that may have a different size. You might want to write this instead:

    printf("Buffer length at line %d: %zu\n", index, strlen(buffer));


Related Topics



Leave a reply



Submit