From 4968a55da80c384d70d14dbbe41f06ef41df0753 Mon Sep 17 00:00:00 2001 From: Khairul Kasmiran Date: Fri, 29 Dec 2017 21:04:13 +0800 Subject: [PATCH] Fixed Elf_(r_bin_elf_get_osabi_name) --- libr/bin/format/elf/elf.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/libr/bin/format/elf/elf.c b/libr/bin/format/elf/elf.c index 91c049beb2..1b3a821093 100644 --- a/libr/bin/format/elf/elf.c +++ b/libr/bin/format/elf/elf.c @@ -2131,8 +2131,8 @@ static inline int needle(ELFOBJ *bin, const char *s) { // TODO: must return const char * all those strings must be const char os[LINUX] or so char* Elf_(r_bin_elf_get_osabi_name)(ELFOBJ *bin) { - int i; - int num = bin->ehdr.e_shnum; + size_t i; + size_t num = bin->ehdr.e_shnum; const char *section_name = NULL; switch (bin->ehdr.e_ident[EI_OSABI]) { case ELFOSABI_LINUX: return strdup("linux"); @@ -2141,17 +2141,19 @@ char* Elf_(r_bin_elf_get_osabi_name)(ELFOBJ *bin) { case ELFOSABI_HPUX: return strdup("hpux"); } - for (i = 0; i < num; i++) { - if (bin->shdr[i].sh_type == SHT_NOTE) { - section_name = &bin->shstrtab[bin->shdr[i].sh_name]; - if (!strcmp (section_name, ".note.openbsd.ident")) { - return strdup ("openbsd"); - } - if (!strcmp (section_name, ".note.minix.ident")) { - return strdup ("minix"); - } - if (!strcmp (section_name, ".note.netbsd.ident")) { - return strdup ("netbsd"); + if (bin->shdr && bin->shstrtab) { + for (i = 0; i < num; i++) { + if (bin->shdr[i].sh_type == SHT_NOTE && bin->shdr[i].sh_name < bin->shstrtab_size) { + section_name = &bin->shstrtab[bin->shdr[i].sh_name]; + if (!strcmp (section_name, ".note.openbsd.ident")) { + return strdup ("openbsd"); + } + if (!strcmp (section_name, ".note.minix.ident")) { + return strdup ("minix"); + } + if (!strcmp (section_name, ".note.netbsd.ident")) { + return strdup ("netbsd"); + } } } }