aboutsummaryrefslogtreecommitdiff
path: root/libelf/elf_end.c
diff options
context:
space:
mode:
Diffstat (limited to 'libelf/elf_end.c')
-rw-r--r--libelf/elf_end.c32
1 files changed, 21 insertions, 11 deletions
diff --git a/libelf/elf_end.c b/libelf/elf_end.c
index 5c451f36..80f4d13f 100644
--- a/libelf/elf_end.c
+++ b/libelf/elf_end.c
@@ -1,5 +1,6 @@
/* Free resources associated with Elf descriptor.
Copyright (C) 1998,1999,2000,2001,2002,2004,2005,2007,2015,2016 Red Hat, Inc.
+ Copyright (C) 2023 Mark J. Wielaard <mark@klomp.org>
This file is part of elfutils.
Written by Ulrich Drepper <drepper@redhat.com>, 1998.
@@ -32,12 +33,22 @@
#endif
#include <assert.h>
+#include <search.h>
#include <stddef.h>
#include <stdlib.h>
#include "libelfP.h"
+static void
+free_chunk (void *n)
+{
+ Elf_Data_Chunk *rawchunk = (Elf_Data_Chunk *)n;
+ if (rawchunk->dummy_scn.flags & ELF_F_MALLOCED)
+ free (rawchunk->data.d.d_buf);
+ free (rawchunk);
+}
+
int
elf_end (Elf *elf)
{
@@ -71,7 +82,10 @@ elf_end (Elf *elf)
elf->state.ar.ar_sym = NULL;
if (elf->state.ar.children != NULL)
- return 0;
+ {
+ rwlock_unlock(elf->lock);
+ return 0;
+ }
}
/* Remove this structure from the children list. */
@@ -112,20 +126,13 @@ elf_end (Elf *elf)
case ELF_K_ELF:
{
- Elf_Data_Chunk *rawchunks
+ void *rawchunks
= (elf->class == ELFCLASS32
|| (offsetof (struct Elf, state.elf32.rawchunks)
== offsetof (struct Elf, state.elf64.rawchunks))
? elf->state.elf32.rawchunks
: elf->state.elf64.rawchunks);
- while (rawchunks != NULL)
- {
- Elf_Data_Chunk *next = rawchunks->next;
- if (rawchunks->dummy_scn.flags & ELF_F_MALLOCED)
- free (rawchunks->data.d.d_buf);
- free (rawchunks);
- rawchunks = next;
- }
+ tdestroy (rawchunks, free_chunk);
Elf_ScnList *list = (elf->class == ELFCLASS32
|| (offsetof (struct Elf, state.elf32.scns)
@@ -153,7 +160,10 @@ elf_end (Elf *elf)
rawdata_base. If it is already used it will be
freed below. */
if (scn->zdata_base != scn->rawdata_base)
- free (scn->zdata_base);
+ {
+ free (scn->zdata_base);
+ scn->zdata_base = NULL;
+ }
/* If the file has the same byte order and the
architecture doesn't require overly stringent