Check the phdr malloced flag before calling realloc. If the phdr was
mmapped in then use malloc. The code already zerod all entries and set
the phdr malloced flag unconditionally.

        * libelf/elf32_newphdr.c (newphdr): Check phdr malloced flag
        before calling realloc or malloc.

Signed-off-by: Mark Wielaard <[email protected]>
---
 libelf/elf32_newphdr.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/libelf/elf32_newphdr.c b/libelf/elf32_newphdr.c
index 86b93eb441fb..4b6a17422b6d 100644
--- a/libelf/elf32_newphdr.c
+++ b/libelf/elf32_newphdr.c
@@ -137,9 +137,14 @@ elfw2(LIBELFBITS,newphdr) (Elf *elf, size_t count)
 
       /* Allocate a new program header with the appropriate number of
         elements.  */
-      result = (ElfW2(LIBELFBITS,Phdr) *)
-       realloc (elf->state.ELFW(elf,LIBELFBITS).phdr,
-                count * sizeof (ElfW2(LIBELFBITS,Phdr)));
+      if (elf->state.ELFW(elf,LIBELFBITS).phdr_flags & ELF_F_MALLOCED)
+       result = (ElfW2(LIBELFBITS,Phdr) *)
+         realloc (elf->state.ELFW(elf,LIBELFBITS).phdr,
+                  count * sizeof (ElfW2(LIBELFBITS,Phdr)));
+      else
+       result = (ElfW2(LIBELFBITS,Phdr) *)
+         malloc (count * sizeof (ElfW2(LIBELFBITS,Phdr)));
+
       if (result == NULL)
        __libelf_seterrno (ELF_E_NOMEM);
       else
-- 
2.56.1

Reply via email to