Skip to contentSkip to conversation

hw/arm/sbsa-ref: Don't leak string in sbsa_fdt_add_gic_node()

PATCH In review Peter Maydell 4 participants 5 messages Aug 22, 2024 – Aug 27, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/arm/sbsa-ref: Don't leak string in sbsa_fdt_add_gic_node()

In sbsa_fdt_add_gic_node() we g_strdup_printf() two nodename strings, but only free one.

Since the string is actually entirely constant and we don't make any use of printf's format-string operations, we can drop the g_strdup_printf() use entirely.

---
A small once-only leak, so this is 9.2 material. Spotted with clang leak-sanitizer.

 hw/arm/sbsa-ref.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
hw/arm/sbsa-ref.c +6 -9
index ae37a923015..5cd8cd705be 100644
@@ -164,23 +164,20 @@ static uint64_t sbsa_ref_cpu_mp_affinity(SBSAMachineState *sms, int idx)
 
 static void sbsa_fdt_add_gic_node(SBSAMachineState *sms)
 {
-    char *nodename;
+    const char *intc_nodename = "/intc";
+    const char *its_nodename = "/intc/its";
 
-    nodename = g_strdup_printf("/intc");
-    qemu_fdt_add_subnode(sms->fdt, nodename);
-    qemu_fdt_setprop_sized_cells(sms->fdt, nodename, "reg",
+    qemu_fdt_add_subnode(sms->fdt, intc_nodename);
+    qemu_fdt_setprop_sized_cells(sms->fdt, intc_nodename, "reg",
                                  2, sbsa_ref_memmap[SBSA_GIC_DIST].base,
                                  2, sbsa_ref_memmap[SBSA_GIC_DIST].size,
                                  2, sbsa_ref_memmap[SBSA_GIC_REDIST].base,
                                  2, sbsa_ref_memmap[SBSA_GIC_REDIST].size);
 
-    nodename = g_strdup_printf("/intc/its");
-    qemu_fdt_add_subnode(sms->fdt, nodename);
-    qemu_fdt_setprop_sized_cells(sms->fdt, nodename, "reg",
+    qemu_fdt_add_subnode(sms->fdt, its_nodename);
+    qemu_fdt_setprop_sized_cells(sms->fdt, its_nodename, "reg",
                                  2, sbsa_ref_memmap[SBSA_GIC_ITS].base,
                                  2, sbsa_ref_memmap[SBSA_GIC_ITS].size);
-
-    g_free(nodename);
 }
 
 /*
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Peter Maydell

In sbsa_fdt_add_gic_node() we g_strdup_printf() two nodename strings, but only free one.

Since the string is actually entirely constant and we don't make any use of printf's format-string operations, we can drop the g_strdup_printf() use entirely.

Quoted 7 lines

---
A small once-only leak, so this is 9.2 material. Spotted with clang leak-sanitizer.

  hw/arm/sbsa-ref.c | 15 ++++++---------
  1 file changed, 6 insertions(+), 9 deletions(-)
#2 lore raw
In reply to Peter Maydell
Quoted 20 lines · Peter Maydell

In sbsa_fdt_add_gic_node() we g_strdup_printf() two nodename strings, but only free one.

Since the string is actually entirely constant and we don't make any use of printf's format-string operations, we can drop the g_strdup_printf() use entirely.

---
A small once-only leak, so this is 9.2 material. Spotted with clang leak-sanitizer.

  hw/arm/sbsa-ref.c | 15 ++++++---------
  1 file changed, 6 insertions(+), 9 deletions(-)
hw/arm/sbsa-ref.c +0 -0
index ae37a923015..5cd8cd705be 100644

hw/arm/sbsa-ref.c static uint64_t sbsa_ref_cpu_mp_affinity(SBSAMachineState *sms, int idx) · line 168

Quoted 6 lines
hw/arm/sbsa-ref.c +2 -1
@@ -164,23 +164,20 @@ static uint64_t sbsa_ref_cpu_mp_affinity(SBSAMachineState *sms, int idx)
 
  static void sbsa_fdt_add_gic_node(SBSAMachineState *sms)
  {
-    char *nodename;
+    const char *intc_nodename = "/intc";
+    const char *its_nodename = "/intc/its";

Should we use static qualifiers?

#3 lore raw
Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 5 lines

  static void sbsa_fdt_add_gic_node(SBSAMachineState *sms) {
-    char *nodename;
+    const char *intc_nodename = "/intc";
+    const char *its_nodename = "/intc/its";

Should we use static qualifiers?'

No. The real object is the string literal. The local variable simply allows multiple references within the function.

r~

Reviewed-by on v1 1/1
#4 lore raw
In reply to Peter Maydell
Quoted 7 lines · Peter Maydell

In sbsa_fdt_add_gic_node() we g_strdup_printf() two nodename strings, but only free one.

Since the string is actually entirely constant and we don't make any use of printf's format-string operations, we can drop the g_strdup_printf() use entirely.

Quoted 8 lines

---
A small once-only leak, so this is 9.2 material. Spotted with clang leak-sanitizer.

  hw/arm/sbsa-ref.c | 15 ++++++---------
  1 file changed, 6 insertions(+), 9 deletions(-)