0

I keep having a segmentation fault when I try to run recover.c.

I now have only 1 error when I run Valgrind.

Can somebody take a look at my code and give me some guidance ?

here is my code :

[code hidden]

Here's my valgrind output:

==18860== Memcheck, a memory error detector
==18860== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==18860== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==18860== Command: ./recover
==18860== 
==18860== Conditional jump or move depends on uninitialised value(s)
==18860==    at 0x4008B4: main (recover.c:49)
==18860== 
�=18860== 
==18860== HEAP SUMMARY:
==18860==     in use at exit: 0 bytes in 0 blocks
==18860==   total heap usage: 51 allocs, 51 frees, 28,968 bytes allocated
==18860== 
==18860== All heap blocks were freed -- no leaks are possible
==18860== 
==18860== For counts of detected and suppressed errors, rerun with: -v
==18860== Use --track-origins=yes to see where uninitialised values come from
==18860== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

1 Answer 1

0

You are fwriting to an img that is not yet open if the first 4 bytes are not a jpg signature. Look at where you have your fwrite statement.

if (it's a signature)
    // do this
write to img

If the first bytes aren't a signature (which, in this raw file, they aren't), you are still trying to write to img which doesn't yet exist (hence the segfault trying to fwrite to 0x00.

3
  • should I move the fwrite inside the if statement, right after I open the file (img) ?
    – AziCode
    Commented Feb 27, 2016 at 0:57
  • well, maybe check if img is not null before you write?
    – curiouskiwi
    Commented Feb 27, 2016 at 1:03
  • I have updated the code and valgrind output
    – AziCode
    Commented Feb 27, 2016 at 1:14

You must log in to answer this question.

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