Skip to contentSkip to conversation

xen-block: Fix uninitialized variable

PATCH In review Anthony PERARD 3 participants 4 messages Apr 6, 2020 – Apr 7, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen-block: Fix uninitialized variable

Since 7f5d9b206d1e ("object-add: don't create return value if failed"), qmp_object_add() don't write any value in 'ret_data', thus has random data. Then qobject_unref() fails and abort().

Fix by initialising 'ret_data' properly.

Fixes 5f07c4d60d09 ("qapi: Flatten object-add")
---
 hw/block/xen-block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/block/xen-block.c +1 -1
index 07bb32e22b51..99cb4c67cb09 100644
@@ -860,7 +860,7 @@ static XenBlockIOThread *xen_block_iothread_create(const char *id,
     XenBlockIOThread *iothread = g_new(XenBlockIOThread, 1);
     Error *local_err = NULL;
     QDict *opts;
-    QObject *ret_data;
+    QObject *ret_data = NULL;
 
     iothread->id = g_strdup(id);
 
Signature · 1 line
Anthony PERARD
#1 lore raw
Quoted 5 lines · Anthony PERARD

Since 7f5d9b206d1e ("object-add: don't create return value if failed"), qmp_object_add() don't write any value in 'ret_data', thus has random data. Then qobject_unref() fails and abort().

Fix by initialising 'ret_data' properly.

Or move qobject_unref() after the error check?

-- >8 --

hw/block/xen-block.c +1 -1
index 07bb32e22b..f3f1cbef65 100644
@@ -869,7 +869,6 @@ static XenBlockIOThread *xen_block_iothread_create(const char *id,
     qdict_put_str(opts, "id", id);
     qmp_object_add(opts, &ret_data, &local_err);
     qobject_unref(opts);
-    qobject_unref(ret_data);
 
     if (local_err) {
         error_propagate(errp, local_err);
@@ -878,6 +877,7 @@ static XenBlockIOThread *xen_block_iothread_create(const char *id,
         g_free(iothread);
         return NULL;
     }
+    qobject_unref(ret_data);
 
     return iothread;
 }

---

Quoted 21 lines
Fixes 5f07c4d60d09 ("qapi: Flatten object-add")
---
  hw/block/xen-block.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/block/xen-block.c +1 -1
index 07bb32e22b51..99cb4c67cb09 100644
@@ -860,7 +860,7 @@ static XenBlockIOThread *xen_block_iothread_create(const char *id,
      XenBlockIOThread *iothread = g_new(XenBlockIOThread, 1);
      Error *local_err = NULL;
      QDict *opts;
-    QObject *ret_data;
+    QObject *ret_data = NULL;
 
      iothread->id = g_strdup(id);
 
#2 lore raw
Quoted 24 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Anthony PERARD

Since 7f5d9b206d1e ("object-add: don't create return value if failed"), qmp_object_add() don't write any value in 'ret_data', thus has random data. Then qobject_unref() fails and abort().

Fix by initialising 'ret_data' properly.

Or move qobject_unref() after the error check?

-- >8 --

hw/block/xen-block.c +0 -1
index 07bb32e22b..f3f1cbef65 100644
@@ -869,7 +869,6 @@ static XenBlockIOThread *xen_block_iothread_create(const
 char *id,
     qdict_put_str(opts, "id", id);
     qmp_object_add(opts, &ret_data, &local_err);
     qobject_unref(opts);
-    qobject_unref(ret_data);
 
     if (local_err) {
         error_propagate(errp, local_err);

hw/block/xen-block.c static XenBlockIOThread *xen_block_iothread_create(const · line 881

Quoted 5 lines
hw/block/xen-block.c +1 -0
@@ -878,6 +877,7 @@ static XenBlockIOThread *xen_block_iothread_create(const
 char *id,
         g_free(iothread);
         return NULL;
     }
+    qobject_unref(ret_data);

That won't help, qmp_object_add() doesn't change the value of ret_data at all. The other users of qmp_object_add() passes an initialised 'ret_data', so we should do the same I think.

Thanks,

Signature · 1 line
Anthony PERARD
Reviewed-by
#3 lore raw
Quoted 22 lines · Anthony PERARD
Quoted 21 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Anthony PERARD

Since 7f5d9b206d1e ("object-add: don't create return value if failed"), qmp_object_add() don't write any value in 'ret_data', thus has random data. Then qobject_unref() fails and abort().

Fix by initialising 'ret_data' properly.

Or move qobject_unref() after the error check?

-- >8 --

hw/block/xen-block.c +0 -1
index 07bb32e22b..f3f1cbef65 100644
@@ -869,7 +869,6 @@ static XenBlockIOThread *xen_block_iothread_create(const
 char *id,
     qdict_put_str(opts, "id", id);
     qmp_object_add(opts, &ret_data, &local_err);
     qobject_unref(opts);
-    qobject_unref(ret_data);
 
Quoted 12 lines

hw/block/xen-block.c static XenBlockIOThread *xen_block_iothread_create(const · line 881

Quoted 8 lines
hw/block/xen-block.c +0 -0
     if (local_err) {
         error_propagate(errp, local_err);
hw/block/xen-block.c +1 -0
@@ -878,6 +877,7 @@ static XenBlockIOThread *xen_block_iothread_create(const
 char *id,
         g_free(iothread);
         return NULL;
     }
+    qobject_unref(ret_data);

That won't help, qmp_object_add() doesn't change the value of ret_data at all. The other users of qmp_object_add() passes an initialised 'ret_data', so we should do the same I think.

Since the QMP core does it, other callers should do it, too.

For QAPI commands that don't return anything, the marshaller should not use @ret_data, let alone store through it. qmp_object_add() complies. Thus, assert(!ret_data) would do. qobject_unref(ret_data) is also correct.