Possible Memory leak in bed hash.c

jacob navia jacob@jacob.remcomp.fr
Tue Sep 12 12:05:29 GMT 2023


Function: bfd_elf_strtab_init, file hash.c lines 94-126

Type of bug: Memory leak
-------------------

Description:
-------------

/* Create a new hash table.  */
struct elf_strtab_hash *_bfd_elf_strtab_init(void)
{
    struct elf_strtab_hash *table;
    size_t      amt = sizeof(struct elf_strtab_hash);

    table = (struct elf_strtab_hash *)malloc(amt);
    if (table == NULL) 
        return NULL; 
    // This call allocates several fields in the table.
    if (!bfd_hash_table_init(&table->table,elf_strtab_hash_newfunc,
                 sizeof(struct elf_strtab_hash_entry))) {
        free(table);
        return NULL; 
    }
    table->sec_size = 0;
    table->size = 1;
    table->alloced = 64; 
    amt = sizeof(struct elf_strtab_hasn_entry *);
    table->array = ((struct elf_strtab_hash_entry **)
            malloc(table->alloced * amt));
    if (table->array == NULL) {
        free(table);          <<<<<<<<<<<<<<<< MEMORY LEAK                                                                                             
        return NULL; 
    }
    table->array[0] = NULL; 

    return table;
}

We call « bfd_hash_table_init" that initializes the table with several huge structures. It returns OK, and we go on with table->sec_size = 0; etc.

Then, we attempt to allocate the array.

If it fails, we free just the table, leaking all previously allocated subfields.

HOW TO FIX:
—————

Just call « bfd_hash_table_free » instead of « free » 

Priority: LOW
In these times of plenty (gigabytes of RAM, etc) nobody cares about writing good software. 


More information about the Binutils mailing list