summaryrefslogtreecommitdiffstats
path: root/libdwfl/elf-from-memory.c
diff options
context:
space:
mode:
authorMark Wielaard <[email protected]>2015-10-08 00:16:03 +0200
committerMark Wielaard <[email protected]>2015-10-08 08:43:42 +0200
commit86ed7f7f53179d7a893329e6b9851dbb75aba405 (patch)
treeaf0c8b1d33c6472e47c4256626552756d6a32fc6 /libdwfl/elf-from-memory.c
parent7eff36d5daa6ebca5e6399638a7643af105ae5b0 (diff)
Allocate exact amount of bytes for phdrs and shdrs.
Signed-off-by: Mark Wielaard <[email protected]>
Diffstat (limited to 'libdwfl/elf-from-memory.c')
-rw-r--r--libdwfl/elf-from-memory.c16
1 files changed, 6 insertions, 10 deletions
diff --git a/libdwfl/elf-from-memory.c b/libdwfl/elf-from-memory.c
index 5be21bb0..dd42e954 100644
--- a/libdwfl/elf-from-memory.c
+++ b/libdwfl/elf-from-memory.c
@@ -38,10 +38,6 @@
#include <stdlib.h>
#include <string.h>
-#ifndef MAX
-# define MAX(a, b) ((a) > (b) ? (a) : (b))
-#endif
-
/* Reconstruct an ELF file by reading the segments out of remote memory
based on the ELF file header at EHDR_VMA and the ELF program headers it
points to. If not null, *LOADBASEP is filled in with the difference
@@ -195,17 +191,15 @@ elf_from_remote_memory (GElf_Addr ehdr_vma,
xlatefrom.d_buf = buffer;
}
- if (unlikely (phnum >
- SIZE_MAX / MAX (sizeof (Elf32_Phdr), sizeof (Elf64_Phdr))))
+ bool class32 = ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32;
+ size_t phdr_size = class32 ? sizeof (Elf32_Phdr) : sizeof (Elf64_Phdr);
+ if (unlikely (phnum > SIZE_MAX / phdr_size))
{
free (buffer);
goto no_memory;
}
- const size_t phdrsp_bytes =
- phnum * MAX (sizeof (Elf32_Phdr), sizeof (Elf64_Phdr));
+ const size_t phdrsp_bytes = phnum * phdr_size;
phdrsp = malloc (phdrsp_bytes);
- Elf32_Phdr (*p32)[phnum] = phdrsp;
- Elf64_Phdr (*p64)[phnum] = phdrsp;
if (unlikely (phdrsp == NULL))
{
free (buffer);
@@ -221,6 +215,8 @@ elf_from_remote_memory (GElf_Addr ehdr_vma,
GElf_Off segments_end_mem = 0;
GElf_Addr loadbase = ehdr_vma;
bool found_base = false;
+ Elf32_Phdr (*p32)[phnum] = phdrsp;
+ Elf64_Phdr (*p64)[phnum] = phdrsp;
switch (ehdr.e32.e_ident[EI_CLASS])
{
/* Sanity checks segments and calculates segment_end,