Not using memset causes files to load improperly, yet I feel it is unnecessary. I am using fread, reading 1 byte before reallocating to a += 1 size, earlier I loaded 2048 bytes at a time and doubled the size of cache after each read which had similar results to what I am currently doing.


Code removed per Academic Honesty

1 Answer 1


You cannot fgetc image (binary) data into a char (which BYTE is). It will give a "false eof" as described here and here.

I ran a "snippet repro" of load (not integrated in server), and hello.php returned 281, as expected.

It might be time to break out valgrind and make sure there are no memory leaks, the leading cause of unpredictable results. This valgrind -v --leak-check=full --show-leak-kinds=all --track-origins=yes call gives lots of feedback detail. And don't forget debug50 and/or gdb.

valgrind may also explain the two hello.php? failures, especially if they render fine with a browser call.

  • Right I had a small leak which is gone now, and valgrind isn't complaining about anything. My post has been reverted to earlier code (originally I used fread). I switched to fgetc because fread always returned 1, which gave me hard time tracking the size of file and probably also caused many more issues. On fread always returning 1, I did try to swap the size and nmemb arguments which did not seem to affect anything. Could you give me a hint to go from hereon? I have omitted malloc checks in the code above on purpose by the way.
    – kluvin
    Sep 24, 2016 at 15:38
  • Nope I misread what you wrote, I thought I absolutely couldn't use fgetc when you meant I couldn't cast to a char. Though I still wonder is it possible to use fread?
    – kluvin
    Sep 24, 2016 at 16:39
  • IMO fread makes a better load. From the doc, as you already know [the return value ] equals the number of bytes transferred only when size is 1. So something like fread(buffer, 1, 512, file) would do the trick. (more IMO, suggesting a constant nmemb size à la pset4/recover). Sep 24, 2016 at 17:59

You must log in to answer this question.

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