Skip to contentSkip to conversation

Fix memory leaks in ELF loader

PATCH In review Dirk Behme 2 participants 3 messages May 1, 2006 – May 2, 2006

lore
Share & tools
lore

Dirk Behme

author
#0 lore raw

PATCH 1/1 Fix memory leaks in ELF loader

Fix memory leaks in ELF loader.

Regards

Dirk

elf_ops.h renamed from elf_ops.h_orig +9 -6
@@ -148,7 +148,7 @@ int glue(load_elf, SZ)(int fd, int64_t v
     uint8_t *data = NULL;
 
     if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
-        goto fail;
+        goto fail1;
     if (must_swab) {
         glue(bswap_ehdr, SZ)(&ehdr);
     }
@@ -162,9 +162,9 @@ int glue(load_elf, SZ)(int fd, int64_t v
     lseek(fd, ehdr.e_phoff, SEEK_SET);
     phdr = qemu_mallocz(size);
     if (!phdr)
-        goto fail;
+        goto fail2;
     if (read(fd, phdr, size) != size)
-        goto fail;
+        goto fail2;
     if (must_swab) {
         for(i = 0; i < ehdr.e_phnum; i++) {
             ph = &phdr[i];
@@ -181,9 +181,9 @@ int glue(load_elf, SZ)(int fd, int64_t v
             data = qemu_mallocz(mem_size);
             if (ph->p_filesz > 0) {
                 if (lseek(fd, ph->p_offset, SEEK_SET) < 0)
-                    goto fail;
+                    goto fail3;
                 if (read(fd, data, ph->p_filesz) != ph->p_filesz)
-                    goto fail;
+                    goto fail3;
             }
             addr = ph->p_vaddr + virt_to_phys_addend;
 
@@ -195,10 +195,13 @@ int glue(load_elf, SZ)(int fd, int64_t v
             data = NULL;
         }
     }
+    qemu_free(phdr);
     return total_size;
- fail:
+ fail3:
     qemu_free(data);
+ fail2:
     qemu_free(phdr);
+ fail1:
     return -1;
 }
 
#1 lore raw

Why adding several labels ? qemu_free(NULL) is perfectly valid. I would just add the missing "qemu_free(phdr)" ...

Fabrice.

Quoted 70 lines · Dirk Behme

Fix memory leaks in ELF loader.

Regards

Dirk

------------------------------------------------------------------------

elf_ops.h renamed from elf_ops.h_orig +9 -7
@@ -148,7 +148,7 @@ int glue(load_elf, SZ)(int fd, int64_t v
     uint8_t *data = NULL;
 
     if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
-        goto fail;
+        goto fail1;
     if (must_swab) {
         glue(bswap_ehdr, SZ)(&ehdr);
     }
@@ -162,9 +162,9 @@ int glue(load_elf, SZ)(int fd, int64_t v
     lseek(fd, ehdr.e_phoff, SEEK_SET);
     phdr = qemu_mallocz(size);
     if (!phdr)
-        goto fail;
+        goto fail2;
     if (read(fd, phdr, size) != size)
-        goto fail;
+        goto fail2;
     if (must_swab) {
         for(i = 0; i < ehdr.e_phnum; i++) {
             ph = &phdr[i];
@@ -181,9 +181,9 @@ int glue(load_elf, SZ)(int fd, int64_t v
             data = qemu_mallocz(mem_size);
             if (ph->p_filesz > 0) {
                 if (lseek(fd, ph->p_offset, SEEK_SET) < 0)
-                    goto fail;
+                    goto fail3;
                 if (read(fd, data, ph->p_filesz) != ph->p_filesz)
-                    goto fail;
+                    goto fail3;
             }
             addr = ph->p_vaddr + virt_to_phys_addend;
 
@@ -195,10 +195,13 @@ int glue(load_elf, SZ)(int fd, int64_t v
             data = NULL;
         }
     }
+    qemu_free(phdr);
     return total_size;
- fail:
+ fail3:
     qemu_free(data);
+ fail2:
     qemu_free(phdr);
+ fail1:
     return -1;
 }
 
 
 
 
 
------------------------------------------------------------------------

_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel

author
#2 lore raw
Quoted 1 line · Fabrice Bellard

Why adding several labels ? qemu_free(NULL) is perfectly valid. I would just add the missing "qemu_free(phdr)" ...

Okay. Wasn't sure about this ;)

Do you want a patch for "qemu_free(phdr)"?

Dirk