Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled exception at an if statement in C code

I am using Visual Studio 2010 and in the following code snippet there occurs an exception at the if statement after fseek.

int load_filenew(char *filename, char **buffer)
{
    int size = 0;
    FILE *fp = 0;

    fp = fopen(filename, "rb");
    if (!fp)
    {
        printf(" fopen failed.\n");
        return 1;
    }

    fseek(fp, 0, SEEK_END);
    size = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    if (size)
    {
        if (*buffer)
        {
            free(*buffer);
        }
        *buffer = 0;
        *buffer = (char *)malloc(size + 1);
        if (!*buffer)
        {
            printf(" malloc failed.\n");
            fclose(fp);
            return 3;
        }
        memset(*buffer, 0, size + 1);
        fread(*buffer, size, 1, fp);
        (*buffer)[size] = '\0';
    }
    else
    {
        fclose(fp);
        return 2;
    }
    fclose(fp);

    return 0;
}

This function is called several times in the application but at some times unhandled exception is thrown at following line while loading a file

//exception code
if (size)
{
    if (*buffer)

Please help — what could be the possible cause and how to resolve it?

like image 800
priya Avatar asked May 23 '13 04:05

priya


2 Answers

not solving your problem directly but:

    if (*buffer)
    {
        free(*buffer);
    }
    *buffer = 0;
    *buffer = (char *)malloc(size + 1);

have you considered using realloc() instead?

    p = realloc(*buffer, size + 1);
    if ( p != NULL ) 
    { 
      *buffer = p; 
    }
like image 40
AndersK Avatar answered Oct 09 '22 13:10

AndersK


It seems like buffer is probably set to NULL or some other invalid pointer and probably segfaults when you dereference it. It could also be your first call to free if the pointer is invalid. Ideally you need to show us the code that calls this function.

Also keep in mind it's bad form to call a matching malloc and free in different functions. Unless that function has only one purpose, to allocate a new structure or to free an existing one (In other words allocation of any resource should be done in the same function as deallocation of that same resource. The only exception is a function that composes more complex allocations and deallocations).

int load_filenew(char *filename, char **buffer)
{
    int size = 0;
    FILE *fp = 0;

    if(buffer == NULL)
    {
        return 1;
    }

    fp = fopen(filename, "rb");
    if (!fp)
    {
        printf(" fopen failed.\n");
        return 2;
    }

    fseek(fp, 0, SEEK_END);
    size = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    if (size)
    {
        *buffer = (char *)malloc(size + 1);
        if (!*buffer)
        {
            printf(" malloc failed.\n");
            fclose(fp);
            return 3;
        }
        memset(*buffer, 0, size + 1);
        fread(*buffer, size, 1, fp);
        (*buffer)[size] = '\0';
    }
    else
    {
        fclose(fp);
        return 3;
    }
    fclose(fp);

    return 0;
}
like image 54
Eric des Courtis Avatar answered Oct 09 '22 13:10

Eric des Courtis