With count zero, newphdr removes any existing phdr table. If the
previous table was malloced it gets freed. Make sure to also clear the
malloced flag so it doesn't get freed again. This is mostly
theoretical since the code also sets the phdr field to NULL. And free
(NULL) should be fine. But better to keep the flags correct.

        * libelf/elf32_newphdr.c (elf32_newphdr): Clear MALLOCED flag
        after freeing phdr.

Signed-off-by: Mark Wielaard <[email protected]>
---
 libelf/elf32_newphdr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/libelf/elf32_newphdr.c b/libelf/elf32_newphdr.c
index 7dd78ca9c43a..86b93eb441fb 100644
--- a/libelf/elf32_newphdr.c
+++ b/libelf/elf32_newphdr.c
@@ -90,7 +90,10 @@ elfw2(LIBELFBITS,newphdr) (Elf *elf, size_t count)
       if (elf->state.ELFW(elf,LIBELFBITS).phdr != NULL)
        {
          if (elf->state.ELFW(elf,LIBELFBITS).phdr_flags & ELF_F_MALLOCED)
-           free (elf->state.ELFW(elf,LIBELFBITS).phdr);
+           {
+             free (elf->state.ELFW(elf,LIBELFBITS).phdr);
+             elf->state.ELFW(elf,LIBELFBITS).phdr_flags &= ~ELF_F_MALLOCED;
+           }
 
          /* Set the pointer to NULL.  */
          elf->state.ELFW(elf,LIBELFBITS).phdr = NULL;
-- 
2.56.1

Reply via email to