0

PSET6 is a huge challenge. I have juste passed the first checked and I am trying to understand why my load function is not 100% correct.

This is the feedback of the second check :

~/workspace/pset6/ $ 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 // HERE ! :) 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 favicon.ico returns 200, image/x-icon, and correct file :) Requesting test.css returns 200, text/css, and correct file :) Requesting test.js returns 200, text/javascript, and correct file :) 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 :) Requesting /test redirects to /test/ :( Requesting /test/ outputs /test/index.html :( Requesting directory containing index.php outputs index.php \ expected output, but not "HTTP/1.1 200 OK\r\nContent-Type: text/h..." :( Requesting two files in a row (cat.html then cat.jpg) succeeds https://sandbox.cs50.net/checks/29f2dd8998ee4890affefbb924170391

This is the code of load. Please notice that I did not code indexes.

bool load(FILE* file, BYTE** content, size_t* length)
{

BYTE buffer[BYTES];
size_t size = 0;
*content = malloc(BYTES);

int ret = fread(buffer, 1, BYTES, file);
size = size+ret;

while(ret > 0) {
    memcpy(*content, buffer, ret);
    ret = fread(buffer, 1, BYTES, file);
    size = size+ret;
    *content = realloc(*content, size+1);
}

length = size;

if (*content == NULL) {
    return false; }

return true;

}

Is it something with the pointers? My lookup function works fine as well.

1 Answer 1

1

The primary point of failure is here memcpy(*content, buffer, ret);. You are always copying buffer into the first byte of content. Since most of the test files are less than 512 bytes.....

This length = size; looks like a problem too, since it's changing the pointer length to size, not the contents of the pointer.

There may be other problems as well, debug50 or gdb should help you track them down and sort them out.

3
  • I am gonna try that. Thank you ! Dec 29, 2016 at 9:51
  • I still have a problem and I cannot solve it. I think that my code design is not bad but I cannot find the way to change the memcpy() function so that it will not always copy the content into the first byte. I tried something like this : memcpy(*content + size, buffer, ret); but I had more error on my Check 50 : sandbox.cs50.net/checks/eefcf90b5a87458eb4c974270ff86f00 Dec 30, 2016 at 14:43
  • That is definitely on the right track. Without seeing the new code, and just imaging you changed the memcpy, that would be a problem on the first iteration, (assuming a file > 512), since size is already 512 from here size = size+ret;, but *content is allocated for only 512. So buffer is copied into byte 513. Frankly, I think you should get rid of the first fread, and put it in the while clause, something like while((ret = fread...) > 0)). That would make it much easier to keep your counters and your allocations in sync. Dec 30, 2016 at 17:38

You must log in to answer this question.

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