]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libelf: tdelete dummy key if anything goes wrong setting up rawchunk
authorMark Wielaard <mark@klomp.org>
Thu, 7 Sep 2023 14:14:43 +0000 (16:14 +0200)
committerMark Wielaard <mark@klomp.org>
Mon, 11 Sep 2023 07:39:39 +0000 (09:39 +0200)
elf_getdata_rawchunk uses a binary search tree cache. If a rawchunk is
not yet in the cache we setup a new entry. But if anything went wrong
setting up the new rawchunk we would leave a NULL key in the
cache. This could blow up the next search. Fix this by removing the
(dummy) key from the cache on any failure.

* libelf/elf_getdata_rawchunk.c (elf_getdata_rawchunk): Don't
assign NULL to *found. Call tdelete if anything goes wrong.

Signed-off-by: Mark Wielaard <mark@klomp.org>
libelf/elf_getdata_rawchunk.c

index cfd4039639c9fc9410f4c6c1d3d2322feb1ccb9d..05ff329c75c909b7190a4de176a0651ae046eb7b 100644 (file)
@@ -107,8 +107,10 @@ elf_getdata_rawchunk (Elf *elf, int64_t offset, size_t size, Elf_Type type)
       goto out;
     }
 
-  /* New entry.  */
-  *found = NULL;
+  /* New entry.  Note that *found will point to the newly inserted
+     (dummy) key.  We'll replace it with a real rawchunk when that is
+     setup.  Make sure to tdelete the dummy key if anything goes
+     wrong.  */
 
   size_t align = __libelf_type_align (elf->class, type);
   if (elf->map_address != NULL)
@@ -134,6 +136,7 @@ elf_getdata_rawchunk (Elf *elf, int64_t offset, size_t size, Elf_Type type)
       if (rawchunk == NULL)
        {
        nomem:
+         tdelete (&key, &elf->state.elf.rawchunks, &chunk_compare);
          __libelf_seterrno (ELF_E_NOMEM);
          goto out;
        }
@@ -144,6 +147,7 @@ elf_getdata_rawchunk (Elf *elf, int64_t offset, size_t size, Elf_Type type)
                    != size))
        {
          /* Something went wrong.  */
+         tdelete (&key, &elf->state.elf.rawchunks, &chunk_compare);
          free (rawchunk);
          __libelf_seterrno (ELF_E_READ_ERROR);
          goto out;