public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Possible Memory leak in bed hash.c
@ 2023-09-12 12:05 jacob navia
  2023-09-13  6:37 ` Alan Modra
  2023-09-13 10:47 ` Nick Clifton
  0 siblings, 2 replies; 4+ messages in thread
From: jacob navia @ 2023-09-12 12:05 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 1632 bytes --]

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. 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-09-13 10:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-12 12:05 Possible Memory leak in bed hash.c jacob navia
2023-09-13  6:37 ` Alan Modra
2023-09-13 10:39   ` Nick Clifton
2023-09-13 10:47 ` Nick Clifton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).