Skip to contentSkip to conversation

hw/ppc/pegasos: Fix memory leak

PATCH Applied BALATON Zoltan 3 participants 7 messages Nov 1, 2025 – Nov 9, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/ppc/pegasos: Fix memory leak

Commit 9099b430a4 introduced an early return that caused a leak of a GString. Allocate it later to avoid the leak.

Fixes 9099b430a4 (hw/ppc/pegasos2: Change device tree generation)

Resolves: Coverity CID 1642027

---
 hw/ppc/pegasos.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/ppc/pegasos.c +2 -1
index 3a498edd16..8ce185de3e 100644
@@ -847,7 +847,7 @@ static struct {
 static void add_pci_device(PCIBus *bus, PCIDevice *d, void *opaque)
 {
     FDTInfo *fi = opaque;
-    GString *node = g_string_new(NULL);
+    GString *node;
     uint32_t cells[(PCI_NUM_REGIONS + 1) * 5];
     int i, j;
     const char *name = NULL;
@@ -871,6 +871,7 @@ static void add_pci_device(PCIBus *bus, PCIDevice *d, void *opaque)
             break;
         }
     }
+    node = g_string_new(NULL);
     g_string_printf(node, "%s/%s@%x", fi->path, (name ?: pn),
                     PCI_SLOT(d->devfn));
     if (PCI_FUNC(d->devfn)) {
Signature · 1 line
2.41.3
Reviewed-by on v1 1/1
#2 lore raw
In reply to BALATON Zoltan

Hi Balaton,

Thanks for taking care of this ...

Quoted 3 lines · BALATON Zoltan

Commit 9099b430a4 introduced an early return that caused a leak of a GString. Allocate it later to avoid the leak.

I think we also want to mention:

Quoted 12 lines
Fixes 9099b430a4 (hw/ppc/pegasos2: Change device tree generation)

Resolves: Coverity CID 1642027

---
  hw/ppc/pegasos.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
hw/ppc/pegasos.c +0 -0
index 3a498edd16..8ce185de3e 100644

hw/ppc/pegasos.c static struct { · line 850

Quoted 5 lines
hw/ppc/pegasos.c +1 -1
@@ -847,7 +847,7 @@ static struct {
  static void add_pci_device(PCIBus *bus, PCIDevice *d, void *opaque)
  {
      FDTInfo *fi = opaque;
-    GString *node = g_string_new(NULL);
+    GString *node;

Curious to know if there were any technical reasons for not using g_autoptr which Peter initially suggested ?

Anyways, it fixes the leak, so

Quoted 11 lines
hw/ppc/pegasos.c +1 -0
      uint32_t cells[(PCI_NUM_REGIONS + 1) * 5];
      int i, j;
      const char *name = NULL;
@@ -871,6 +871,7 @@ static void add_pci_device(PCIBus *bus, PCIDevice *d, void *opaque)
              break;
          }
      }
+    node = g_string_new(NULL);
      g_string_printf(node, "%s/%s@%x", fi->path, (name ?: pn),
                      PCI_SLOT(d->devfn));
      if (PCI_FUNC(d->devfn)) {
#3 lore raw
Quoted 5 lines · Harsh Prateek Bora

Hi Balaton,

Thanks for taking care of this ...

Quoted 8 lines
Quoted 3 lines · BALATON Zoltan

Commit 9099b430a4 introduced an early return that caused a leak of a GString. Allocate it later to avoid the leak.

I think we also want to mention:

You can if you like, but it's not essential -- I just forward the info from the coverity reports.

-- PMM

#4 lore raw
Quoted 11 lines · Peter Maydell
Quoted 10 lines · Harsh Prateek Bora

Hi Balaton,

Thanks for taking care of this ...

Quoted 3 lines · BALATON Zoltan

Commit 9099b430a4 introduced an early return that caused a leak of a GString. Allocate it later to avoid the leak.

Quoted 6 lines
Quoted 3 lines

I think we also want to mention:

You can if you like, but it's not essential -- I just forward the info from the coverity reports.

Sure, thanks Peter.
I think now that I mentioned it, b4 am will pick it anyway.

regards,
Harsh

Quoted 2 lines

-- PMM

#5 lore raw
In reply to Harsh Prateek Bora
Quoted 4 lines · Harsh Prateek Bora

Hi Balaton,

Thanks for taking care of this ...

Quoted 8 lines
Quoted 3 lines · BALATON Zoltan

Commit 9099b430a4 introduced an early return that caused a leak of a GString. Allocate it later to avoid the leak.

I think we also want to mention:

You can add it on merge.

Quoted 7 lines
Quoted 7 lines
Fixes 9099b430a4 (hw/ppc/pegasos2: Change device tree generation)

Resolves: Coverity CID 1642027

---
  hw/ppc/pegasos.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 12 lines

hw/ppc/pegasos.c static struct { · line 850

Quoted 10 lines
hw/ppc/pegasos.c +1 -1
index 3a498edd16..8ce185de3e 100644
@@ -847,7 +847,7 @@ static struct {
  static void add_pci_device(PCIBus *bus, PCIDevice *d, void *opaque)
  {
      FDTInfo *fi = opaque;
-    GString *node = g_string_new(NULL);
+    GString *node;

Curious to know if there were any technical reasons for not using g_autoptr which Peter initially suggested ?

Just thought it's simpler and more straight forward this way and saves a bit of unnecessary complication. We don't even allocate the string now when we exit, with g_autoptr it might do some additional operations unnecessarily. As this function otherwise does not have multiple exit points just one free at the end works.

Quoted 3 lines

Anyways, it fixes the leak, so

Thanks,
BALATON Zoltan

Quoted 15 lines
Quoted 13 lines
hw/ppc/pegasos.c +1 -0
      uint32_t cells[(PCI_NUM_REGIONS + 1) * 5];
      int i, j;
      const char *name = NULL;
@@ -871,6 +871,7 @@ static void add_pci_device(PCIBus *bus, PCIDevice *d, 
 void *opaque)
              break;
          }
      }
+    node = g_string_new(NULL);
      g_string_printf(node, "%s/%s@%x", fi->path, (name ?: pn),
                      PCI_SLOT(d->devfn));
      if (PCI_FUNC(d->devfn)) {
#6 lore raw
Quoted 36 lines · BALATON Zoltan
Quoted 4 lines · Harsh Prateek Bora

Hi Balaton,

Thanks for taking care of this ...

Quoted 8 lines
Quoted 3 lines · BALATON Zoltan

Commit 9099b430a4 introduced an early return that caused a leak of a GString. Allocate it later to avoid the leak.

I think we also want to mention:

You can add it on merge.

Quoted 7 lines
Quoted 7 lines
Fixes 9099b430a4 (hw/ppc/pegasos2: Change device tree generation)

Resolves: Coverity CID 1642027

---
  hw/ppc/pegasos.c | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 12 lines

hw/ppc/pegasos.c static struct { · line 850

Quoted 10 lines
hw/ppc/pegasos.c +1 -1
index 3a498edd16..8ce185de3e 100644
@@ -847,7 +847,7 @@ static struct {
   static void add_pci_device(PCIBus *bus, PCIDevice *d, void *opaque)
   {
       FDTInfo *fi = opaque;
-    GString *node = g_string_new(NULL);
+    GString *node;

Curious to know if there were any technical reasons for not using g_autoptr which Peter initially suggested ?

Quoted 1 line

Just thought it's simpler and more straight forward this way and saves a bit of unnecessary complication. We don't even allocate the string now when we exit, with g_autoptr it might do some additional operations unnecessarily. As this function otherwise does not have multiple exit points just one free at the end works.

Queued, thanks.