I've tried for about five days to finish the load function but now that I have two compact versions which don't work I don't have any idea what i am doing wrong.

Code for the first load function:

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

if(file == NULL){
    return false;

int b;
*content = NULL; 
BYTE* buffer = "";
size_t fileSize = 0;

for(b = fgetc(file); b != EOF; b = fgetc(file)){

    buffer = addByte(buffer, b);
    *content = realloc(*content, fileSize);    

strcpy(*content, buffer);
*length = fileSize;

return true;

... and the addByte function...

BYTE* addByte(BYTE* str, BYTE b){

BYTE* bP = malloc(sizeof( b + 1) * sizeof(bP) );

bP[0] = b;
bP[1] = '\0';

BYTE* res = malloc((strlen(str) + strlen(bP)) * sizeof(res));

strcpy(res, str);
strcat(res, bP);

return res;


The other load function:

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

*content = NULL;

size_t fileSize = 0;

fseek(file, 0, SEEK_END);
fileSize = ftell(file);
fseek(file, -fileSize, SEEK_CUR);

*content = malloc(fileSize * sizeof(BYTE) * sizeof(BYTE**) * sizeof(*content));
fread(*content, sizeof(BYTE), fileSize, file);

length = &fileSize;

return true;

I don't see any error in the functions. in the first function the cat.html file loads but the image cat.jpg won't. The alt text of the tag is displayed instead. cat.jpg doesn't load at all and favicon.ico doesn't load also(by that I mean the seperate files of the directory). With the second load function cat.jpg in cat.html won't show and the alt text appears again and when I reload the page a random sized wall of random characters appears. favicon.ico appears to be running but in hello.php a segmentation fault occurrs.
Thanks in advance :)


I've tried to make the load function smaller without using my own addByte() function. Instead I used the BYTE* buffer variable as an array and added each b from the for-loop of my first load function for(int b = fgetc(file); b != EOF; b = fgetc(file))etc. on the i'th place of buffer. Here is my current code:

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

    int i = 0;
    size_t fileSize = 0;
    *content = NULL;
    BYTE *buffer = NULL;

    for(int b = fgetc(file); b != EOF; b = fgetc(file)){


        buffer = realloc(buffer, fileSize);

        buffer[i] = b;



    *length = fileSize;
    *content = buffer;

    return true;

The only thing I found out in the last days was that feof(file)'s integer value is 0 and the integer value of EOF is -1. Due to the fact that no functions like stcpy() or memcpy() are used I think that there is something basic I don't understand. Maybe it is simple pointer arithmetic? Maybe I don't understand realloc()? Maybe it is something with the null-terminator \0 or I don't understand a concept fully yet. I'm starting to freak out honestly.

check50 failures

enter image description here

And when I click on cat.html this happens...

1 Answer 1


"other load" first. From the spec:

Odds are you’re unfamiliar with popen. That function opens a "pipe" to a process (php-cgi in our case), which provides us with a FILE pointer via which we can read that process’s standard output (as though it were an actual file).

You cannot use fseek or ftell, or any of the other file-positioning functions, on a pipe. This post explains it far better than I can. There are innumerable articles on the internet that describe pipes, here's one such. This method will cause check50/server2 failures for php tests, so perhaps best to abandon that approach.

A problem common to both functions is sizeof. EG sizeof(bP). bP is a pointer, sizeof(a pointer) is usually 8 on 64bit architecture.

Think about a black pixel from pset4. It would be represented as 3 zero bytes. The null terminator of a string is a zero byte ('\0' notation is just for the humans, computer sees 0). So here strcpy(*content, buffer); and here (strlen(str) and other places, the computer will be "fooled"*. cat.html is text (character) data, so does not contain a 0 byte, so seems to load ok.

*I interpret this line The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0') from man strcpy this way: The strcpy() function copies the string pointed to by src, **up to and ** including the terminating null byte ('\0'). By "fooled" I mean that strcpy "stops" when it encounters a null byte. Perhaps you'll find the function memcpy of use.

IMO the addByte function is adding a level of complexity that will make success elusive. The first load function is on the right track. You know how big you want *content to be. You know you want to add the next byte to the "new end" of *content. Work with that.

debug50 and/or gdb can be valuable tools for troubleshooting server. You'll find some starter tips for using gdb to debug load at the end of this answer.

Additional comments re check50 results

The first two errors look like you are not conforming to this (or any capitalization thereof), from the spec of lookup. The last two indicate a bug in indexes. That could include a memory leak which could explain the browser result of "broken image" when calling cat.html.

  • Thank you for your reply. So one question would be if the for-loop with the fgetc() would be wrong from the get go because you said that the file functions wouldn't work for a pipe? I understand that the bytes of an image of 0 could be misunderstood as a null terminator right? But what do you mean by "fooled" when you talk about 'strcpy(*content, buffer)' and 'strlen(str)' ? Do you want to say that those functions would be wrong here? Last one, how do I get the real size of the image? Do I have it already due to the fact that the html file and the image(while with errors) loads?
    – C4zZ
    Commented Dec 26, 2016 at 21:34
  • fgetc works with pipes, additional info/clarification edited into answer. additional explanation of "fooled" edited into answer. You get the real size of an image by counting the number of bytes, so yes, it looks like "you already have it". Commented Dec 27, 2016 at 14:08
  • Updated my question. Looked over the function for a little bit ... for cat.html file the size is correct but for cat.jpg, after the for loop from the first load function I don't even reach 1000. Something is wrong here because cat.jpg itself is 26860 bytes long.
    – C4zZ
    Commented Dec 29, 2016 at 23:19
  • This load function looks like it will work and it worked in a repro. What check50 failures are you getting? Commented Dec 30, 2016 at 12:57
  • Added an image of the check50 failures.
    – C4zZ
    Commented Dec 30, 2016 at 16:15

You must log in to answer this question.

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