Skip to contentSkip to conversation

Error handling cleanup and fixes

PATCH In review Eduardo Habkost 8 participants 24 messages Jun 8, 2017 – Jul 6, 2017 Arm/Zynq architecture

lore
Share & tools
lore
#0 lore raw

cover Error handling cleanup and fixes

This series fixes and simplifies some of the existing code that assigns to *errp directly.

Eduardo Habkost (5):
xilinx: Fix error handling
block: Don't try to set *errp directly
websock: Don't try to set *errp directly
migration: Don't try to set *errp directly
vnc: No need for Error** parameter at vnc_client_io_error()

 ui/vnc.h                |  2 +-
 block.c                 |  8 +++-----
 hw/dma/xilinx_axidma.c  |  4 +---
 hw/net/xilinx_axienet.c |  4 +---
 io/channel-websock.c    |  4 ++--
 migration/migration.c   |  2 +-
 ui/vnc.c                | 13 +++++--------
 7 files changed, 14 insertions(+), 23 deletions(-)
Signature · 1 line
2.11.0.259.g40922b1
#1 lore raw

PATCH 1/5 xilinx: Fix error handling

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

error_propagate() handles non-NULL *errp correctly, so the "if (!*errp)" check can be removed.

Cc "Edgar E. Iglesias" <[email protected]>
Cc Alistair Francis <[email protected]>
Cc Jason Wang <[email protected]>
---
 hw/dma/xilinx_axidma.c  | 4 +---
 hw/net/xilinx_axienet.c | 4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
hw/dma/xilinx_axidma.c +1 -3
index 6065689ad1..3987b5ff96 100644
@@ -554,9 +554,7 @@ static void xilinx_axidma_realize(DeviceState *dev, Error **errp)
     return;
 
 xilinx_axidma_realize_fail:
-    if (!*errp) {
-        *errp = local_err;
-    }
+    error_propagate(errp, local_err);
 }
 
 static void xilinx_axidma_init(Object *obj)
hw/net/xilinx_axienet.c +1 -3
index b6701844d3..5ffa739f68 100644
@@ -981,9 +981,7 @@ static void xilinx_enet_realize(DeviceState *dev, Error **errp)
     return;
 
 xilinx_enet_realize_fail:
-    if (!*errp) {
-        *errp = local_err;
-    }
+    error_propagate(errp, local_err);
 }
 
 static void xilinx_enet_init(Object *obj)
Signature · 1 line
2.11.0.259.g40922b1
Reviewed-by on v1 1/5
#2 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 1/5] xilinx: Fix error handling

Quoted 11 lines · Eduardo Habkost

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

error_propagate() handles non-NULL *errp correctly, so the "if (!*errp)" check can be removed.

Cc "Edgar E. Iglesias" <[email protected]>
Cc Alistair Francis <[email protected]>
Cc Jason Wang <[email protected]>

Thanks,
Alistair

Quoted 39 lines
---
 hw/dma/xilinx_axidma.c  | 4 +---
 hw/net/xilinx_axienet.c | 4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
hw/dma/xilinx_axidma.c +1 -3
index 6065689ad1..3987b5ff96 100644
@@ -554,9 +554,7 @@ static void xilinx_axidma_realize(DeviceState *dev, Error **errp)
     return;
 
 xilinx_axidma_realize_fail:
-    if (!*errp) {
-        *errp = local_err;
-    }
+    error_propagate(errp, local_err);
 }
 
 static void xilinx_axidma_init(Object *obj)
hw/net/xilinx_axienet.c +1 -4
index b6701844d3..5ffa739f68 100644
@@ -981,9 +981,7 @@ static void xilinx_enet_realize(DeviceState *dev, Error **errp)
     return;
 
 xilinx_enet_realize_fail:
-    if (!*errp) {
-        *errp = local_err;
-    }
+    error_propagate(errp, local_err);
 }
 
 static void xilinx_enet_init(Object *obj)
--

2.11.0.259.g40922b1

Reviewed-by on v1 1/5
#3 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 1/5] xilinx: Fix error handling

In reply to Eduardo Habkost
Quoted 36 lines · Eduardo Habkost

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

error_propagate() handles non-NULL *errp correctly, so the "if (!*errp)" check can be removed.

Cc "Edgar E. Iglesias" <[email protected]>
Cc Alistair Francis <[email protected]>
Cc Jason Wang <[email protected]>
---
 hw/dma/xilinx_axidma.c  | 4 +---
 hw/net/xilinx_axienet.c | 4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
hw/dma/xilinx_axidma.c +1 -3
index 6065689ad1..3987b5ff96 100644
@@ -554,9 +554,7 @@ static void xilinx_axidma_realize(DeviceState *dev, Error **errp)
     return;
 
 xilinx_axidma_realize_fail:
-    if (!*errp) {
-        *errp = local_err;
-    }
+    error_propagate(errp, local_err);
 }
 
 static void xilinx_axidma_init(Object *obj)
hw/net/xilinx_axienet.c +0 -0
index b6701844d3..5ffa739f68 100644

hw/net/xilinx_axienet.c static void xilinx_enet_realize(DeviceState *dev, Error **errp) · line 987

Quoted 10 lines
hw/net/xilinx_axienet.c +1 -3
@@ -981,9 +981,7 @@ static void xilinx_enet_realize(DeviceState *dev, Error **errp)
     return;
 
 xilinx_enet_realize_fail:
-    if (!*errp) {
-        *errp = local_err;
-    }
+    error_propagate(errp, local_err);
 }
 
 static void xilinx_enet_init(Object *obj)

The qdev core always passes &err. So this is "merely" a fix for a latent bug.

If it could pass null, you'd fix a crash bug.

If it could pass pointer to non-null (&error_fatal, &error_abort), you'd plug a memory leak.

Suggest to rephrase the commit message:

xilinx: Fix latent error handling bug

Assigning directly to *errp is not valid, as errp may be null, &error_fatal, or &error_abort. The !*errp conditional protects against the latter two, but we then leak @local_err. Fortunately, the qdev core always passes pointer to null, so this is "merely" a latent bug.

Use error_propagate() instead.

What do you think?

With something like that:

#4 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 1/5] xilinx: Fix error handling

Quoted 67 lines · Markus Armbruster
Quoted 36 lines · Eduardo Habkost

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

error_propagate() handles non-NULL *errp correctly, so the "if (!*errp)" check can be removed.

Cc "Edgar E. Iglesias" <[email protected]>
Cc Alistair Francis <[email protected]>
Cc Jason Wang <[email protected]>
---
 hw/dma/xilinx_axidma.c  | 4 +---
 hw/net/xilinx_axienet.c | 4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
hw/dma/xilinx_axidma.c +1 -3
index 6065689ad1..3987b5ff96 100644
@@ -554,9 +554,7 @@ static void xilinx_axidma_realize(DeviceState *dev, Error **errp)
     return;
 
 xilinx_axidma_realize_fail:
-    if (!*errp) {
-        *errp = local_err;
-    }
+    error_propagate(errp, local_err);
 }
 
 static void xilinx_axidma_init(Object *obj)
hw/net/xilinx_axienet.c +0 -0
index b6701844d3..5ffa739f68 100644

hw/net/xilinx_axienet.c static void xilinx_enet_realize(DeviceState *dev, Error **errp) · line 987

Quoted 10 lines
hw/net/xilinx_axienet.c +1 -3
@@ -981,9 +981,7 @@ static void xilinx_enet_realize(DeviceState *dev, Error **errp)
     return;
 
 xilinx_enet_realize_fail:
-    if (!*errp) {
-        *errp = local_err;
-    }
+    error_propagate(errp, local_err);
 }
 
 static void xilinx_enet_init(Object *obj)

The qdev core always passes &err. So this is "merely" a fix for a latent bug.

If it could pass null, you'd fix a crash bug.

If it could pass pointer to non-null (&error_fatal, &error_abort), you'd plug a memory leak.

Suggest to rephrase the commit message:

xilinx: Fix latent error handling bug

Assigning directly to *errp is not valid, as errp may be null, &error_fatal, or &error_abort. The !*errp conditional protects against the latter two, but we then leak @local_err. Fortunately, the qdev core always passes pointer to null, so this is "merely" a latent bug.

Quoted 3 lines

Use error_propagate() instead.

What do you think?

Looks good to me. Do you want to fix it when applying?

Quoted 3 lines

With something like that:

Thanks!

Signature · 1 line
Eduardo
#5 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 1/5] xilinx: Fix error handling

Quoted 68 lines · Eduardo Habkost
Quoted 67 lines · Markus Armbruster
Quoted 36 lines · Eduardo Habkost

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

error_propagate() handles non-NULL *errp correctly, so the "if (!*errp)" check can be removed.

Cc "Edgar E. Iglesias" <[email protected]>
Cc Alistair Francis <[email protected]>
Cc Jason Wang <[email protected]>
---
 hw/dma/xilinx_axidma.c  | 4 +---
 hw/net/xilinx_axienet.c | 4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
hw/dma/xilinx_axidma.c +1 -3
index 6065689ad1..3987b5ff96 100644
@@ -554,9 +554,7 @@ static void xilinx_axidma_realize(DeviceState *dev, Error **errp)
     return;
 
 xilinx_axidma_realize_fail:
-    if (!*errp) {
-        *errp = local_err;
-    }
+    error_propagate(errp, local_err);
 }
 
 static void xilinx_axidma_init(Object *obj)
hw/net/xilinx_axienet.c +0 -0
index b6701844d3..5ffa739f68 100644

hw/net/xilinx_axienet.c static void xilinx_enet_realize(DeviceState *dev, Error **errp) · line 987

Quoted 10 lines
hw/net/xilinx_axienet.c +1 -3
@@ -981,9 +981,7 @@ static void xilinx_enet_realize(DeviceState *dev, Error **errp)
     return;
 
 xilinx_enet_realize_fail:
-    if (!*errp) {
-        *errp = local_err;
-    }
+    error_propagate(errp, local_err);
 }
 
 static void xilinx_enet_init(Object *obj)

The qdev core always passes &err. So this is "merely" a fix for a latent bug.

If it could pass null, you'd fix a crash bug.

If it could pass pointer to non-null (&error_fatal, &error_abort), you'd plug a memory leak.

Suggest to rephrase the commit message:

xilinx: Fix latent error handling bug

Assigning directly to *errp is not valid, as errp may be null, &error_fatal, or &error_abort. The !*errp conditional protects against the latter two, but we then leak @local_err. Fortunately, the qdev core always passes pointer to null, so this is "merely" a latent bug.

Quoted 5 lines
Quoted 3 lines

Use error_propagate() instead.

What do you think?

Looks good to me. Do you want to fix it when applying?

Can do!

Quoted 4 lines
Quoted 2 lines

With something like that:

Thanks!

#6 lore raw

PATCH 2/5 block: Don't try to set *errp directly

In reply to Eduardo Habkost

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

With this, there's no need to check if errp is NULL anymore, as error_propagate() and error_prepend() are able to handle that.

---
 block.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
block.c +3 -5
index fa1d06d846..1750a1838e 100644
@@ -4263,11 +4263,9 @@ bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
     assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
     if (!QLIST_EMPTY(&bs->op_blockers[op])) {
         blocker = QLIST_FIRST(&bs->op_blockers[op]);
-        if (errp) {
-            *errp = error_copy(blocker->reason);
-            error_prepend(errp, "Node '%s' is busy: ",
-                          bdrv_get_device_or_node_name(bs));
-        }
+        error_propagate(errp, error_copy(blocker->reason));
+        error_prepend(errp, "Node '%s' is busy: ",
+                      bdrv_get_device_or_node_name(bs));
         return true;
     }
     return false;
Signature · 1 line
2.11.0.259.g40922b1
Reviewed-by on v1 2/5
#7 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH 2/5] block: Don't try to set *errp directly

Quoted 2 lines · Eduardo Habkost

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

Well, the NULL case was already handled by the code, but &error_fatal and &error_abort certainly not.

Quoted 7 lines

With this, there's no need to check if errp is NULL anymore, as error_propagate() and error_prepend() are able to handle that.

Berto

Reviewed-by on v1 2/5
#8 lore raw

Re: [Qemu-devel] [PATCH 2/5] block: Don't try to set *errp directly

In reply to Eduardo Habkost
Quoted 23 lines · Eduardo Habkost

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

With this, there's no need to check if errp is NULL anymore, as error_propagate() and error_prepend() are able to handle that.

---
 block.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
block.c +0 -1
index fa1d06d846..1750a1838e 100644
@@ -4263,11 +4263,9 @@ bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
     assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
     if (!QLIST_EMPTY(&bs->op_blockers[op])) {
         blocker = QLIST_FIRST(&bs->op_blockers[op]);
-        if (errp) {

block.c bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp) · line 4271

Quoted 10 lines
block.c +3 -4
-            *errp = error_copy(blocker->reason);
-            error_prepend(errp, "Node '%s' is busy: ",
-                          bdrv_get_device_or_node_name(bs));
-        }
+        error_propagate(errp, error_copy(blocker->reason));
+        error_prepend(errp, "Node '%s' is busy: ",
+                      bdrv_get_device_or_node_name(bs));
         return true;
     }
     return false;
#9 lore raw

PATCH 3/5 websock: Don't try to set *errp directly

In reply to Eduardo Habkost

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

Cc "Daniel P. Berrange" <[email protected]>
---
 io/channel-websock.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
io/channel-websock.c +2 -2
index 8fabadea2f..5a3badbec2 100644
@@ -856,7 +856,7 @@ static ssize_t qio_channel_websock_readv(QIOChannel *ioc,
     ssize_t ret;
 
     if (wioc->io_err) {
-        *errp = error_copy(wioc->io_err);
+        error_propagate(errp, error_copy(wioc->io_err));
         return -1;
     }
 
@@ -902,7 +902,7 @@ static ssize_t qio_channel_websock_writev(QIOChannel *ioc,
     ssize_t ret;
 
     if (wioc->io_err) {
-        *errp = error_copy(wioc->io_err);
+        error_propagate(errp, error_copy(wioc->io_err));
         return -1;
     }
 
Signature · 1 line
2.11.0.259.g40922b1
Reviewed-by on v1 3/5
#10 lore raw

Re: [Qemu-devel] [PATCH 3/5] websock: Don't try to set *errp directly

Quoted 36 lines · Eduardo Habkost

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

Cc "Daniel P. Berrange" <[email protected]>

---
io/channel-websock.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
io/channel-websock.c +2 -2
index 8fabadea2f..5a3badbec2 100644
@@ -856,7 +856,7 @@ static ssize_t qio_channel_websock_readv(QIOChannel *ioc,
    ssize_t ret;
 
    if (wioc->io_err) {
-        *errp = error_copy(wioc->io_err);
+        error_propagate(errp, error_copy(wioc->io_err));
        return -1;
    }
 
@@ -902,7 +902,7 @@ static ssize_t qio_channel_websock_writev(QIOChannel *ioc,
    ssize_t ret;
 
    if (wioc->io_err) {
-        *errp = error_copy(wioc->io_err);
+        error_propagate(errp, error_copy(wioc->io_err));
        return -1;
    }
 
Signature · 1 line
2.11.0.259.g40922b1
Reviewed-by on v1 3/5
#11 lore raw

Re: [Qemu-devel] [PATCH 3/5] websock: Don't try to set *errp directly

In reply to Eduardo Habkost
Quoted 23 lines · Eduardo Habkost

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

Cc "Daniel P. Berrange" <[email protected]>
---
 io/channel-websock.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
io/channel-websock.c +1 -1
index 8fabadea2f..5a3badbec2 100644
@@ -856,7 +856,7 @@ static ssize_t qio_channel_websock_readv(QIOChannel *ioc,
     ssize_t ret;
 
     if (wioc->io_err) {
-        *errp = error_copy(wioc->io_err);
+        error_propagate(errp, error_copy(wioc->io_err));
         return -1;
     }
 

io/channel-websock.c static ssize_t qio_channel_websock_writev(QIOChannel *ioc, · line 907

Quoted 7 lines
io/channel-websock.c +1 -1
@@ -902,7 +902,7 @@ static ssize_t qio_channel_websock_writev(QIOChannel *ioc,
     ssize_t ret;
 
     if (wioc->io_err) {
-        *errp = error_copy(wioc->io_err);
+        error_propagate(errp, error_copy(wioc->io_err));
         return -1;
     }

The error_copy() is wasted when !errp. Good enough for an error path. Same for PATCH 2.

#12 lore raw

PATCH 4/5 migration: Don't try to set *errp directly

In reply to Eduardo Habkost

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

Cc Juan Quintela <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
---
 migration/migration.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/migration.c +1 -1
index 48c94c9ca1..5a8bef3ef5 100644
@@ -1188,7 +1188,7 @@ bool migration_is_blocked(Error **errp)
     }
 
     if (migration_blockers) {
-        *errp = error_copy(migration_blockers->data);
+        error_propagate(errp, error_copy(migration_blockers->data));
         return true;
     }
 
Signature · 1 line
2.11.0.259.g40922b1
#14 lore raw

Re: [Qemu-devel] [PATCH 4/5] migration: Don't try to set *errp directly

In reply to Eduardo Habkost
Quoted 15 lines · * Eduardo Habkost ([email protected]) wrote:

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

Cc Juan Quintela <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
---
 migration/migration.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/migration.c +0 -0
index 48c94c9ca1..5a8bef3ef5 100644

migration/migration.c bool migration_is_blocked(Error **errp) · line 1193

Quoted 7 lines
migration/migration.c +1 -1
@@ -1188,7 +1188,7 @@ bool migration_is_blocked(Error **errp)
     }
 
     if (migration_blockers) {
-        *errp = error_copy(migration_blockers->data);
+        error_propagate(errp, error_copy(migration_blockers->data));
         return true;
     }

if errp is NULL doesn't that leak the error_copy ?

Dave

Quoted 4 lines
Signature · 2 lines
-- 
2.11.0.259.g40922b1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#15 lore raw

Re: [Qemu-devel] [PATCH 4/5] migration: Don't try to set *errp directly

Quoted 18 lines · * Dr. David Alan Gilbert ([email protected]) wrote:
Quoted 17 lines · * Eduardo Habkost ([email protected]) wrote:

Assigning directly to *errp is not valid, as errp may be NULL, &error_fatal, or &error_abort. Use error_propagate() instead.

Cc Juan Quintela <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
---
 migration/migration.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/migration.c +0 -0
index 48c94c9ca1..5a8bef3ef5 100644
@@ -1188,7 +1188,7 @@ bool migration_is_blocked(Error **errp)
     }
 
Quoted 7 lines

migration/migration.c bool migration_is_blocked(Error **errp) · line 1193

Quoted 5 lines
migration/migration.c +1 -1
     if (migration_blockers) {
-        *errp = error_copy(migration_blockers->data);
+        error_propagate(errp, error_copy(migration_blockers->data));
         return true;
     }

if errp is NULL doesn't that leak the error_copy ?

To answer myself, no it doesn't - error_propagate free's it in that case.

Dave

Quoted 8 lines

Dave

Quoted 4 lines
Signature · 2 lines
-- 
2.11.0.259.g40922b1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#16 lore raw

PATCH 5/5 vnc: No need for Error** parameter at vnc_client_io_error()

In reply to Eduardo Habkost

The only callers of vnc_client_io_error() with non-NULL errp don't use *errp after the function gets called, so there's no need to use an Error** argument. Change parameter to Error* and simplify the code.

Cc Gerd Hoffmann <[email protected]>
Cc Daniel P. Berrange <[email protected]>
---
 ui/vnc.h |  2 +-
 ui/vnc.c | 13 +++++--------
 2 files changed, 6 insertions(+), 9 deletions(-)
ui/vnc.h +1 -1
index 694cf32ca9..52f65bbd86 100644
@@ -526,7 +526,7 @@ uint32_t read_u32(uint8_t *data, size_t offset);
 
 /* Protocol stage functions */
 void vnc_client_error(VncState *vs);
-ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp);
+ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error *err);
 
 void start_client_init(VncState *vs);
 void start_auth_vnc(VncState *vs);
ui/vnc.c +5 -8
index 47b49c7318..51f13f0c29 100644
@@ -1180,7 +1180,7 @@ void vnc_disconnect_finish(VncState *vs)
     g_free(vs);
 }
 
-ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp)
+ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error *err)
 {
     if (ret <= 0) {
         if (ret == 0) {
@@ -1188,14 +1188,11 @@ ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp)
             vnc_disconnect_start(vs);
         } else if (ret != QIO_CHANNEL_ERR_BLOCK) {
             VNC_DEBUG("Closing down client sock: ret %zd (%s)\n",
-                      ret, errp ? error_get_pretty(*errp) : "Unknown");
+                      ret, err ? error_get_pretty(err) : "Unknown");
             vnc_disconnect_start(vs);
         }
 
-        if (errp) {
-            error_free(*errp);
-            *errp = NULL;
-        }
+        error_free(err);
         return 0;
     }
     return ret;
@@ -1231,7 +1228,7 @@ ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
     ret = qio_channel_write(
         vs->ioc, (const char *)data, datalen, &err);
     VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
-    return vnc_client_io_error(vs, ret, &err);
+    return vnc_client_io_error(vs, ret, err);
 }
 
 
@@ -1344,7 +1341,7 @@ ssize_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
     ret = qio_channel_read(
         vs->ioc, (char *)data, datalen, &err);
     VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret);
-    return vnc_client_io_error(vs, ret, &err);
+    return vnc_client_io_error(vs, ret, err);
 }
 
 
Signature · 1 line
2.11.0.259.g40922b1
#17 lore raw

Re: [Qemu-devel] [PATCH 5/5] vnc: No need for Error** parameter at vnc_client_io_error()

Quoted 13 lines · Eduardo Habkost

The only callers of vnc_client_io_error() with non-NULL errp don't use *errp after the function gets called, so there's no need to use an Error** argument. Change parameter to Error* and simplify the code.

Cc Gerd Hoffmann <[email protected]>
Cc Daniel P. Berrange <[email protected]>
---
 ui/vnc.h |  2 +-
 ui/vnc.c | 13 +++++--------
 2 files changed, 6 insertions(+), 9 deletions(-)

ui/vnc.c void vnc_disconnect_finish(VncState *vs) · line 1184

Quoted 3 lines
ui/vnc.c +1 -1
-ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp)
+ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error *err)
 {

This is unusual.

Quoted 6 lines
     if (ret <= 0) {
         if (ret == 0) {
ui/vnc.c +0 -0
@@ -1188,14 +1188,11 @@ ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp)
             vnc_disconnect_start(vs);
         } else if (ret != QIO_CHANNEL_ERR_BLOCK) {
             VNC_DEBUG("Closing down client sock: ret %zd (%s)\n",

ui/vnc.c ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp) · line 1195

Quoted 10 lines
ui/vnc.c +2 -5
-                      ret, errp ? error_get_pretty(*errp) : "Unknown");
+                      ret, err ? error_get_pretty(err) : "Unknown");
             vnc_disconnect_start(vs);
         }
 
-        if (errp) {
-            error_free(*errp);
-            *errp = NULL;
-        }
+        error_free(err);

Worse, it's action at a distance. You are freeing memory here that was allocated in the callers.

ui/vnc.c ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) · line 1232

Quoted 10 lines
ui/vnc.c +1 -1
         return 0;
     }
     return ret;
@@ -1231,7 +1228,7 @@ ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
     ret = qio_channel_write(
         vs->ioc, (const char *)data, datalen, &err);
     VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
-    return vnc_client_io_error(vs, ret, &err);
+    return vnc_client_io_error(vs, ret, err);
 }

It looks like the only reason that err gets passed is for the sake of VNC_DEBUG messages, but I'm not sure I like this patch.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#18 lore raw

Re: [Qemu-devel] [PATCH 5/5] vnc: No need for Error** parameter at vnc_client_io_error()

Quoted 14 lines · Eric Blake
Quoted 13 lines · Eduardo Habkost

The only callers of vnc_client_io_error() with non-NULL errp don't use *errp after the function gets called, so there's no need to use an Error** argument. Change parameter to Error* and simplify the code.

Cc Gerd Hoffmann <[email protected]>
Cc Daniel P. Berrange <[email protected]>
---
 ui/vnc.h |  2 +-
 ui/vnc.c | 13 +++++--------
 2 files changed, 6 insertions(+), 9 deletions(-)
Quoted 5 lines

ui/vnc.c void vnc_disconnect_finish(VncState *vs) · line 1184

Quoted 3 lines
ui/vnc.c +1 -1
-ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp)
+ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error *err)
 {

This is unusual.

Why? I would say that using Error** for input (and not output) is the unusual pattern.

This is the only remaining place in the whole tree where code assigns something to *errp outside util/error.c (and no callers of this function rely on this feature).

Quoted 12 lines
Quoted 12 lines
     if (ret <= 0) {
         if (ret == 0) {
ui/vnc.c +1 -1
@@ -1188,14 +1188,11 @@ ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp)
             vnc_disconnect_start(vs);
         } else if (ret != QIO_CHANNEL_ERR_BLOCK) {
             VNC_DEBUG("Closing down client sock: ret %zd (%s)\n",
-                      ret, errp ? error_get_pretty(*errp) : "Unknown");
+                      ret, err ? error_get_pretty(err) : "Unknown");
             vnc_disconnect_start(vs);
         }
 
Quoted 8 lines

ui/vnc.c ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp) · line 1195

Quoted 5 lines
ui/vnc.c +1 -4
-        if (errp) {
-            error_free(*errp);
-            *errp = NULL;
-        }
+        error_free(err);

Worse, it's action at a distance. You are freeing memory here that was allocated in the callers.

Isn't this one of the purposes of this function?

The difference here is that now the function function is just taking ownership of err, making the interface and the implementation simpler. If I document this more clearly at vnc_client_io_error()'s declaration, would it make this change acceptable?

Quoted 14 lines

ui/vnc.c ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) · line 1232

Quoted 11 lines
ui/vnc.c +1 -1
         return 0;
     }
     return ret;
@@ -1231,7 +1228,7 @@ ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
     ret = qio_channel_write(
         vs->ioc, (const char *)data, datalen, &err);
     VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret);
-    return vnc_client_io_error(vs, ret, &err);
+    return vnc_client_io_error(vs, ret, err);
 }

It looks like the only reason that err gets passed is for the sake of VNC_DEBUG messages, but I'm not sure I like this patch.

err is passed for two purposes:
* Debug messages;
* Calling error_free().

The function keeps doing both like before, but the difference is that now it just takes ownership of err.

Signature · 1 line
Eduardo
#19 lore raw

Re: [Qemu-devel] [PATCH 5/5] vnc: No need for Error** parameter at vnc_client_io_error()

Quoted 51 lines · Eduardo Habkost
Quoted 14 lines · Eric Blake
Quoted 13 lines · Eduardo Habkost

The only callers of vnc_client_io_error() with non-NULL errp don't use *errp after the function gets called, so there's no need to use an Error** argument. Change parameter to Error* and simplify the code.

Cc Gerd Hoffmann <[email protected]>
Cc Daniel P. Berrange <[email protected]>
---
 ui/vnc.h |  2 +-
 ui/vnc.c | 13 +++++--------
 2 files changed, 6 insertions(+), 9 deletions(-)
Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +1 -1
-ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp)
+ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error *err)
 {

This is unusual.

Why? I would say that using Error** for input (and not output) is the unusual pattern.

This is the only remaining place in the whole tree where code assigns something to *errp outside util/error.c (and no callers of this function rely on this feature).

Quoted 12 lines
Quoted 12 lines
     if (ret <= 0) {
         if (ret == 0) {
(quoted diff) +1 -1
@@ -1188,14 +1188,11 @@ ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp)
             vnc_disconnect_start(vs);
         } else if (ret != QIO_CHANNEL_ERR_BLOCK) {
             VNC_DEBUG("Closing down client sock: ret %zd (%s)\n",
-                      ret, errp ? error_get_pretty(*errp) : "Unknown");
+                      ret, err ? error_get_pretty(err) : "Unknown");
             vnc_disconnect_start(vs);
         }
 
Quoted 8 lines

Review ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp) · line 1195

Quoted 5 lines
(quoted diff) +1 -4
-        if (errp) {
-            error_free(*errp);
-            *errp = NULL;
-        }
+        error_free(err);

Worse, it's action at a distance. You are freeing memory here that was allocated in the callers.

Quoted 7 lines

Isn't this one of the purposes of this function?

The difference here is that now the function function is just taking ownership of err, making the interface and the implementation simpler. If I document this more clearly at vnc_client_io_error()'s declaration, would it make this change acceptable?

What about the following?

---
 ui/vnc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
ui/vnc.c +9 -0
index 51f13f0c29..cb55554210 100644
@@ -1180,6 +1180,15 @@ void vnc_disconnect_finish(VncState *vs)
     g_free(vs);
 }
 
+
+/*
+ * Handle I/O error (@ret < 0) or EOF (@ret == 0) from I/O
+ * channel.  In case of errors or EOF, @err is freed using
+ * error_free().
+ *
+ * Returns 0 in case @ret <= 0 and the error was properly
+ * handled, otherwise returns @ret.
+ */
 ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error *err)
 {
     if (ret <= 0) {
Signature · 4 lines
2.11.0.259.g40922b1

-- 
Eduardo
Reviewed-by
#20 lore raw

Re: [Qemu-devel] [PATCH 5/5] vnc: No need for Error** parameter at vnc_client_io_error()

Quoted 8 lines · Eduardo Habkost
Quoted 8 lines
Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +1 -1
-ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp)
+ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error *err)
 {

This is unusual.

Why? I would say that using Error** for input (and not output) is the unusual pattern.

Yes, and when you frame it that way, it makes sense. But with no comment framing it that way, ...

Quoted 7 lines
Quoted 7 lines

Isn't this one of the purposes of this function?

The difference here is that now the function function is just taking ownership of err, making the interface and the implementation simpler. If I document this more clearly at vnc_client_io_error()'s declaration, would it make this change acceptable?

... why yes, you've hit on why I felt uneasy - we are missing good documentation!

Quoted 3 lines

What about the following?

Yes, that makes it MUCH easier to understand what's going on.

With this squashed in,

Quoted 26 lines
---
 ui/vnc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
ui/vnc.c +9 -0
index 51f13f0c29..cb55554210 100644
@@ -1180,6 +1180,15 @@ void vnc_disconnect_finish(VncState *vs)
     g_free(vs);
 }
 
+
+/*
+ * Handle I/O error (@ret < 0) or EOF (@ret == 0) from I/O
+ * channel.  In case of errors or EOF, @err is freed using
+ * error_free().
+ *
+ * Returns 0 in case @ret <= 0 and the error was properly
+ * handled, otherwise returns @ret.
+ */
 ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error *err)
 {
     if (ret <= 0) {
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on the v1 series
#21 lore raw
In reply to Eduardo Habkost
Quoted 9 lines · Eduardo Habkost

This series fixes and simplifies some of the existing code that assigns to *errp directly.

Eduardo Habkost (5):
xilinx: Fix error handling
block: Don't try to set *errp directly
websock: Don't try to set *errp directly
migration: Don't try to set *errp directly
vnc: No need for Error** parameter at vnc_client_io_error()

For 1-4:

I'm not sure about 5

Quoted 10 lines
 ui/vnc.h                |  2 +-
 block.c                 |  8 +++-----
 hw/dma/xilinx_axidma.c  |  4 +---
 hw/net/xilinx_axienet.c |  4 +---
 io/channel-websock.c    |  4 ++--
 migration/migration.c   |  2 +-
 ui/vnc.c                | 13 +++++--------
 7 files changed, 14 insertions(+), 23 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#22 lore raw
In reply to Eduardo Habkost
Quoted 9 lines · Eduardo Habkost

This series fixes and simplifies some of the existing code that assigns to *errp directly.

Eduardo Habkost (5):
xilinx: Fix error handling
block: Don't try to set *errp directly
websock: Don't try to set *errp directly
migration: Don't try to set *errp directly
vnc: No need for Error** parameter at vnc_client_io_error()

I'm willing to take PATCH 1-3 through my tree. PATCH 4 has been merged already. I'd like to leave PATCH 5 to Gerd.