Skip to contentSkip to conversation

hw/machine: Free old values of string properties

PATCH Applied Marcel Apfelbaum 6 participants 11 messages Jun 1, 2014 – Aug 7, 2014 4 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 13 lines · Eduardo Habkost

---
Cc: Marcel Apfelbaum <[email protected]>
Cc: Andreas Färber <[email protected]>

---
 hw/core/machine.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/core/machine.c +0 -0
index cbba679..df612bb 100644

hw/core/machine.c static void machine_set_accel(Object *obj, const char *value, Error **errp) · line 27

Quoted 4 lines
hw/core/machine.c +1 -0
@@ -24,6 +24,7 @@ static void machine_set_accel(Object *obj, const char *value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
 
+    g_free(ms->accel);

I am not sure if in QMP is not caller's responsibility to free the input string. If I think about it, I ask an object to set "my" string and it deletes it :(... Same for the others.

Added Markus and Luiz, maybe they have an opinion on that. Thanks,
Marcel

Quoted 59 lines
hw/core/machine.c +7 -0
     ms->accel = g_strdup(value);
 }
 
@@ -79,6 +80,7 @@ static void machine_set_kernel(Object *obj, const char *value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
 
+    g_free(ms->kernel_filename);
     ms->kernel_filename = g_strdup(value);
 }
 
@@ -93,6 +95,7 @@ static void machine_set_initrd(Object *obj, const char *value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
 
+    g_free(ms->initrd_filename);
     ms->initrd_filename = g_strdup(value);
 }
 
@@ -107,6 +110,7 @@ static void machine_set_append(Object *obj, const char *value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
 
+    g_free(ms->kernel_cmdline);
     ms->kernel_cmdline = g_strdup(value);
 }
 
@@ -121,6 +125,7 @@ static void machine_set_dtb(Object *obj, const char *value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
 
+    g_free(ms->dtb);
     ms->dtb = g_strdup(value);
 }
 
@@ -135,6 +140,7 @@ static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
 
+    g_free(ms->dumpdtb);
     ms->dumpdtb = g_strdup(value);
 }
 
@@ -176,6 +182,7 @@ static void machine_set_dt_compatible(Object *obj, const char *value, Error **er
 {
     MachineState *ms = MACHINE(obj);
 
+    g_free(ms->dt_compatible);
     ms->dt_compatible = g_strdup(value);
 }
 
@@ -232,6 +239,7 @@ static void machine_set_firmware(Object *obj, const char *value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
 
+    g_free(ms->firmware);
     ms->firmware = g_strdup(value);
 }
 
#3 lore raw
Quoted 17 lines · Marcel Apfelbaum
Quoted 16 lines · Eduardo Habkost

---
Cc: Marcel Apfelbaum <[email protected]>
Cc: Andreas Färber <[email protected]>

---
 hw/core/machine.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/core/machine.c +0 -0
index cbba679..df612bb 100644
@@ -24,6 +24,7 @@ static void machine_set_accel(Object *obj, const char *value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
 
Quoted 10 lines

hw/core/machine.c static void machine_set_accel(Object *obj, const char *value, Error **errp) · line 27

Quoted 1 line
hw/core/machine.c +1 -0
+    g_free(ms->accel);

I am not sure if in QMP is not caller's responsibility to free the input string. If I think about it, I ask an object to set "my" string and it deletes it :(... Same for the others.

Added Markus and Luiz, maybe they have an opinion on that.

Quoted 3 lines
hw/core/machine.c +0 -0
     ms->accel = g_strdup(value);
 }
 

Misunderstanding? Eduardo's patch frees the old value before it overwrites it. It doesn't free "the input string", assuming by "the input string" you mean argument value.

#4 lore raw
Quoted 26 lines · Markus Armbruster
Quoted 24 lines · Marcel Apfelbaum
Quoted 13 lines · Eduardo Habkost

---
Cc: Marcel Apfelbaum <[email protected]>
Cc: Andreas Färber <[email protected]>

---
 hw/core/machine.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/core/machine.c +0 -0
index cbba679..df612bb 100644

hw/core/machine.c static void machine_set_accel(Object *obj, const char *value, Error **errp) · line 27

Quoted 4 lines
hw/core/machine.c +1 -0
@@ -24,6 +24,7 @@ static void machine_set_accel(Object *obj, const char *value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
 
+    g_free(ms->accel);

I am not sure if in QMP is not caller's responsibility to free the input string. If I think about it, I ask an object to set "my" string and it deletes it :(... Same for the others.

Added Markus and Luiz, maybe they have an opinion on that.

Quoted 7 lines
Quoted 3 lines

hw/core/machine.c static void machine_set_accel(Object *obj, const char *value, Error **errp) · line 30

Quoted 3 lines
hw/core/machine.c +0 -0
     ms->accel = g_strdup(value);
 }
 

Misunderstanding? Eduardo's patch frees the old value before it overwrites it. It doesn't free "the input string", assuming by "the input string" you mean argument value.

You are right! My bad, for some reason I saw g_free(value), but it was me not reading it right :(.

Thanks,
Marcel

Reviewed-by
#5 lore raw
Quoted 33 lines · Marcel Apfelbaum
Quoted 32 lines · Markus Armbruster
Quoted 29 lines · Marcel Apfelbaum
Quoted 13 lines · Eduardo Habkost

---
Cc: Marcel Apfelbaum <[email protected]>
Cc: Andreas Färber <[email protected]>

---
 hw/core/machine.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/core/machine.c +0 -0
index cbba679..df612bb 100644

hw/core/machine.c static void machine_set_accel(Object *obj, const char *value, Error **errp) · line 27

Quoted 4 lines
hw/core/machine.c +1 -0
@@ -24,6 +24,7 @@ static void machine_set_accel(Object *obj, const char *value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
 
+    g_free(ms->accel);

I am not sure if in QMP is not caller's responsibility to free the input string.
If I think about it, I ask an object to set "my" string and it deletes it :(...
Same for the others.

Added Markus and Luiz, maybe they have an opinion on that.

Quoted 3 lines
hw/core/machine.c +0 -0
     ms->accel = g_strdup(value);
 }
 
Quoted 6 lines
Quoted 3 lines

Misunderstanding? Eduardo's patch frees the old value before it overwrites it. It doesn't free "the input string", assuming by "the input string" you mean argument value.

You are right! My bad, for some reason I saw g_free(value), but it was me not reading it right :(.

Happens :)

author Reviewed-by
#6 lore raw
Quoted 39 lines · Markus Armbruster
Quoted 37 lines · Marcel Apfelbaum
Quoted 35 lines · Markus Armbruster
Quoted 17 lines · Marcel Apfelbaum
Quoted 16 lines · Eduardo Habkost

---
Cc: Marcel Apfelbaum <[email protected]>
Cc: Andreas Färber <[email protected]>

---
 hw/core/machine.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/core/machine.c +0 -0
index cbba679..df612bb 100644
@@ -24,6 +24,7 @@ static void machine_set_accel(Object *obj, const char *value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
 
Quoted 12 lines

hw/core/machine.c static void machine_set_accel(Object *obj, const char *value, Error **errp) · line 27

Quoted 1 line
hw/core/machine.c +1 -0
+    g_free(ms->accel);

I am not sure if in QMP is not caller's responsibility to free the input string.
If I think about it, I ask an object to set "my" string and it deletes it :(...
Same for the others.

Added Markus and Luiz, maybe they have an opinion on that.

Quoted 3 lines
hw/core/machine.c +0 -0
     ms->accel = g_strdup(value);
 }
 

Misunderstanding? Eduardo's patch frees the old value before it overwrites it. It doesn't free "the input string", assuming by "the input string" you mean argument value.

Quoted 6 lines
Quoted 2 lines

You are right! My bad, for some reason I saw g_free(value), but it was me not reading it right :(.

Happens :)

Yes indeed.

A message that is not in the archive (sent off-list or not yet received): [email protected]

Reviewed-by
#8 lore raw

Re: [Qemu-devel] [PATCH 1/3] rng-random: NULL check not needed before g_free()

Quoted 15 lines · Eduardo Habkost

g_free() is NULL-safe.

---
Cc: Anthony Liguori <[email protected]>
Cc: Luiz Capitulino <[email protected]>

---
 backends/rng-random.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
backends/rng-random.c +0 -0
index 136499d..601d9dc 100644

backends/rng-random.c static void rng_random_set_filename(Object *obj, const char *filename, · line 111

Quoted 10 lines
backends/rng-random.c +1 -4
@@ -106,10 +106,7 @@ static void rng_random_set_filename(Object *obj, const char *filename,
         return;
     }
 
-    if (s->filename) {
-        g_free(s->filename);
-    }
-
+    g_free(s->filename);
     s->filename = g_strdup(filename);
 }

Note there are a quite a few more instances of this anti-pattern. Coccinelle can find them, semantic patch appended. Beware, it can throw away comments in the conditional, which is usually not what we want.

@@
expression E;
@@
- if (E != NULL) {
- g_free(E);
- }
+ g_free(E);

#9 lore raw

Re: [Qemu-devel] [PATCH 1/3] rng-random: NULL check not needed before g_free()

Quoted 39 lines · Markus Armbruster
Quoted 15 lines · Eduardo Habkost

g_free() is NULL-safe.

---
Cc: Anthony Liguori <[email protected]>
Cc: Luiz Capitulino <[email protected]>

---
 backends/rng-random.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
backends/rng-random.c +0 -0
index 136499d..601d9dc 100644

backends/rng-random.c static void rng_random_set_filename(Object *obj, const char *filename, · line 111

Quoted 10 lines
backends/rng-random.c +1 -4
@@ -106,10 +106,7 @@ static void rng_random_set_filename(Object *obj, const char *filename,
         return;
     }
 
-    if (s->filename) {
-        g_free(s->filename);
-    }
-
+    g_free(s->filename);
     s->filename = g_strdup(filename);
 }

Note there are a quite a few more instances of this anti-pattern. Coccinelle can find them, semantic patch appended. Beware, it can throw away comments in the conditional, which is usually not what we want.

@@
expression E;
@@

Review

Quoted 4 lines
(quoted diff) +1 -3
-	if (E != NULL) {
-	    g_free(E);
-	}
+       g_free(E);

[amos@z qemu]$ grep g_free -B 2 -r * |less

then try to search "/if.*NULL"

Not all can be found by this way, but many instances are found.

Signature · 1 line
			Amos.
#10 lore raw

Re: [Qemu-devel] [PATCH 1/3] rng-random: NULL check not needed before g_free()

In reply to a message not in the archive
Quoted 3 lines · On Fri, 30 May 2014 17:02:18 -0300 Eduardo Habkost <[email protected]> wrote:

g_free() is NULL-safe.

This one should to go through qemu-trivial (CCed).

Quoted 23 lines

---
Cc: Anthony Liguori <[email protected]>
Cc: Luiz Capitulino <[email protected]>

---
 backends/rng-random.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
backends/rng-random.c +1 -4
index 136499d..601d9dc 100644
@@ -106,10 +106,7 @@ static void rng_random_set_filename(Object *obj, const char *filename,
         return;
     }
 
-    if (s->filename) {
-        g_free(s->filename);
-    }
-
+    g_free(s->filename);
     s->filename = g_strdup(filename);
 }
 
#11 lore raw

Re: [Qemu-devel] [Qemu-trivial] [PATCH 1/3] rng-random: NULL check not needed before g_free()

Quoted 4 lines · Luiz Capitulino
Quoted 1 line · On Fri, 30 May 2014 17:02:18 -0300 Eduardo Habkost <[email protected]> wrote:

g_free() is NULL-safe.

Applied to -trivial, thank you!

/mjt

A message that is not in the archive (sent off-list or not yet received): [email protected]

Reviewed-by
#13 lore raw

Re: [Qemu-devel] [PATCH 2/3] rng-egd: Free old chr_name value before setting new one

Quoted 13 lines · Eduardo Habkost

---
Cc: Anthony Liguori <[email protected]>
Cc: Amos Kong <[email protected]>

---
 backends/rng-egd.c | 1 +
 1 file changed, 1 insertion(+)
backends/rng-egd.c +0 -0
index 25bb3b4..2962795 100644

backends/rng-egd.c static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp) · line 175

Quoted 7 lines
backends/rng-egd.c +1 -0
@@ -169,6 +169,7 @@ static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp)
     if (b->opened) {
         error_set(errp, QERR_PERMISSION_DENIED);
     } else {
+        g_free(s->chr_name);
         s->chr_name = g_strdup(value);
     }
 }

I see you searched for the bug elsewhere. Appreciated. Do you think you got them all?

Aside: I hate

    if (bad) {
        handle error
    } else {
        continue normal work
    }

The normal flow gets nested deeper and deeper. When the code grows, normal work can easily end up after the conditional by mistake. Better:

    if (bad) {
        handle error
        return
    }
    continue normal work

Anyway, not your fault.

#14 lore raw

Re: [Qemu-devel] [PATCH 2/3] rng-egd: Free old chr_name value before setting new one

Quoted 10 lines · Markus Armbruster
Quoted 8 lines · Eduardo Habkost

---
Cc: Anthony Liguori <[email protected]>
Cc: Amos Kong <[email protected]>

---
 backends/rng-egd.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 15 lines

backends/rng-egd.c static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp) · line 175

Quoted 12 lines
backends/rng-egd.c +1 -0
index 25bb3b4..2962795 100644
@@ -169,6 +169,7 @@ static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp)
     if (b->opened) {
         error_set(errp, QERR_PERMISSION_DENIED);
     } else {
+        g_free(s->chr_name);
         s->chr_name = g_strdup(value);
     }
 }

I see you searched for the bug elsewhere. Appreciated. Do you think you got them all?

I grepped for all object_property_add_str occurrences, and (unless I made a mistake) I fixed all of them.

Signature · 1 line
Eduardo