The if statement given for recover.c for some reason keeps returning false and I'm not sure exactly what I'm doing wrong here.

  • Do you mean if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)? Apr 10, 2017 at 23:50
  • yeah it's that part
    – NovNov
    Apr 11, 2017 at 0:44

1 Answer 1


One thing that's wrong: char filename[1];. How "big" is a file name? Bigger than 1. (and don't forget the null-byte, sprintf creates a valid string, ie null terminated). That's not causing this problem, but it's a memory leak/buffer overflow waiting to happen.

An other (bigger) thing is: back-to-back reads. Here fread(buffer, MEM_BLOCK, 1, input); then here while (fread(buffer, 1, MEM_BLOCK, input) == MEM_BLOCK);. It's probably always returning false because it never finds a jpg sig.

An other (even bigger) thing is: int buffer[MEM_BLOCK];. An int is length 4, so this is reading (and writing) 4 * MEM_BLOCK bytes. You need to use a byte type. Get a hint from bmp.h in resize exercise.

A quick way to properly format your code, whether on initial post or edit: select the code, then click the {} icon in the menu bar.

  • thanks for tips but I still don't understand why it's not finding any jpg sigs.
    – NovNov
    Apr 11, 2017 at 13:49
  • to get rid of back-to-back reads (which i assume is an issue because the file pointer moves one block forward), i removed the while condition and just used the number of files i it to test; it still doesn't find anything.
    – NovNov
    Apr 11, 2017 at 13:55
  • Don't use the number of files to test. Spoiler alert: check50 does not use the same raw file as in the distro code. Answer edited with other findings. Apr 11, 2017 at 16:07
  • Thank you!! using the correct size with BYTE did it.
    – NovNov
    Apr 11, 2017 at 16:53

You must log in to answer this question.

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