Johnny Doey
Johnny Doey

Reputation: 93

C: Invalid read of size 1 & Address is 0 bytes after a block size 118 alloc'd

I'm in need of some help with my code throwing some errors in valgrind. The code works though... so I don't understand what the problem is, could you guys have a look for me? Thanks!

Using the compile line: gcc -Wall -pedantic -ansi -g

char* readfile(char *filename) {
    FILE *fp;
    int size = 0;
    char *buffer = NULL;

    fp = fopen(filename, "r");
    if(fp != NULL) {
        /* Grab the size of the file */
        fseek(fp, 0L, SEEK_END);
        size = ftell(fp);
        fseek(fp, 0L, SEEK_SET);

        /* Memory allocate char */
        buffer = (char*) malloc(size);
        if(buffer == NULL) {
            printf("ERROR: Memory allocation error!\n");
            exit(EXIT_FAILURE);
        }

        /* Read the file and close */
        fread(buffer, size, 1, fp);
        fclose(fp);
    } else {
        printf("Failed to open %s file\n", filename);
    }

    return buffer;
}

int main(int argc, char * argv[]) {
    struct ets ets;
    struct menu_item menu_items[NUM_MENU_ITEMS];
    char *filebuffer = 0;

    /*if(argc != 4) {
        perror("Inputs are not valid\n");
        return EXIT_FAILURE;
    }*/

    filebuffer = readfile(argv[1]);

    printf("Contents: %s\n", filebuffer);
    free(filebuffer);

    return 0;
}

administrator:Assignment 2 Administrator$ valgrind --leak-check=full ./main equip.dat
==3972== Memcheck, a memory error detector
==3972== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==3972== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==3972== Command: ./main equip.dat
==3972== 
==3972== Invalid read of size 1
==3972==    at 0x828A: strlen (in /usr/local/Cellar/valgrind/3.10.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==3972==    by 0x14BA8C: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==3972==    by 0x14A35E: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==3972==    by 0x14273F: printf (in /usr/lib/system/libsystem_c.dylib)
==3972==    by 0x100000BF1: main (ets_main.c:59)
==3972==  Address 0x10001c196 is 0 bytes after a block of size 118 alloc'd
==3972==    at 0x6B1B: malloc (in /usr/local/Cellar/valgrind/3.10.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==3972==    by 0x100000B19: readfile (ets_main.c:31)
==3972==    by 0x100000BDB: main (ets_main.c:57)
==3972== 

Upvotes: 2

Views: 4765

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53016

You don't null terminate the buffer, change malloc to

buffer = malloc(1 + size);

and then add this somewhere after checking that malloc succeeded

buffer[size] = '\0';

When passing the returned pointer to printf() it's reading 1 byte past the malloced block, hence the valgrind error.

Upvotes: 2

Related Questions