0

I am pretty sure that my issue lies in load, but I am stumped as to what the problem is. My check50 (server1) was all green so I believe that parse is okay. My check50 (server2) looks like this:

:) server.c exists

:) server compiles

:( Requesting cat.jpg returns 200, image/jpeg, and correct image \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

:( Requesting cat.html returns 200, text/html, and correct file \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

:( Requesting cat2.HTML returns 200, text/html, and correct file \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

:( Requesting cat3.HtMl returns 200, text/html, and correct file \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

:( Requesting cat.gif returns 200, image/gif, and correct file \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

:( Requesting favicon.ico returns 200, image/x-icon, and correct file \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

:( Requesting test.css returns 200, text/css, and correct file \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

:( Requesting test.js returns 200, text/javascript, and correct file \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

:) Requesting hello.php returns 200, text/html, and correct output

:) Requesting hello.php? returns 200, text/html, and correct output

:( Requesting hello.php?name=Alice returns 200, text/html, and correct output \ expected output, not standard error of "======= Backtrace: =========\n======= M..."

:) Requesting /test redirects to /test/ :( Requesting /test/ outputs /test/index.html \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

:) Requesting directory containing index.php outputs index.php

:( Requesting two files in a row (cat.html then cat.jpg) succeeds \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

This is my code for load:

    bool load(FILE* file, BYTE** content, size_t* length)
    {
        char* buffer = malloc(5000 * sizeof(char));
        int capacity = 5000;
        int size = 0;
        int ch;
        for (ch = fgetc(file); ch != EOF; ch = fgetc(file))
        {
            if (size == capacity)
            {
                if (capacity < INT_MAX)
                    capacity = capacity + 1000;
                else
                {
                    free(buffer);
                    return false;
                }

                buffer = realloc(buffer, capacity * sizeof(char));
                if (buffer == NULL)
                {
                return false;
                }
            }
            buffer[size] = ch;
            size++;
        }
        char* actual = malloc(sizeof(char) * size);
        strcpy(actual, buffer);
        free(buffer);
        *content = actual;
        *length = size;
        free(actual);
        return true;
    }

My code for indexes is as follows:

    char* indexes(const char* path)
    {
        int x = strlen(path);
        char* f_php = "index.php";
        char* f_html = "index.html";
        char* new_path = calloc(1, ((strlen(path) + strlen(f_php) + 1) * sizeof(char)));
        char* new_path2 = calloc(1, ((strlen(path) + strlen(f_html) + 1) *     sizeof(char)));
        strcpy(new_path, path);
        strcpy(new_path2, path);
        if (new_path[x - 1] != '/')
        {
            strcat(new_path, "/");
            strcat(new_path2, "/");
        }
        strcat(new_path, f_php);
        strcat(new_path2, f_html);
        if(access(new_path, F_OK) != -1)
        {    
            free(new_path2);
            return new_path;
        }
        else if (access(new_path2, F_OK) != -1)
        {
            free(new_path);
            return new_path2;
        }
        else
        {
            free(new_path);
            free(new_path2);
            return NULL;
        }
    }

Please help!! Thanks.

1

Backtrace is a seg fault. This will cause a segfault.

free(buffer);
*content = buffer;

It begs the question why do you need to declare actual at all?

| improve this answer | |
  • My mistake on not switching actual with buffer for setting *content. I made the change in the function, but the output for check50is the exact same. I have made the proper edits above. – mori91 Jun 16 '16 at 22:08
  • don't free(actual). Now *content points to......nothing. – DinoCoderSaurus Jun 16 '16 at 22:10
  • 1
    Thanks so much! I was thinking once I assigned the value I could free it for some reason. – mori91 Jun 16 '16 at 22:18

You must log in to answer this question.

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