Skip to contentSkip to conversation

hw/mips/boston.c: Fix memory leak in boston_fdt_filter() error-handling paths

PATCH Applied Peter Maydell 2 participants 3 messages Nov 6, 2020 – Nov 8, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/mips/boston.c: Fix memory leak in boston_fdt_filter() error-handling paths

Coverity points out that the error-handling paths in the boston_fdt_filter() function don't free the fdt that was allocated. Fix the leak by using g_autofree.

Fixes Coverity CID 1432275
---
 hw/mips/boston.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
hw/mips/boston.c +4 -6
index 3356d7a6814..3d40867dc4c 100644
@@ -349,11 +349,9 @@ static const void *boston_fdt_filter(void *opaque, const void *fdt_orig,
     MachineState *machine = s->mach;
     const char *cmdline;
     int err;
-    void *fdt;
-    size_t fdt_sz, ram_low_sz, ram_high_sz;
-
-    fdt_sz = fdt_totalsize(fdt_orig) * 2;
-    fdt = g_malloc0(fdt_sz);
+    size_t ram_low_sz, ram_high_sz;
+    size_t fdt_sz = fdt_totalsize(fdt_orig) * 2;
+    g_autofree void *fdt = g_malloc0(fdt_sz);
 
     err = fdt_open_into(fdt_orig, fdt, fdt_sz);
     if (err) {
@@ -380,7 +378,7 @@ static const void *boston_fdt_filter(void *opaque, const void *fdt_orig,
 
     s->fdt_base = *load_addr;
 
-    return fdt;
+    return g_steal_pointer(&fdt);
 }
 
 static const void *boston_kernel_filter(void *opaque, const void *kernel,
Signature · 1 line
2.20.1
#2 lore raw
In reply to Peter Maydell
Quoted 10 lines · Peter Maydell

Coverity points out that the error-handling paths in the boston_fdt_filter() function don't free the fdt that was allocated. Fix the leak by using g_autofree.

Fixes Coverity CID 1432275
---
 hw/mips/boston.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

Thanks, applied to mips-fixes.