Click here to Skip to main content
15,850,103 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In my code I changed some lines.


I get a segmentation fault, and here's valgrind's output:

==3587== Invalid read of size 4
==3587==    at 0x5E27CD0: fwrite (iofwrite.c:41)
==3587==    by 0x42D989: main (recover.c:61)
==3587==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==3587== 
==3587== 
==3587== Process terminating with default action of signal 11 (SIGSEGV)
==3587==  Access not within mapped region at address 0x0
==3587==    at 0x5E27CD0: fwrite (iofwrite.c:41)
==3587==    by 0x42D989: main (recover.c:61)
==3587==  If you believe this happened as a result of a stack
==3587==  overflow in your program's main thread (unlikely but
==3587==  possible), you can try to increase the size of the
==3587==  main thread stack using the --main-stacksize= flag.
==3587==  The main thread stack size used in this run was 8388608.
==3587== 
==3587== HEAP SUMMARY:
==3587==     in use at exit: 568 bytes in 1 blocks
==3587==   total heap usage: 1 allocs, 0 frees, 568 bytes allocated
==3587== 
==3587== LEAK SUMMARY:
==3587==    definitely lost: 0 bytes in 0 blocks
==3587==    indirectly lost: 0 bytes in 0 blocks
==3587==      possibly lost: 0 bytes in 0 blocks
==3587==    still reachable: 568 bytes in 1 blocks
==3587==         suppressed: 0 bytes in 0 blocks
==3587== Reachable blocks (those to which a pointer was found) are not shown.
==3587== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==3587== 
==3587== For counts of detected and suppressed errors, rerun with: -v
==3587== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault


What I have tried:

int main()
{
    typedef uint8_t BYTEINBITS;

    int pictcount = 0;
    
    
    FILE* card = fopen("card.raw", "r");
    
    FILE* newjpg = NULL;
  
     while ( true )
    {
        BYTEINBITS buffer[512];

         
        if ( fread( buffer, 512 * sizeof(char), 1, card) != 1 )
        {
           return 3;
        
        }
 
         
        bool jpg = true;
        
        if ( !( ( buffer[0] == 255 ) && ( buffer[1] == 216 ) && ( buffer[2] == 255) && ( buffer[3] > 224 ) ) )         
        {
            jpg = false;
        }
 
         
        if (jpg == true)
        {
            
            pictcount++;
            char title[8];
            sprintf(title,"%03d.jpg", pictcount);
            
            newjpg  = fopen(title, "w");
        
        }
         
            fwrite(buffer, 512 * sizeof(char), 1, newjpg);
         
    }
    
     fclose(card);
     fclose(newjpg);
    
}
Posted
Updated 2-Jan-17 10:34am
v2

The code can fail under specific circumstances:

  • When opening the file card.raw fails (e.g. does not exist in the current working directory): Check if card is not NULL
  • When pictcount is greater than 999 (may not happen with the actual code): Make the title array larger
  • When pict.raw is not a JPEG file (fwrite is called with newjpg = NULL): Place the first fread call and the JPEG check before the loop
 
Share this answer
 
The error message tells you all.
==3587== Invalid read of size 4
==3587==    at 0x5E27CD0: fwrite (iofwrite.c:41)
==3587==    by 0x42D989: main (recover.c:61)
==3587==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

buffer is an array, and the way you use it in fwrite is wrong, the value used by fwrite is the contain of buffer instead of its address.
 
Share this answer
 
Comments
Member 12919791 2-Jan-17 15:55pm    
With &buffer it gives the same error.
Am I supposed to use malloc()?

Thanks a lot,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900