Skip to contentSkip to conversation

g_return_if_fail(), g_return_val_if_fail() misuse

PATCH Applied Markus Armbruster 4 participants 9 messages Jan 26, 2021 – Mar 9, 2021

lore
Share & tools
lore
#0 lore raw

cover g_return_if_fail(), g_return_val_if_fail() misuse

Markus Armbruster (2):
backends/dbus-vmstate: Fix short read error handling
vhost_user_gpu: Drop dead check for g_malloc() failure

 backends/dbus-vmstate.c     | 5 ++++-
 hw/display/vhost-user-gpu.c | 1 -
 2 files changed, 4 insertions(+), 2 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/2 backends/dbus-vmstate: Fix short read error handling

When dbus_vmstate_post_load() fails, it complains to stderr. Except on short read, where it checks with g_return_val_if_fail(). This fails silently if G_DISABLE_CHECKS is undefined (it should be), or else pads the short read with uninitialized bytes.

Replace g_return_val_if_fail() by a proper error check.

Fixes 5010cec2bc87dafab39b3913c8ca91f88df9c540
---
 backends/dbus-vmstate.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
backends/dbus-vmstate.c +4 -1
index bd050e8e9c..2a0d2e4a31 100644
@@ -229,7 +229,10 @@ static int dbus_vmstate_post_load(void *opaque, int version_id)
                                      &bytes_read, NULL, &err)) {
             goto error;
         }
-        g_return_val_if_fail(bytes_read == len, -1);
+        if (bytes_read != len) {
+            error_report("%s: Short read", __func__);
+            return -1;
+        }
         id[len] = 0;
 
         trace_dbus_vmstate_loading(id);
Signature · 1 line
2.26.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] backends/dbus-vmstate: Fix short read error handling

Quoted 10 lines · Markus Armbruster

When dbus_vmstate_post_load() fails, it complains to stderr. Except on short read, where it checks with g_return_val_if_fail(). This fails silently if G_DISABLE_CHECKS is undefined (it should be), or else pads the short read with uninitialized bytes.

Replace g_return_val_if_fail() by a proper error check.

Fixes 5010cec2bc87dafab39b3913c8ca91f88df9c540

thanks

Quoted 24 lines
---
 backends/dbus-vmstate.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
backends/dbus-vmstate.c +4 -2
index bd050e8e9c..2a0d2e4a31 100644
@@ -229,7 +229,10 @@ static int dbus_vmstate_post_load(void *opaque, int version_id)
                                      &bytes_read, NULL, &err)) {
             goto error;
         }
-        g_return_val_if_fail(bytes_read == len, -1);
+        if (bytes_read != len) {
+            error_report("%s: Short read", __func__);
+            return -1;
+        }
         id[len] = 0;
 
         trace_dbus_vmstate_loading(id);
--

2.26.2

Signature · 1 line
Marc-André Lureau
#3 lore raw

Re: [PATCH 1/2] backends/dbus-vmstate: Fix short read error handling

In reply to Markus Armbruster

Le 26/01/2021 à 13:42, Markus Armbruster a écrit :

Quoted 20 lines

When dbus_vmstate_post_load() fails, it complains to stderr. Except on short read, where it checks with g_return_val_if_fail(). This fails silently if G_DISABLE_CHECKS is undefined (it should be), or else pads the short read with uninitialized bytes.

Replace g_return_val_if_fail() by a proper error check.

Fixes 5010cec2bc87dafab39b3913c8ca91f88df9c540
---
 backends/dbus-vmstate.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
backends/dbus-vmstate.c +0 -0
index bd050e8e9c..2a0d2e4a31 100644
@@ -229,7 +229,10 @@ static int dbus_vmstate_post_load(void *opaque, int version_id)
                                      &bytes_read, NULL, &err)) {

backends/dbus-vmstate.c static int dbus_vmstate_post_load(void *opaque, int version_id) · line 238

Quoted 10 lines
backends/dbus-vmstate.c +4 -1
             goto error;
         }
-        g_return_val_if_fail(bytes_read == len, -1);
+        if (bytes_read != len) {
+            error_report("%s: Short read", __func__);
+            return -1;
+        }
         id[len] = 0;
 
         trace_dbus_vmstate_loading(id);

Applied to my trivial-patches branch.

Thanks,
Laurent

#4 lore raw

PATCH 2/2 vhost_user_gpu: Drop dead check for g_malloc() failure

In reply to Markus Armbruster
---
 hw/display/vhost-user-gpu.c | 1 -
 1 file changed, 1 deletion(-)
hw/display/vhost-user-gpu.c +0 -1
index 51f1747c4a..db042c7c5e 100644
@@ -332,7 +332,6 @@ vhost_user_gpu_chr_read(void *opaque)
     }
 
     msg = g_malloc(VHOST_USER_GPU_HDR_SIZE + size);
-    g_return_if_fail(msg != NULL);
 
     r = qemu_chr_fe_read_all(&g->vhost_chr,
                              (uint8_t *)&msg->payload, size);
Signature · 1 line
2.26.2
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] vhost_user_gpu: Drop dead check for g_malloc() failure

Quoted 2 lines · Markus Armbruster

thanks

Quoted 20 lines
---
 hw/display/vhost-user-gpu.c | 1 -
 1 file changed, 1 deletion(-)
hw/display/vhost-user-gpu.c +0 -2
index 51f1747c4a..db042c7c5e 100644
@@ -332,7 +332,6 @@ vhost_user_gpu_chr_read(void *opaque)
     }
 
     msg = g_malloc(VHOST_USER_GPU_HDR_SIZE + size);
-    g_return_if_fail(msg != NULL);
 
     r = qemu_chr_fe_read_all(&g->vhost_chr,
                              (uint8_t *)&msg->payload, size);
--

2.26.2

Signature · 1 line
Marc-André Lureau
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] vhost_user_gpu: Drop dead check for g_malloc() failure

In reply to Markus Armbruster
Quoted 1 line · Markus Armbruster
Quoted 18 lines
---
 hw/display/vhost-user-gpu.c | 1 -
 1 file changed, 1 deletion(-)
hw/display/vhost-user-gpu.c +0 -1
index 51f1747c4a..db042c7c5e 100644
@@ -332,7 +332,6 @@ vhost_user_gpu_chr_read(void *opaque)
     }
 
     msg = g_malloc(VHOST_USER_GPU_HDR_SIZE + size);
-    g_return_if_fail(msg != NULL);
 
     r = qemu_chr_fe_read_all(&g->vhost_chr,
                              (uint8_t *)&msg->payload, size);
Signature · 1 line
2.26.2
#7 lore raw

Re: [PATCH 2/2] vhost_user_gpu: Drop dead check for g_malloc() failure

In reply to Markus Armbruster

Le 26/01/2021 à 13:42, Markus Armbruster a écrit :

Quoted 11 lines
---
 hw/display/vhost-user-gpu.c | 1 -
 1 file changed, 1 deletion(-)
hw/display/vhost-user-gpu.c +0 -0
index 51f1747c4a..db042c7c5e 100644

hw/display/vhost-user-gpu.c vhost_user_gpu_chr_read(void *opaque) · line 337

Quoted 7 lines
hw/display/vhost-user-gpu.c +0 -1
@@ -332,7 +332,6 @@ vhost_user_gpu_chr_read(void *opaque)
     }
 
     msg = g_malloc(VHOST_USER_GPU_HDR_SIZE + size);
-    g_return_if_fail(msg != NULL);
 
     r = qemu_chr_fe_read_all(&g->vhost_chr,
                              (uint8_t *)&msg->payload, size);

Applied to my trivial-patches branch.

Thanks,
Laurent

#8 lore raw
In reply to Markus Armbruster

I think this could go via qemu-trivial.

Quoted 7 lines · Markus Armbruster

Markus Armbruster (2):
backends/dbus-vmstate: Fix short read error handling
vhost_user_gpu: Drop dead check for g_malloc() failure

 backends/dbus-vmstate.c     | 5 ++++-
 hw/display/vhost-user-gpu.c | 1 -
 2 files changed, 4 insertions(+), 2 deletions(-)