0

My code is working fine until I try to free the allocated memory. I malloced the files pointer, and later on I used realloc to increase the size. But then it gives me invalid pointer error when I try to free the memory, not sure why.

char *files = malloc(1);
char *temp = strdup(argv[i]);
strcat(temp, "/");
strcat(temp, dp->d_name);
DIR *child_dir;
child_dir = opendir (temp);

if (child_dir == NULL) {
    files = realloc(files, strlen(dp->d_name)+1);
    strcat(files, dp->d_name);
    strcat(files, "/");
} else {
    struct dirent *child_dp;
    while ((child_dp = readdir (child_dir)) != NULL) {
        if (!strcmp(child_dp->d_name, ".")
            || !strcmp(child_dp->d_name, ".."))
                continue;

        files = realloc(files, strlen(child_dp->d_name) + 1);
        strcat(files, child_dp->d_name);
        strcat(files, "/");
    }
}
close(fd[0]);
int n = write(fd[1], files, strlen(files));
free(temp); // free
free(files); // free
temp = NULL;
files = NULL;
return;

This is the error I am getting,

======= Backtrace: =========
/lib64/libc.so.6(+0x721af)[0x7fa2e697c1af]
/lib64/libc.so.6(+0x77706)[0x7fa2e6981706]
/lib64/libc.so.6(+0x78453)[0x7fa2e6982453]
./myfind[0x40110c]
./myfind[0x400b02]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7fa2e692a6e5]
./myfind[0x400a09]
======= Memory map: ========

Note: If I run the same code without freeing any memory space, it works fine. Which means that the pointers are pointing to the correct location in the memory.

1
  • 2
    You're causing undefined behavior when you do strcat(temp, "/");. temp is only big enough for the argv[i] string that you copied it from, it doesn't have room for you to concatenate additional strings to it. Commented Oct 14, 2017 at 0:41

1 Answer 1

2

You're corrupting your heap with this code:

char *temp = strdup(argv[i]);
strcat(temp, "/");
strcat(temp, dp->d_name);

strdup only allocates enough space for the string it's duplicating, but you then concatenate more onto the end without reallocating to make room.

You're also not leaving space for the NUL terminator when you realloc files in the if condition, though in most cases you'll get away with that (you should allocate the correct amount though).

Lastly, in the while loop of the else case, each realloc is only allocating enough for the stuff you're adding, but not leaving space for what already exists (and again, no space is left for the NUL terminator). The repeated misuse is guaranteed heap corruption after a while.

Sign up to request clarification or add additional context in comments.

3 Comments

"in most cases you'll get away with that", I'm not sure that may not leave the wrong impression.... maybe the parenthetical should read ("if you fail to allocate the correct about of memory -- you should be shot!"), or maybe just "-- Undefined Behavior results" :)
@DavidC.Rankin: Yeah, I'm just pointing out that there are mistakes that often don't cause symptoms on some or all compilers/systems, so "it works" isn't enough to say the code is correct.
Yes, I understood the crux of what you were saying, it just struck me as a bit odd/humorous on this Friday evening (thus the :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.