Skip to contentSkip to conversation

elf-loader: Avoid calling qsort(NULL, 0, ...) call

PATCH In review Philippe Mathieu-Daudé 2 participants 3 messages Apr 21, 2018 – May 9, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 elf-loader: Avoid calling qsort(NULL, 0, ...) call

This fixes the following ASan warning:

  $ qemu-system-xtensa -M kc705 -m 128M -semihosting -nographic -monitor null -kernel Image.elf
  include/hw/elf_ops.h:179:5: runtime error: null pointer passed as argument 1, which is declared to never be null
Reported-by AddressSanitizer
---
 include/hw/elf_ops.h | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)
include/hw/elf_ops.h +22 -17
index b6e19e35d0..f0ac7c6c4e 100644
@@ -110,7 +110,7 @@ static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab,
     struct elf_shdr *symtab, *strtab, *shdr_table = NULL;
     struct elf_sym *syms = NULL;
     struct syminfo *s;
-    int nsyms, i;
+    int nsyms, i, ret = -1;
     char *str = NULL;
 
     shdr_table = load_at(fd, ehdr->e_shoff,
@@ -143,6 +143,7 @@ static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab,
     if (!str) {
         goto fail;
     }
+    ret = 0;
 
     i = 0;
     while (i < nsyms) {
@@ -170,30 +171,34 @@ static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab,
         }
         i++;
     }
-    syms = g_realloc(syms, nsyms * sizeof(*syms));
+    if (nsyms) {
+        syms = g_realloc(syms, nsyms * sizeof(*syms));
 
-    qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ));
-    for (i = 0; i < nsyms - 1; i++) {
-        if (syms[i].st_size == 0) {
-            syms[i].st_size = syms[i + 1].st_value - syms[i].st_value;
+        qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ));
+        for (i = 0; i < nsyms - 1; i++) {
+            if (syms[i].st_size == 0) {
+                syms[i].st_size = syms[i + 1].st_value - syms[i].st_value;
+            }
         }
+
+        /* Commit */
+        s = g_malloc0(sizeof(*s));
+        s->lookup_symbol = glue(lookup_symbol, SZ);
+        glue(s->disas_symtab.elf, SZ) = syms;
+        s->disas_num_syms = nsyms;
+        s->disas_strtab = str;
+        s->next = syminfos;
+        syminfos = s;
+
+        goto out;
     }
 
-    /* Commit */
-    s = g_malloc0(sizeof(*s));
-    s->lookup_symbol = glue(lookup_symbol, SZ);
-    glue(s->disas_symtab.elf, SZ) = syms;
-    s->disas_num_syms = nsyms;
-    s->disas_strtab = str;
-    s->next = syminfos;
-    syminfos = s;
-    g_free(shdr_table);
-    return 0;
  fail:
     g_free(syms);
     g_free(str);
+ out:
     g_free(shdr_table);
-    return -1;
+    return ret;
 }
 
 static int glue(elf_reloc, SZ)(struct elfhdr *ehdr, int fd, int must_swab,
Signature · 1 line
2.17.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Philippe Mathieu-Daudé

This fixes the following ASan warning:

  $ qemu-system-xtensa -M kc705 -m 128M -semihosting -nographic -monitor null -kernel Image.elf
  include/hw/elf_ops.h:179:5: runtime error: null pointer passed as argument 1, which is declared to never be null
Reported-by AddressSanitizer
---
 include/hw/elf_ops.h | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)

r~

#2 lore raw

Hi Paolo,

Quoted 6 lines · Richard Henderson
Quoted 5 lines · Philippe Mathieu-Daudé

This fixes the following ASan warning:

  $ qemu-system-xtensa -M kc705 -m 128M -semihosting -nographic -monitor null -kernel Image.elf
  include/hw/elf_ops.h:179:5: runtime error: null pointer passed as argument 1, which is declared to never be null
Quoted 7 lines
Quoted 5 lines
Reported-by AddressSanitizer
---
 include/hw/elf_ops.h | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)

Since this patch isn't Xtensa specific, can it goes via your MISC tree?

Thanks,

Phil.