0

i am really confused as to why i get segmentation fault with valgrind on this. It recovers all the photos and returns check50 with green smiley faces but there is something wrong with the code, would apreciate any info on segmentation fault issue. I read into this, but can't figure out what is wrong. Thank you so much for taking your time! Please dont focus on the names on the files, i will implement sprintf when my code is up and running :)

int main(int argc, char* argv[])
{
    if (argc > 1)
    {
        printf ("No argument necessary");
        return 1;
    }
    FILE* mem_card = fopen("card.raw","r");    
    if (mem_card == NULL)
    {
        return 2;
    }
    unsigned char buffer [512];
    FILE * output = NULL;
    char* title [16]= {"000.jpg","001.jpg", "002.jpg","003.jpg", "004.jpg","005.jpg", "006.jpg","007.jpg", "008.jpg","009.jpg", "010.jpg","011.jpg", "012.jpg","013.jpg", "014.jpg","015.jpg"};
    int no_jpg = 0;
    while(0 < 1)
    {

            if (feof(mem_card))
            {
                fclose(mem_card);
                return 0;
            }

            fread (&buffer, 512, 1, mem_card);

        if (buffer[0] == 0xff && (buffer[1] == 0xd8) && (buffer[2] == 0xff) && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
        {
            if (output == NULL) 
            {
                output = fopen(title[no_jpg], "w");
                if (output == NULL) 
                {
                    return 1;
                }
                no_jpg++;
                fwrite(&buffer, 512, 1, output);
            }
            else if (output != NULL)
            {
                fclose(output);
                fopen(title[no_jpg], "w");
                if (output == NULL) 
                {
                    return 1;
                }
                no_jpg++;
                fwrite(&buffer, 512, 1, output);
            }
        }
        else if (output != NULL)
        {
            fwrite(&buffer, 512, 1, output);
        } 
    }
    fclose(output);
    fclose(mem_card);
}
1

Interesting problem. I ran your code and read the fine print from the valgrind output.

==7731==  If you believe this happened as a result of a stack
==7731==  overflow in your program's main thread (unlikely but
==7731==  possible), you can try to increase the size of the
==7731==  main thread stack using the --main-stacksize= flag.
==7731==  The main thread stack size used in this run was 8388608.

I reran as follows: valgrind ./recover --main-stacksize=8388608 and it ran successfully with no leaks.

However, I did find a couple problems with your code that caused one failure under check50 - it doesn't handle the last file correctly. Observe the following:

while(0 < 1)
{
        if (feof(mem_card))
        {
            fclose(mem_card);
            return 0;
        }

        fread (&buffer, 512, 1, mem_card);

Your code will check for end of file and then read from the inoput file. It seems to be working, but should be reversed.

More importantly, when you do detect the last file, you close the input file and exit the program without closing the output file. You have two lines at the end of the program to close both files, but since you return 0, those lines will never be executed. Instead, if you simply break inside the if(feof() ) code block, those two lines will execute and the program will terminate normally.

My other problem with this code is that you have hardcoded the names of the program. While the assignment specified that it should recover 16 files, it could be argued that it should be able to handle any number of files up to 999, implied by the 3 digit file name. It's good practice anyways, but I'll leave it to you whether you wish to do so. ;-)

If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)

1
  • Saved by Cliff B yet again :) Thank you! – Mags Sep 17 '15 at 2:52

You must log in to answer this question.

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