public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: jacob navia <jacob@jacob.remcomp.fr>
To: binutils@sourceware.org
Subject: Possible Memory leak in bed hash.c
Date: Tue, 12 Sep 2023 14:05:29 +0200	[thread overview]
Message-ID: <86B413C7-E812-411A-A790-90B80E81B411@jacob.remcomp.fr> (raw)

[-- 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. 

             reply	other threads:[~2023-09-12 12:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-12 12:05 jacob navia [this message]
2023-09-13  6:37 ` Alan Modra
2023-09-13 10:39   ` Nick Clifton
2023-09-13 10:47 ` Nick Clifton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86B413C7-E812-411A-A790-90B80E81B411@jacob.remcomp.fr \
    --to=jacob@jacob.remcomp.fr \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).