0

I can't figure out why I am failing 2 tests in check50.

Here is my code:

bool load(FILE* file, BYTE** content, size_t* length)
{
if (file == NULL) {
    return false;
}
int temp = 0;
*length = 0;
BYTE* temporaryByte = malloc(1);
while(fread(temporaryByte, 1, 1, file) == 1) {
    temp++;
}
*length = temp;
fseek(file, 0, SEEK_SET);
BYTE* memory = malloc(*length);
for (int i = 0; i < *length; i++) {
    fread(memory + i, 1, 1, file);
}
*content = memory;
return true;

}

 const char* lookup(const char* path)
 {
// TODO
const char* substring = strrchr(path, 46);
printf("substring ->%s<- is substring\n", substring);
if (strcasecmp((substring), ".css") == 0) {
    printf("css\n");
    return "text/css";
}
else if (strcasecmp((substring), ".html") == 0) {
    printf("html\n");
    return "text/html";
}
else if (strcasecmp((substring), ".gif") == 0) {
    printf("gif\n");
    return "image/gif";
}
else if (strcasecmp((substring), ".ico") == 0) {
    printf("ico\n");
    return "image/x-icon";
}
else if (strcasecmp((substring), ".jpg") == 0) {
    printf("jpg\n");
    return "image/jpeg";
}
else if (strcasecmp((substring), ".js") == 0) {
    printf("js\n");
    return "text/javascript";
}
else if (strcasecmp((substring), ".php") == 0) {
    printf("php\n");
    return "text/x-php";
}
else if (strcasecmp((substring), ".png") == 0) {
    printf("png\n");
    return "image/png";
}
else {
    printf("other\n");
    return NULL;

} }

   bool parse(const char* line, char* abs_path, char* query)
    {
     char* placeholder = "/";
      if (strncmp(line, "GET ", 4) != 0) {
    error(405);
    return false;
}
if (strncmp(line + 4, placeholder, 1) != 0) {
    error(501);
    return false;
}
const char* index = (line + 4);
while(strncmp(index, " ", 1) != 0) {
    if (strncmp(index, "\"", 1) == 0) {
        error(400);
        return false;
    }
    index++;
}
index++;
if (strncmp(index, "HTTP/1.1", 8) != 0) {
    error(505);
    return false;
}
int i = 0;
index = line + 4;
while(strncmp(index, " ", 1) != 0 && strncmp(index, "?", 1) != 0) {
    *(abs_path + i) = *(index);
    index++;
    i++;
}
i = 0;
if (strncmp(index, "?", 1) == 0) {
    index++;
    while(strncmp(index, "\r\n", 2) != 0) {
        *(query + i) = *(index);
        i++;
        index++;
    }
}
else {
    query = "";
}
return true;
  } 


  char* indexes(const char* path)
   {
char* memory = malloc(strlen(path) + 1);
sprintf(memory, "%s/index.html", path);
FILE* file = fopen(memory, "r");
if (file != NULL) {
    return memory;
}
sprintf(memory, "%s/index.php", path);
file = fopen(memory, "r");
if (file != NULL) {
    return memory;
}
sprintf(memory, "%sindex.html", path);
file = fopen(memory, "r");
if (file != NULL) {
    return memory;
}
sprintf(memory, "%sindex.php", path);
file = fopen(memory, "r");
if (file != NULL) {
    return memory;
}
printf("memory ->%s<- memory\n", memory);
return NULL;
  }`

Here are the errors:

check50 2015.fall.pset6.server2 server.c
 :) server.c exists
  :) server compiles
    :) Requesting cat.jpg returns 200, image/jpeg, and correct image
   :) Requesting cat.html returns 200, text/html, and correct file
   :) Requesting cat2.HTML returns 200, text/html, and correct file
 :) Requesting cat3.HtMl returns 200, text/html, and correct file
 :) Requesting cat.gif returns 200, image/gif, and correct file
 ....
  :( Requesting /test/ outputs /test/index.html
  :) Requesting directory containing index.php outputs index.php
   :( Requesting two files in a row (cat.html then cat.jpg) succeeds
  https://sandbox.cs50.net/checks/a7deaa4858c1480ebc8162b0c5e6f390

Thank-you very much for helping me!

-Jon

4
  • assuming the other ".php" tests passed, this failure :( Requesting /test/ outputs /test/index.html would be from indexes. This parse does not reflect any change to null-terminate abs_path as discussed in earlier post. That's a potential cause of "two files in a row" failure. Why don't you troubleshoot those two things, and post an answer when you find and fix the problems? – DinoCoderSaurus Aug 11 '16 at 20:35
  • How do you troubleshoot "two files in a row"? – Jon Aug 11 '16 at 20:53
  • i Just added indexes function – Jon Aug 11 '16 at 20:56
  • Do you mean to add NULL to the end of abs_path and query? – Jon Aug 11 '16 at 21:11
1

What's the thing that makes a string a string? Why, the terminating null-byte, of course! Rewatch a couple of minutes of Week 2, 2nd lecture starting around 9:00. That's where Rob talks about the null terminator ('\0'). abs_path and query both gotta end with one of those. NULL is a defined constant, not a char. added In other words, when you are done with this loop:

while(strncmp(index, " ", 1) != 0 && strncmp(index, "?", 1) != 0) {
    *(abs_path + i) = *(index);
    index++;
    i++;
}

you need to null terminate abs_path. It will be something like *(abs_path + i) = '\0';

indexes

Reread this from the spec/instructions:

Complete the implementation of indexes in such a way that the function, given a /path/to/a/directory, returns /path/to/a/directory/index.php if index.php actually exists therein, or /path/to/a/directory/index.html if index.html actually exists therein, or NULL. In the first of those cases, this function should dynamically allocate memory on the heap for the returned string.

It is very explicit about the order in which you should check for those files. You must test for index.php first, then index.html.

from new code post (indexes)

Your allocation of memory is causing the "Error in free". Let's say path is "/". This line char* memory = malloc(strlen(path) + 1); allocates 2 bytes to ``memory. On the next line sprintf(memory, "%s/index.php", path); you are appending "/index.php" bytes to memory. ie Adding 10 bytes. While it may look ok in the printfs, it is a buffer overrun. That memory belongs to someone else. Since you may "append" "/index.html" to memory, the proper allocation is strlen(path) + strlen("/index.html") + 1.

7
  • When I use access, I get this error: ` *** Error in ./server': free(): invalid next size (normal): 0x00000000013c7100 *** And i get the same errors in check50 – Jon Aug 11 '16 at 23:38
  • Does that mean to use ` sprintf(memory, "%s/index.php\0", path); `? – Jon Aug 11 '16 at 23:41
  • answer amended.... – DinoCoderSaurus Aug 12 '16 at 13:45
  • When I do all that you said. Check50 still gives me those two errors. It might be because of that error ` *** Error in ./server': free(): invalid next size (normal): 0x00000000013c7100 *** ` Here is my code : gist.github.com/fdaa01f42ebccab4318e9cfe74885d5e – Jon Aug 12 '16 at 18:49
  • answer amended -oo- – DinoCoderSaurus Aug 12 '16 at 19:48

You must log in to answer this question.

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