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 this code, I want to write out 50 JPGs from a file.

(actual instructions: )[^]

This is the terminal output:
~/workspace/pset4/jpg/ $ make recover clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow recover.c -lcrypt -lcs50 -lm -o recover ~/workspace/pset4/jpg/ $ ./recover

---Segmentation fault---

With valgrind, I see that there are no leaks, but a line says that no memory allocations were made:

Invalid read of size 4 ==17227== at 0x5E268D4: fclose@@GLIBC_2.2.5 (iofclose.c:54) ==17227== by 0x42DB5F: main (recover.c:85)

==17227== Address 0x0 is not stack'd, malloc'd or (recently) free'd

==17227== 
==17227== 
==17227== Process terminating with default action of signal 11 (SIGSEGV) 
==17227== Access not within mapped region at address 0x0 
==17227== at 0x5E268D4: fclose@@GLIBC_2.2.5 (iofclose.c:54) 
==17227== by 0x42DB5F: main (recover.c:85) ==17227== If you believe this happened as a result of a stack 
==17227== overflow in your program's main thread (unlikely but 
==17227== possible), you can try to increase the size of the 
==17227== main thread stack using the --main-stacksize= flag. 
==17227== The main thread stack size used in this run was 8388608. 
==17227== 
==17227== HEAP SUMMARY: 
==17227== in use at exit: 0 bytes in 0 blocks 
==17227== total heap usage: 1 allocs, 1 frees, 568 bytes allocated =
=17227== 
==17227== All heap blocks were freed -- no leaks are possible 
==17227== 
==17227== For counts of detected and suppressed errors, rerun with: -v 
==17227== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
 Segmentation fault

Thanks a lot,


What I have tried:

/**
 * recover.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Recovers JPEGs from a forensic image.
 */
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <stdint.h>



int main()
{
    typedef uint8_t BYTEINBITS;

    int pictcount = 0;
    
    
    FILE* input = fopen("card.raw", "r");
 
     if ( input == NULL )
    {
        printf(" Could not open card.raw \n");
        return 2;
    }
    
    FILE* output = NULL;
    
    bool  atend = false;
    
    while ( atend == false)
    {
        BYTEINBITS buffer[512];

         
        fread( &buffer, 512 * sizeof(char), 1, input); 
 
         
        bool jpg = true;
        
        if ( !( ( buffer[0] == 255 ) && ( buffer[1] == 216 ) && ( buffer[2] == 255) && ( buffer[3] > 224 ) ) )         
        {
            jpg = false;
        }
 
         
        if (jpg == true)
        {
             
             if (pictcount >= 1)
             {
              
                  fclose(output);   
             }         
                 
            pictcount++;
            char title[8];
            sprintf(title,"%03d.jpg", pictcount);
            
            output  = fopen("title", "w");
        
        }
        
        if (pictcount > 0)
        {
         
            fwrite( &buffer, 512 * sizeof(char), 1, output );

        }
        
        if ( fread( &buffer, 512*sizeof(char), 1, input) != 1)
        {
            atend = true;   
        }
    }
     
    fclose(input);
    fclose(output);


    
    
}
Posted
Updated 1-Jan-17 21:57pm

1 solution

The error is here:
BYTEINBITS buffer[512];
fread( &buffer, 512 * sizeof(char), 1, input); 

buffer is an array and therefore already a pointer but you are passing the address of that pointer to fread (you are passing a pointer to a pointer instead of a pointer).

A working version should look like:
uint8_t buffer[512];
fread(buffer, sizeof(buffer), 1, input); 

Note that I have also removed the useless typedef for uint8_t (it makes the code less readable), the fixed buffer size of 512 and the sizeof(char) (which might not always the same as sizeof(uint8_t)). Just use sizeof(buffer) which expands to the array size in bytes.
 
Share this answer
 
Comments
Member 12919791 2-Jan-17 11:41am    
I understand the mistake above, but the same error comes even after I changed that line.
Jochen Arndt 2-Jan-17 15:01pm    
I have only shown the first line containing the error but it must be fixed for all fread and fwrite calls.
Member 12919791 2-Jan-17 15:03pm    
Yes, the changes are made to other freads

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