0

I'm nearing the end of this pset, having successfully implemented both parse and lookup. I've already had one attempt at load and, thinking it was correct (it is not), moved onto index; in the process of writing and testing this I came across this error:

*** Error in `./server': malloc(): memory corruption: 0x0000000001809ad0 ***
Aborted

The error occurs in the block below:

    void transfer(const char* path, const char* type)
    {
        // ensure path is readable
        if (access(path, R_OK) == -1)
        {
            error(403);
            return;
        }

        // open file


        FILE* file = fopen(path, "r");

        if (file == NULL)
        {
            error(500);
            return;
        }

After debugging/using GDB, I found it occurs specifically on the line:

FILE* file = fopen(path, "r");

After some searching on google and stack exchange, I found the issue seems to come from potential overflow of allocated memory or something similar. Since the 'index' function is the only place where I have allocated memory by this point, I believe the error to stem from it. This is my index function at the moment:

char* indexes(const char* path)
{
    // open directory
    DIR* dir = opendir(path);

    // store all the files in namelist
    struct dirent** namelist = NULL;
    int n = scandir(path, &namelist, NULL, alphasort);
    for (int i = 0; i < n; i++)
    {
        // print out current entry for debugging purposes
        printf("Current directory item = %s\n", namelist[i]->d_name);
        if (strcmp(namelist[i]->d_name, "index.php") == 0)
        {
            // allocate enough memory to store string and terminator
            char* index = NULL;
            index = calloc(1, sizeof(path) + 10);

            // copy path into allocated memory and add file name onto the end
            strcpy(index, path);
            strcat(index, "index.php");

            // free allocated memory and close directory
            freedir(namelist, n);
            closedir(dir);
            return index;
        }
        else if (strcmp(namelist[i]->d_name, "index.html") == 0)
        {
            // allocate memory
            char* index = NULL;
            index = calloc(1, sizeof(path) + 11);

            // copy and concatenate strings
            strcpy(index, path);
            strcat(index, "index.html");

            freedir(namelist, n);
            closedir(dir);
            return index;
        }
    }
    freedir(namelist, n);
    closedir(dir);

    // index files not found
    return NULL;
}

I'm aware a bit of error checking is missing and lots of code is copied and pasted, but at the moment I'm trying to nail the core functionality before cleaning it up. So my question is, does anyone know why I'm getting this error?

Thanks in advance for any help!

1

I don't know if sizeof(path) is causing all your problems, but it sure is a problem. path is a pointer, sizeof(path) is 8. If path is something like "/home/ubuntu/workspace/pset6/public", well you see the problem. Think strlen.

You might also consider how you could use access() and complete this function with lots fewer lines of code, lots less memory and overall lots less work for you and the computer. (And (unless I miss my guess), if you do scandir with alphasort, you won't pass check50/server2 because it will always find index.html before it finds index.php).

Don't forget about valgrind as a troubleshooting tool.

  • Thanks for the help! Now that you've pointed it out, I can't believe I didn't see the sizeof issue and it definitely seems like the main issue here. Looking at the man page for access, I can see why that would save me a lot of bother, especially since since my use of scandir is wrong! I'll give it another go, and should hopefully be able to move forward. Thanks again, you've helped a lot. – Finn Ashley Aug 15 '16 at 21:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .