Home > Software design >  How to solve an Invalid write/read of size 8 involving a void pointer in C
How to solve an Invalid write/read of size 8 involving a void pointer in C

Time:03-02

I am currently working on a hash table for a project and I am having some trouble with the memory cleanup. I am using Valgrind and I am getting this error response.

    ==1409499== Invalid write of size 8
    ==1409499==    at 0x4014F9: symtabInstall (symtab.c:106)
    ==1409499==    by 0x4011D0: main (test1.c:17)
    ==1409499==  Address 0x4a47128 is 0 bytes after a block of size 8 alloc'd
    ==1409499==    at 0x484086F: malloc (vg_replace_malloc.c:381)
    ==1409499==    by 0x4014B0: symtabInstall (symtab.c:102)
    ==1409499==    by 0x4011D0: main (test1.c:17)
    
    
    int symtabInstall(void *symtabHandle, const char *symbol, void *data){
        // Install a (symbol, data) pair in the table.
        // If the symbol is already installed in the table, then the data is
        //   overwritten.
        // If the symbol is not already installed, then space is allocated and
        //   a copy is made of the symbol, and the (symbol, data) pair is then
        //   installed in the table.
        // If successful, returns 1.
        // If memory cannot be allocated for a new symbol, then returns 0.
        // Note that no validation is made of the symbol table handle passed
        //   in. If not a valid handle, then the behavior is undefined (but
        //   probably bad).
        sym_t *symtable = symtabHandle;
        signed int location = search(symbol, symtable);
        if (location != -1)
            symtable->entries[location]->data = data;
        ///Create the input pair
        values *input = malloc(sizeof(input) * 1);
        if (input == NULL)
            return 0;
        input->symbol = malloc(strlen(symbol)   1);
        input->data = data; /// This is the Line -------------------
        strcpy(input->symbol, symbol);
        ///Find spot to put in
        int symh = hash(symbol, symtable);
        ///Input check
        if (symtable->entries[symh] == NULL){
            symtable->entries[symh] = input;
        } else {
            int i = symh   1;
            int c = 0;
            while(i < symtable->size){
                if (i == symtable->size && c == 0){
                    c = 1;
                } else if (c == 1){
                    return 0;
                }
                i %= symtable->size;
                if (symtable->entries[i] != NULL){
                    symtable->entries[symh] = input;
                    symtable->entries[symh]->data = data;
                }
                i  ;
            }
        }
        return 1;
    }

For context, input is one of the buckets for the hash table and has two pointers symbol and data. Data is the one giving me the issue as I need to allocate memory for it. Here are the structs for both.


    typedef struct values {
        char *symbol;
        void *data;
        struct values *next;
    } values;
    typedef struct{
        values **entries;
        int size;
    } sym_t;

I am also given no knowledge of the data type for data.

CodePudding user response:

It seems the problem is this memory allocation

values *input = malloc(sizeof(input) * 1);

I think you mean

values *input = malloc(sizeof( *input) * 1);

or

values *input = malloc(sizeof(values) * 1);

Pay attention to that using the multi[plier 1 does not make a great sense.:)

Also you forgot to initialize the data member next of the allocated object of the type values.

And the body of this while loop

 while(i < symtable->size){
     if (i == symtable->size && c == 0){
     // ...

also does not make a great sense because the condition in the following if statement

i == symtable->size

never can evaluate to true.

  • Related