Skip to contentSkip to conversation

qapi: fix memory leak in QmpOutputVisitor

PATCH In review Pino Toscano 2 participants 6 messages Oct 18, 2016 – Oct 24, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qapi: fix memory leak in QmpOutputVisitor

qmp_output_start_struct() and qmp_output_start_list() create a new QObject (QDict, QList) and push it to the stack of the QmpOutputVisitor, where it is saved as 'value'. When freeing the iterator in qmp_output_free(), these values are never freed properly.

The simple solution is to qobject_decref() them.

---
 qapi/qmp-output-visitor.c | 1 +
 1 file changed, 1 insertion(+)
qapi/qmp-output-visitor.c +1 -0
index 9e3b67c..eedf256 100644
@@ -220,6 +220,7 @@ static void qmp_output_free(Visitor *v)
     while (!QSLIST_EMPTY(&qov->stack)) {
         e = QSLIST_FIRST(&qov->stack);
         QSLIST_REMOVE_HEAD(&qov->stack, node);
+        qobject_decref(e->value);
         g_free(e);
     }
 
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Pino Toscano

qmp_output_start_struct() and qmp_output_start_list() create a new QObject (QDict, QList) and push it to the stack of the QmpOutputVisitor, where it is saved as 'value'. When freeing the iterator in qmp_output_free(), these values are never freed properly.

Do any of the tests (perhaps run under valgrind) show this leak? If not, maybe we should enhance their coverage.

Quoted 5 lines

The simple solution is to qobject_decref() them.

---
 qapi/qmp-output-visitor.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 14 lines
qapi/qmp-output-visitor.c +1 -0
index 9e3b67c..eedf256 100644
@@ -220,6 +220,7 @@ static void qmp_output_free(Visitor *v)
     while (!QSLIST_EMPTY(&qov->stack)) {
         e = QSLIST_FIRST(&qov->stack);
         QSLIST_REMOVE_HEAD(&qov->stack, node);
+        qobject_decref(e->value);
         g_free(e);
     }
 
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
Quoted 10 lines · Eric Blake
Quoted 4 lines · Pino Toscano

qmp_output_start_struct() and qmp_output_start_list() create a new QObject (QDict, QList) and push it to the stack of the QmpOutputVisitor, where it is saved as 'value'. When freeing the iterator in qmp_output_free(), these values are never freed properly.

Do any of the tests (perhaps run under valgrind) show this leak? If not, maybe we should enhance their coverage.

Quoted 1 line
Quoted 6 lines
Quoted 4 lines

The simple solution is to qobject_decref() them.

---
 qapi/qmp-output-visitor.c | 1 +
 1 file changed, 1 insertion(+)

except this should be done against v2, where you had S-o-B :)

Quoted 16 lines
Quoted 15 lines
qapi/qmp-output-visitor.c +1 -0
index 9e3b67c..eedf256 100644
@@ -220,6 +220,7 @@ static void qmp_output_free(Visitor *v)
     while (!QSLIST_EMPTY(&qov->stack)) {
         e = QSLIST_FIRST(&qov->stack);
         QSLIST_REMOVE_HEAD(&qov->stack, node);
+        qobject_decref(e->value);
         g_free(e);
     }
 
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw
In reply to Eric Blake
Quoted 8 lines · On Tuesday, 18 October 2016 06:13:30 CEST Eric Blake wrote:
Quoted 4 lines · Pino Toscano

qmp_output_start_struct() and qmp_output_start_list() create a new QObject (QDict, QList) and push it to the stack of the QmpOutputVisitor, where it is saved as 'value'. When freeing the iterator in qmp_output_free(), these values are never freed properly.

Do any of the tests (perhaps run under valgrind) show this leak? If not, maybe we should enhance their coverage.

Running a simple qemu-img info file.qcow2 under valgrind was enough for me to show the leak.

In this case, another simple fix is needed to fully fix the leak:
http://lists.nongnu.org/archive/html/qemu-devel/2016-10/msg04023.html
(Yes, I just saw your ACK on this, Eric, just leaving it here for reference.)

Quoted 24 lines
Quoted 5 lines

The simple solution is to qobject_decref() them.

---
 qapi/qmp-output-visitor.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 14 lines
qapi/qmp-output-visitor.c +1 -0
index 9e3b67c..eedf256 100644
@@ -220,6 +220,7 @@ static void qmp_output_free(Visitor *v)
     while (!QSLIST_EMPTY(&qov->stack)) {
         e = QSLIST_FIRST(&qov->stack);
         QSLIST_REMOVE_HEAD(&qov->stack, node);
+        qobject_decref(e->value);
         g_free(e);
     }
 
Signature · 1 line
Pino Toscano
#4 lore raw
Quoted 7 lines · Pino Toscano
Quoted 6 lines · On Tuesday, 18 October 2016 06:13:30 CEST Eric Blake wrote:
Quoted 4 lines · Pino Toscano

qmp_output_start_struct() and qmp_output_start_list() create a new QObject (QDict, QList) and push it to the stack of the QmpOutputVisitor, where it is saved as 'value'. When freeing the iterator in qmp_output_free(), these values are never freed properly.

Quoted 5 lines
Quoted 2 lines

Do any of the tests (perhaps run under valgrind) show this leak? If not, maybe we should enhance their coverage.

Running a simple qemu-img info file.qcow2 under valgrind was enough for me to show the leak.

I'm still not reproducing it. :(

Quoted 3 lines

In this case, another simple fix is needed to fully fix the leak:
http://lists.nongnu.org/archive/html/qemu-devel/2016-10/msg04023.html

In fact, isn't that fix alone enough to fix the leak? The more I think about this patch (and the thread on v2), the more I think it is too prone to double-freeing things.

Quoted 14 lines
Quoted 12 lines
Quoted 10 lines

+++ b/qapi/qmp-output-visitor.c

qapi/qmp-output-visitor.c +1 -0
@@ -220,6 +220,7 @@ static void qmp_output_free(Visitor *v)
     while (!QSLIST_EMPTY(&qov->stack)) {
         e = QSLIST_FIRST(&qov->stack);
         QSLIST_REMOVE_HEAD(&qov->stack, node);
+        qobject_decref(e->value);
         g_free(e);
     }
 
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#5 lore raw
Quoted 8 lines · On Friday, 21 October 2016 16:20:30 CEST Eric Blake wrote:
Quoted 7 lines · Pino Toscano
Quoted 6 lines · On Tuesday, 18 October 2016 06:13:30 CEST Eric Blake wrote:
Quoted 4 lines · Pino Toscano

qmp_output_start_struct() and qmp_output_start_list() create a new QObject (QDict, QList) and push it to the stack of the QmpOutputVisitor, where it is saved as 'value'. When freeing the iterator in qmp_output_free(), these values are never freed properly.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Do any of the tests (perhaps run under valgrind) show this leak? If not, maybe we should enhance their coverage.

Running a simple qemu-img info file.qcow2 under valgrind was enough for me to show the leak.

I'm still not reproducing it. :(

Funny, now I cannot either, not even by resetting master back to the day when I did the patches. And I'm pretty sure that it was an issue, since doing only this patch did not fully fix the leak: valgrind runs were fine, so a full run of the libguestfs test suite with the rebuild qemu as hypervisor.

Quoted 6 lines
Quoted 2 lines

In this case, another simple fix is needed to fully fix the leak:
http://lists.nongnu.org/archive/html/qemu-devel/2016-10/msg04023.html

In fact, isn't that fix alone enough to fix the leak? The more I think about this patch (and the thread on v2), the more I think it is too prone to double-freeing things.

I agree on the "this is not needed part", so let's just drop this patch.

Thanks,

Signature · 1 line
Pino Toscano