A bit of cleanup around Error
Subsystems (2)
cover A bit of cleanup around Error
Note: the last patch adds a drive-by FIXME. I asked for advice on how to fix it:
Subject: Incorrect error handling in xen_enable_tpm()
To: [email protected]
Cc: Stefano Stabellini <[email protected]>, Anthony PERARD <[email protected]>, Paul
Durrant <[email protected]>, "Edgar E. Iglesias" <[email protected]>, [email protected], Vikram Garhwal <[email protected]>
Date: Fri, 14 Nov 2025 10:16:58 +0100
Message-ID: <[email protected]>
No reply so far.
Markus Armbruster (5):
hw/core/loader: Make load_elf_hdr() return bool, simplify caller hw/nvram/xlnx-bbram: More idiomatic and simpler error reporting nbd/client-connection: Replace error_propagate() by assignment
error: error_free(NULL) is safe, drop unnecessary conditionals
error: Consistently name Error * objects err, and not errp
include/hw/loader.h | 4 +++- block/crypto.c | 8 ++++---- hw/acpi/ghes.c | 8 ++++---- hw/acpi/pcihp.c | 4 +--- hw/arm/boot.c | 6 +----- hw/core/loader.c | 8 ++++++-- hw/nvram/xlnx-bbram.c | 18 ++++-------------- hw/ppc/spapr.c | 16 ++++++++-------- hw/riscv/spike.c | 10 +--------- hw/xen/xen-pvh-common.c | 13 ++++++++++--- io/channel-websock.c | 4 +--- io/task.c | 4 +--- migration/migration.c | 6 ++---- nbd/client-connection.c | 3 +-- nbd/common.c | 6 +++--- tests/unit/test-smp-parse.c | 5 +---- 16 files changed, 51 insertions(+), 72 deletions(-)
Signature · 1 line
2.49.0
PATCH 1/5 hw/core/loader: Make load_elf_hdr() return bool, simplify caller
--- include/hw/loader.h | 4 +++- hw/arm/boot.c | 6 +----- hw/core/loader.c | 8 ++++++-- hw/riscv/spike.c | 10 +--------- 4 files changed, 11 insertions(+), 17 deletions(-)
include/hw/loader.h +3 -1
@@ -188,8 +188,10 @@ ssize_t load_elf(const char *filename,* * Inspect an ELF file's header. Read its full header contents into a * buffer and/or determine if the ELF is 64bit. +* +* Returns true on success, false on failure. */ -void load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp); +bool load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp);ssize_t load_aout (const char *filename,hwaddr addr,int max_sz,bool big_endian,hwaddr target_page_size);
hw/arm/boot.c +1 -5
@@ -766,16 +766,12 @@ static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,int data_swab =0 ;int elf_data_order;ssize_t ret; -Error *err =NULL ; - -load_elf_hdr (info->kernel_filename, &elf_header, &elf_is64, &err); -if (err) { +if (!load_elf_hdr (info->kernel_filename, &elf_header, &elf_is64,NULL )) {/* * If the file is not an ELF file we silently return. * The caller will fall back to try other formats. */ -error_free (err);return -1 ; }
hw/core/loader.c +6 -2
@@ -364,8 +364,9 @@ const char *load_elf_strerror(ssize_t error) } } -void load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp) +bool load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp) { +bool ok = false;int fd;uint8_t e_ident_local[EI_NIDENT ];uint8_t *e_ident;
@@ -380,7 +381,7 @@ void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp) fd =open (filename,O_RDONLY |O_BINARY );if (fd <0 ) {error_setg_errno (errp, errno,"Failed to open file: %s" , filename); -return ; +return false; }if (read (fd, hdr,EI_NIDENT ) !=EI_NIDENT ) {error_setg_errno (errp, errno,"Failed to read file: %s" , filename);
@@ -415,8 +416,11 @@ void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp) off += br; } + ok = true; + fail:close (fd); +return ok; }/* return < 0 if error, otherwise the number of bytes loaded in memory */
hw/riscv/spike.c +1 -9
@@ -180,15 +180,7 @@ static void create_fdt(SpikeState *s, const MemMapEntry *memmap,static bool spike_test_elf_image (char *filename) { -Error *err =NULL ; - -load_elf_hdr (filename,NULL ,NULL , &err); -if (err) { -error_free (err); -return false; - }else { -return true; - } +return load_elf_hdr (filename,NULL ,NULL ,NULL ); }static void spike_board_init (MachineState *machine)
Signature · 1 line
2.49.0
Re: [PATCH 1/5] hw/core/loader: Make load_elf_hdr() return bool, simplify caller
Quoted 7 lines · Markus Armbruster
Signed-off-by Markus Armbruster<[email protected]>--- include/hw/loader.h | 4 +++- hw/arm/boot.c | 6 +----- hw/core/loader.c | 8 ++++++-- hw/riscv/spike.c | 10 +--------- 4 files changed, 11 insertions(+), 17 deletions(-)
r~
Re: [PATCH 1/5] hw/core/loader: Make load_elf_hdr() return bool, simplify caller
In reply to Markus ArmbrusterQuoted 1 line · Markus Armbruster
Signed-off-by Markus Armbruster <[email protected]>
Quoted 12 lines
--- include/hw/loader.h | 4 +++- hw/arm/boot.c | 6 +----- hw/core/loader.c | 8 ++++++-- hw/riscv/spike.c | 10 +--------- 4 files changed, 11 insertions(+), 17 deletions(-)include/hw/loader.h +0
-0
include/hw/loader.h ssize_t load_elf(const char *filename, · line 192
Quoted 5 lines
include/hw/loader.h +2
-0@@ -188,8 +188,10 @@ ssize_t load_elf(const char *filename,* * Inspect an ELF file's header. Read its full header contents into a * buffer and/or determine if the ELF is 64bit. +* +* Returns true on success, false on failure.
I don't really care, but IMO, it's obvious contract for bool+errp functions, not worth a comment.
Quoted 6 lines
include/hw/loader.h +1
-1*/ -void load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp); +bool load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp);ssize_t load_aout (const char *filename,hwaddr addr,int max_sz,bool big_endian,hwaddr target_page_size);
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 1/5] hw/core/loader: Make load_elf_hdr() return bool, simplify caller
Quoted 15 lines · Vladimir Sementsov-Ogievskiy
Quoted 1 line · Markus Armbruster
Signed-off-by Markus Armbruster <[email protected]>Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>Quoted 10 lines
--- include/hw/loader.h | 4 +++- hw/arm/boot.c | 6 +----- hw/core/loader.c | 8 ++++++-- hw/riscv/spike.c | 10 +--------- 4 files changed, 11 insertions(+), 17 deletions(-)include/hw/loader.h +0
-0
Quoted 8 lines
include/hw/loader.h ssize_t load_elf(const char *filename, · line 192
Quoted 6 lines
include/hw/loader.h +2
-0@@ -188,8 +188,10 @@ ssize_t load_elf(const char *filename,* * Inspect an ELF file's header. Read its full header contents into a * buffer and/or determine if the ELF is 64bit. +* +* Returns true on success, false on failure. I don't really care, but IMO, it's obvious contract for bool+errp functions, not worth a comment.
Nearby function comments all have a "Returns" sentence. I try to blend in :)
Quoted 5 lines
Quoted 5 lines
include/hw/loader.h +1
-1*/ -void load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp); +bool load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp);ssize_t load_aout (const char *filename,hwaddr addr,int max_sz,bool big_endian,hwaddr target_page_size);
Re: [PATCH 1/5] hw/core/loader: Make load_elf_hdr() return bool, simplify caller
Quoted 26 lines · Markus Armbruster
Quoted 15 lines · Vladimir Sementsov-Ogievskiy
Quoted 1 line · Markus Armbruster
Signed-off-by Markus Armbruster <[email protected]>Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>Quoted 10 lines
--- include/hw/loader.h | 4 +++- hw/arm/boot.c | 6 +----- hw/core/loader.c | 8 ++++++-- hw/riscv/spike.c | 10 +--------- 4 files changed, 11 insertions(+), 17 deletions(-)include/hw/loader.h +0
-0Quoted 8 lines
include/hw/loader.h ssize_t load_elf(const char *filename, · line 192
Quoted 6 lines
include/hw/loader.h +2
-0@@ -188,8 +188,10 @@ ssize_t load_elf(const char *filename,* * Inspect an ELF file's header. Read its full header contents into a * buffer and/or determine if the ELF is 64bit. +* +* Returns true on success, false on failure. I don't really care, but IMO, it's obvious contract for bool+errp functions, not worth a comment.
Quoted 2 lines
Nearby function comments all have a "Returns" sentence. I try to blend in :)
New developers might just have to look at a particular API such this loader one, without knowing the global errp contract. With that in mind, I'll documents @return everywhere.
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
include/hw/loader.h +1
-1*/ -void load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp); +bool load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp);ssize_t load_aout (const char *filename,hwaddr addr,int max_sz,bool big_endian,hwaddr target_page_size);
Re: [PATCH 1/5] hw/core/loader: Make load_elf_hdr() return bool, simplify caller
In reply to Markus ArmbrusterQuoted 1 line · Markus Armbruster
Signed-off-by Markus Armbruster <[email protected]>
Signature · 1 line
Peter Xu
Re: [PATCH 1/5] hw/core/loader: Make load_elf_hdr() return bool, simplify caller
In reply to Markus ArmbrusterQuoted 2 lines · Markus Armbruster
Signed-off-by Markus Armbruster <[email protected]>---
Nice cleanup
Quoted 101 lines
include/hw/loader.h | 4 +++- hw/arm/boot.c | 6 +----- hw/core/loader.c | 8 ++++++-- hw/riscv/spike.c | 10 +--------- 4 files changed, 11 insertions(+), 17 deletions(-)include/hw/loader.h +3
-1@@ -188,8 +188,10 @@ ssize_t load_elf(const char *filename,* * Inspect an ELF file's header. Read its full header contents into a * buffer and/or determine if the ELF is 64bit. +* +* Returns true on success, false on failure. */ -void load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp); +bool load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp);ssize_t load_aout (const char *filename,hwaddr addr,int max_sz,bool big_endian,hwaddr target_page_size);hw/arm/boot.c +1
-5@@ -766,16 +766,12 @@ static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,int data_swab =0 ;int elf_data_order;ssize_t ret; -Error *err =NULL ; - -load_elf_hdr (info->kernel_filename, &elf_header, &elf_is64, &err); -if (err) { +if (!load_elf_hdr (info->kernel_filename, &elf_header, &elf_is64,NULL )) {/* * If the file is not an ELF file we silently return. * The caller will fall back to try other formats. */ -error_free (err);return -1 ; }hw/core/loader.c +6
-2@@ -364,8 +364,9 @@ const char *load_elf_strerror(ssize_t error) } } -void load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp) +bool load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp) { +bool ok = false;int fd;uint8_t e_ident_local[EI_NIDENT ];uint8_t *e_ident;@@ -380,7 +381,7 @@ void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp) fd =open (filename,O_RDONLY |O_BINARY );if (fd <0 ) {error_setg_errno (errp, errno,"Failed to open file: %s" , filename); -return ; +return false; }if (read (fd, hdr,EI_NIDENT ) !=EI_NIDENT ) {error_setg_errno (errp, errno,"Failed to read file: %s" , filename);@@ -415,8 +416,11 @@ void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp) off += br; } + ok = true; + fail:close (fd); +return ok; }/* return < 0 if error, otherwise the number of bytes loaded in memory */ hw/riscv/spike.c +1
-9@@ -180,15 +180,7 @@ static void create_fdt(SpikeState *s, const MemMapEntry *memmap,static bool spike_test_elf_image (char *filename) { -Error *err =NULL ; - -load_elf_hdr (filename,NULL ,NULL , &err); -if (err) { -error_free (err); -return false; - }else { -return true; - } +return load_elf_hdr (filename,NULL ,NULL ,NULL ); }static void spike_board_init (MachineState *machine)
Re: [PATCH 1/5] hw/core/loader: Make load_elf_hdr() return bool, simplify caller
Quoted 104 lines · Daniel Henrique Barboza
Quoted 2 lines · Markus Armbruster
Signed-off-by Markus Armbruster <[email protected]>---
Nice cleanup
Reviewed-by Daniel Henrique Barboza <[email protected]>Quoted 95 lines
include/hw/loader.h | 4 +++- hw/arm/boot.c | 6 +----- hw/core/loader.c | 8 ++++++-- hw/riscv/spike.c | 10 +--------- 4 files changed, 11 insertions(+), 17 deletions(-)include/hw/loader.h +3
-1@@ -188,8 +188,10 @@ ssize_t load_elf(const char *filename,* * Inspect an ELF file's header. Read its full header contents into a * buffer and/or determine if the ELF is 64bit. +* +* Returns true on success, false on failure. */ -void load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp); +bool load_elf_hdr (const char *filename,void *hdr,bool *is64,Error **errp); ssize_t load_aout(const char *filename, hwaddr addr, int max_sz,bool big_endian, hwaddr target_page_size);hw/arm/boot.c +1
-4@@ -766,16 +766,12 @@ static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry, int data_swab = 0;int elf_data_order;ssize_t ret; -Error *err =NULL ; - -load_elf_hdr (info->kernel_filename, &elf_header, &elf_is64, &err); -if (err) { + if (!load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, NULL)) {/* * If the file is not an ELF file we silently return. * The caller will fall back to try other formats. */ -error_free (err);return -1 ; } diff --git a/hw/core/loader.c b/hw/core/loader.cindex
590c5b02aa..10687fe1c8100644hw/core/loader.c +3
-1@@ -364,8 +364,9 @@ const char *load_elf_strerror(ssize_t error) } } -void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp) +bool load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp) { + bool ok = false;int fd;uint8_t e_ident_local[EI_NIDENT ];uint8_t *e_ident;@@ -380,7 +381,7 @@ void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp) fd =open (filename,O_RDONLY |O_BINARY );if (fd <0 ) {error_setg_errno (errp, errno,"Failed to open file: %s" , filename); -return ; +return false; }if (
read(fd, hdr, EI_NIDENT)!=EI_NIDENT) {
error_setg_errno(errp, errno, "Failed to read file: %s",
filename);hw/core/loader.c +2
-0@@ -415,8 +416,11 @@ void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp) off += br; } + ok = true; + fail:close (fd); +return ok; }/* return < 0 if error, otherwise the number of bytes loaded in memory */
hw/riscv/spike.c +0
-2@@ -180,15 +180,7 @@ static void create_fdt(SpikeState *s, const MemMapEntry *memmap, static bool spike_test_elf_image(char *filename) { - Error *err = NULL; -
Quoted 8 lines
hw/riscv/spike.c static void create_fdt(SpikeState *s, const MemMapEntry *memmap, · line 183
Quoted 8 lines
hw/riscv/spike.c +1
-7-load_elf_hdr (filename,NULL ,NULL , &err); -if (err) { -error_free (err); -return false; - }else { -return true; - } +return load_elf_hdr (filename,NULL ,NULL ,NULL );
Does it worth to keep this function or could just be inlined at the two callers now that it's equivalent with load_elf_hdr?
Regards,
BALATON Zoltan
Re: [PATCH 1/5] hw/core/loader: Make load_elf_hdr() return bool, simplify caller
Quoted 9 lines · BALATON Zoltan
Quoted 8 lines · Daniel Henrique Barboza
Quoted 2 lines · Markus Armbruster
Signed-off-by Markus Armbruster <[email protected]>---
Nice cleanup
Reviewed-by Daniel Henrique Barboza <[email protected]>
[...]
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
hw/riscv/spike.c +0
-5@@ -180,15 +180,7 @@ static void create_fdt(SpikeState *s, const MemMapEntry *memmap, static bool spike_test_elf_image(char *filename) { - Error *err = NULL; - -load_elf_hdr (filename,NULL ,NULL , &err); -if (err) { -error_free (err);
Quoted 8 lines
Quoted 5 lines
hw/riscv/spike.c static void create_fdt(SpikeState *s, const MemMapEntry *memmap, · line 183
Quoted 5 lines
hw/riscv/spike.c +1
-4-return false; - }else { -return true; - } +return load_elf_hdr (filename,NULL ,NULL ,NULL );Does it worth to keep this function or could just be inlined at the two callers now that it's equivalent with
load_elf_hdr?
Palmer, Alistair, Daniel, got a preference?
Re: [PATCH 1/5] hw/core/loader: Make load_elf_hdr() return bool, simplify caller
In reply to Markus ArmbrusterQuoted 5 lines · Markus Armbruster
Date: Wed, 19 Nov 2025 14:08:51 +0100
From: Markus Armbruster <[email protected]>
Subject: [PATCH 1/5] hw/core/loader: Makeload_elf_hdr()return bool,
simplify caller
Quoted 7 lines
Signed-off-by Markus Armbruster <[email protected]>--- include/hw/loader.h | 4 +++- hw/arm/boot.c | 6 +----- hw/core/loader.c | 8 ++++++-- hw/riscv/spike.c | 10 +--------- 4 files changed, 11 insertions(+), 17 deletions(-)
PATCH 2/5 hw/nvram/xlnx-bbram: More idiomatic and simpler error reporting
In reply to Markus Armbrusterbbram_bdrv_error() interpolates a "detail" string into a template with error_setg_errno(), then reports the result with error_report(). Produces error messages with an unwanted '.':
BLK-NAME: BBRAM backstore DETAIL failed.: STERROR
Replace both calls of bbram_bdrv_error() by straightforward error_report(), and drop the function. This is less code, easier to read, and the error message is more greppable.
Also delete the unwanted '.'.
--- hw/nvram/xlnx-bbram.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-)
hw/nvram/xlnx-bbram.c +4 -14
@@ -88,18 +88,6 @@ static bool bbram_pgm_enabled(XlnxBBRam *s)return ARRAY_FIELD_EX32 (s->regs,BBRAM_STATUS ,PGM_MODE ) !=0 ; } -static void bbram_bdrv_error (XlnxBBRam *s,int rc,gchar *detail) -{ -Error *errp =NULL ; - -error_setg_errno (&errp, -rc,"%s: BBRAM backstore %s failed." , -blk_name (s->blk), detail); -error_report ("%s" ,error_get_pretty (errp)); -error_free (errp); - -g_free (detail); -} -static void bbram_bdrv_read (XlnxBBRam *s,Error **errp) {uint32_t *ram = &s->regs[R_BBRAM_0 ];
@@ -162,7 +150,8 @@ static void bbram_bdrv_sync(XlnxBBRam *s, uint64_t hwaddr) offset = hwaddr -A_BBRAM_0 ; rc =blk_pwrite (s->blk, offset,4 , &le32,0 );if (rc <0 ) { -bbram_bdrv_error (s, rc,g_strdup_printf ("write to offset %u" , offset)); +error_report ("%s: BBRAM backstore write to offset %u failed: %s" , +blk_name (s->blk), offset,strerror (-rc)); } }
@@ -178,7 +167,8 @@ static void bbram_bdrv_zero(XlnxBBRam *s) rc =blk_make_zero (s->blk,0 );if (rc <0 ) { -bbram_bdrv_error (s, rc,g_strdup ("zeroizing" )); +error_report ("%s: BBRAM backstore zeroizing failed: %s" , +blk_name (s->blk),strerror (-rc)); }/* Restore bbram8 if it is non-zero */
Signature · 1 line
2.49.0
Re: [PATCH 2/5] hw/nvram/xlnx-bbram: More idiomatic and simpler error reporting
Quoted 11 lines · Markus Armbruster
bbram_bdrv_error()interpolates a "detail" string into a template witherror_setg_errno(), then reports the result witherror_report(). Produces error messages with an unwanted '.':BLK-NAME: BBRAM backstore DETAIL failed.: STERROR
Replace both calls of
bbram_bdrv_error()by straightforwarderror_report(), and drop the function. This is less code, easier to read, and the error message is more greppable.Also delete the unwanted '.'.
Also, using "errp" name for local "Error *" (one star) variable is a bit misleading.
Quoted 2 lines
Signed-off-by Markus Armbruster <[email protected]>
Quoted 47 lines
--- hw/nvram/xlnx-bbram.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-)hw/nvram/xlnx-bbram.c +4
-14@@ -88,18 +88,6 @@ static bool bbram_pgm_enabled(XlnxBBRam *s)return ARRAY_FIELD_EX32 (s->regs,BBRAM_STATUS ,PGM_MODE ) !=0 ; } -static void bbram_bdrv_error (XlnxBBRam *s,int rc,gchar *detail) -{ -Error *errp =NULL ; - -error_setg_errno (&errp, -rc,"%s: BBRAM backstore %s failed." , -blk_name (s->blk), detail); -error_report ("%s" ,error_get_pretty (errp)); -error_free (errp); - -g_free (detail); -} -static void bbram_bdrv_read (XlnxBBRam *s,Error **errp) {uint32_t *ram = &s->regs[R_BBRAM_0 ];@@ -162,7 +150,8 @@ static void bbram_bdrv_sync(XlnxBBRam *s, uint64_t hwaddr) offset = hwaddr -A_BBRAM_0 ; rc =blk_pwrite (s->blk, offset,4 , &le32,0 );if (rc <0 ) { -bbram_bdrv_error (s, rc,g_strdup_printf ("write to offset %u" , offset)); +error_report ("%s: BBRAM backstore write to offset %u failed: %s" , +blk_name (s->blk), offset,strerror (-rc)); } }@@ -178,7 +167,8 @@ static void bbram_bdrv_zero(XlnxBBRam *s) rc =blk_make_zero (s->blk,0 );if (rc <0 ) { -bbram_bdrv_error (s, rc,g_strdup ("zeroizing" )); +error_report ("%s: BBRAM backstore zeroizing failed: %s" , +blk_name (s->blk),strerror (-rc)); }/* Restore bbram8 if it is non-zero */
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 2/5] hw/nvram/xlnx-bbram: More idiomatic and simpler error reporting
Quoted 7 lines · Vladimir Sementsov-Ogievskiy
Quoted 6 lines · Markus Armbruster
bbram_bdrv_error()interpolates a "detail" string into a template witherror_setg_errno(), then reports the result witherror_report(). Produces error messages with an unwanted '.':BLK-NAME: BBRAM backstore DETAIL failed.: STERROR
Quoted 7 lines
Quoted 5 lines
Replace both calls of
bbram_bdrv_error()by straightforwarderror_report(), and drop the function. This is less code, easier to read, and the error message is more greppable.Also delete the unwanted '.'.
Also, using "errp" name for local "Error *" (one star) variable is a bit misleading.
True. I don't mention it, because I delete the variable.
My search for misleading uses of @errp led me here.
Quoted 3 lines
Quoted 1 line
Signed-off-by Markus Armbruster <[email protected]>Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>
Thanks!
Re: [PATCH 2/5] hw/nvram/xlnx-bbram: More idiomatic and simpler error reporting
In reply to Markus ArmbrusterQuoted 6 lines · Markus Armbruster
bbram_bdrv_error()interpolates a "detail" string into a template witherror_setg_errno(), then reports the result witherror_report(). Produces error messages with an unwanted '.':BLK-NAME: BBRAM backstore DETAIL failed.: STERROR
Quoted 7 lines
Replace both calls of
bbram_bdrv_error()by straightforwarderror_report(), and drop the function. This is less code, easier to read, and the error message is more greppable.Also delete the unwanted '.'.
Signed-off-by Markus Armbruster <[email protected]>
Signature · 1 line
Peter Xu
Re: [PATCH 2/5] hw/nvram/xlnx-bbram: More idiomatic and simpler error reporting
In reply to Markus ArmbrusterQuoted 9 lines · Markus Armbruster
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
bbram_bdrv_error()interpolates a "detail" string into a template witherror_setg_errno(), then reports the result witherror_report(). Produces error messages with an unwanted '.':BLK-NAME: BBRAM backstore DETAIL failed.: STERROR
Quoted 7 lines
Replace both calls of
bbram_bdrv_error()by straightforwarderror_report(), and drop the function. This is less code, easier to read, and the error message is more greppable.Also delete the unwanted '.'.
Signed-off-by Markus Armbruster <[email protected]>
Quoted 51 lines
--- hw/nvram/xlnx-bbram.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-)hw/nvram/xlnx-bbram.c +4
-15@@ -88,18 +88,6 @@ static bool bbram_pgm_enabled(XlnxBBRam *s)return ARRAY_FIELD_EX32 (s->regs,BBRAM_STATUS ,PGM_MODE ) !=0 ; } -static void bbram_bdrv_error (XlnxBBRam *s,int rc,gchar *detail) -{ -Error *errp =NULL ; - -error_setg_errno (&errp, -rc,"%s: BBRAM backstore %s failed." , -blk_name (s->blk), detail); -error_report ("%s" ,error_get_pretty (errp)); -error_free (errp); - -g_free (detail); -} -static void bbram_bdrv_read (XlnxBBRam *s,Error **errp) {uint32_t *ram = &s->regs[R_BBRAM_0 ];@@ -162,7 +150,8 @@ static void bbram_bdrv_sync(XlnxBBRam *s, uint64_t hwaddr) offset = hwaddr -A_BBRAM_0 ; rc =blk_pwrite (s->blk, offset,4 , &le32,0 );if (rc <0 ) { -bbram_bdrv_error (s, rc,g_strdup_printf ("write to offset %u" , offset)); +error_report ("%s: BBRAM backstore write to offset %u failed: %s" , +blk_name (s->blk), offset,strerror (-rc)); } }@@ -178,7 +167,8 @@ static void bbram_bdrv_zero(XlnxBBRam *s) rc =blk_make_zero (s->blk,0 );if (rc <0 ) { -bbram_bdrv_error (s, rc,g_strdup ("zeroizing" )); +error_report ("%s: BBRAM backstore zeroizing failed: %s" , +blk_name (s->blk),strerror (-rc)); }/* Restore bbram8 if it is non-zero */ --2.49.0
Re: [PATCH 2/5] hw/nvram/xlnx-bbram: More idiomatic and simpler error reporting
In reply to Markus ArmbrusterQuoted 15 lines · Markus Armbruster
Date: Wed, 19 Nov 2025 14:08:52 +0100
From: Markus Armbruster <[email protected]>
Subject: [PATCH 2/5] hw/nvram/xlnx-bbram: More idiomatic and simpler error
reporting
bbram_bdrv_error()interpolates a "detail" string into a template witherror_setg_errno(), then reports the result witherror_report(). Produces error messages with an unwanted '.':BLK-NAME: BBRAM backstore DETAIL failed.: STERROR
Replace both calls of
bbram_bdrv_error()by straightforwarderror_report(), and drop the function. This is less code, easier to read, and the error message is more greppable.
Quoted 6 lines
Also delete the unwanted '.'.
Signed-off-by Markus Armbruster <[email protected]>--- hw/nvram/xlnx-bbram.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-)
PATCH 3/5 nbd/client-connection: Replace error_propagate() by assignment
In reply to Markus Armbrusterconnect_thread_func() sets a variable to null, then error_propagate()s an Error * to it. This is a roundabout way to assign the Error * to it, so replace it by just that.
--- nbd/client-connection.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
nbd/client-connection.c +1 -2
@@ -207,8 +207,7 @@ static void *connect_thread_func(void *opaque)qemu_mutex_lock (&conn->mutex);error_free (conn->err); - conn->err =NULL ; -error_propagate (&conn->err, local_err); + conn->err = local_err;if (ret <0 ) {object_unref (OBJECT (conn->sioc));
Signature · 1 line
2.49.0
Re: [PATCH 3/5] nbd/client-connection: Replace error_propagate() by assignment
Quoted 5 lines · Markus Armbruster
connect_thread_func()sets a variable to null, then error_propagate()s an Error * to it. This is a roundabout way to assign the Error * to it, so replace it by just that.Signed-off-by Markus Armbruster<[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 3/5] nbd/client-connection: Replace error_propagate() by assignment
In reply to Markus ArmbrusterQuoted 5 lines · Markus Armbruster
connect_thread_func()sets a variable to null, then error_propagate()s an Error * to it. This is a roundabout way to assign the Error * to it, so replace it by just that.Signed-off-by Markus Armbruster <[email protected]>
Signature · 1 line
Peter Xu
Re: [PATCH 3/5] nbd/client-connection: Replace error_propagate() by assignment
In reply to Markus ArmbrusterQuoted 5 lines · Markus Armbruster
Date: Wed, 19 Nov 2025 14:08:53 +0100
From: Markus Armbruster <[email protected]>
Subject: [PATCH 3/5] nbd/client-connection: Replaceerror_propagate()by
assignment
Quoted 8 lines
connect_thread_func()sets a variable to null, then error_propagate()s an Error * to it. This is a roundabout way to assign the Error * to it, so replace it by just that.Signed-off-by Markus Armbruster <[email protected]>--- nbd/client-connection.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
PATCH 4/5 error: error_free(NULL) is safe, drop unnecessary conditionals
In reply to Markus Armbruster--- hw/acpi/pcihp.c | 4 +--- io/channel-websock.c | 4 +--- io/task.c | 4 +--- migration/migration.c | 6 ++---- tests/unit/test-smp-parse.c | 5 +---- 5 files changed, 6 insertions(+), 17 deletions(-)
hw/acpi/pcihp.c +1 -3
@@ -62,9 +62,7 @@ static int acpi_pcihp_get_bsel(PCIBus *bus) &local_err);if (local_err || bsel >=ACPI_PCIHP_MAX_HOTPLUG_BUS ) { -if (local_err) { -error_free (local_err); - } +error_free (local_err);return -1 ; }else {return bsel;
io/channel-websock.c +1 -3
@@ -932,9 +932,7 @@ static void qio_channel_websock_finalize(Object *obj)if (ioc->io_tag) {g_source_remove (ioc->io_tag); } -if (ioc->io_err) { -error_free (ioc->io_err); - } +error_free (ioc->io_err);object_unref (OBJECT (ioc->master)); }
io/task.c +1 -3
@@ -91,9 +91,7 @@ static void qio_task_free(QIOTask *task)if (task->destroyResult) { task->destroyResult (task->result); } -if (task->err) { -error_free (task->err); - } +error_free (task->err);object_unref (task->source);qemu_mutex_unlock (&task->thread_lock);
migration/migration.c +2 -4
@@ -1569,10 +1569,8 @@ bool migrate_has_error(MigrationState *s)static void migrate_error_free (MigrationState *s) {QEMU_LOCK_GUARD (&s->error_mutex); -if (s->error) { -error_free (s->error); - s->error =NULL ; - } +error_free (s->error); + s->error =NULL ; }static void migration_connect_set_error (MigrationState *s,const Error *error)
tests/unit/test-smp-parse.c +1 -4
@@ -875,10 +875,7 @@ static void check_parse(MachineState *ms, const SMPConfiguration *config, config_str, expect_err, output_topo_str); end: -if (err !=NULL ) { -error_free (err); - } - +error_free (err);abort (); }
Signature · 1 line
2.49.0
Re: [PATCH 4/5] error: error_free(NULL) is safe, drop unnecessary conditionals
Quoted 8 lines · Markus Armbruster
Signed-off-by Markus Armbruster<[email protected]>--- hw/acpi/pcihp.c | 4 +--- io/channel-websock.c | 4 +--- io/task.c | 4 +--- migration/migration.c | 6 ++---- tests/unit/test-smp-parse.c | 5 +---- 5 files changed, 6 insertions(+), 17 deletions(-)
r~
Re: [PATCH 4/5] error: error_free(NULL) is safe, drop unnecessary conditionals
In reply to Markus ArmbrusterQuoted 1 line · Markus Armbruster
Signed-off-by Markus Armbruster<[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 4/5] error: error_free(NULL) is safe, drop unnecessary conditionals
In reply to Markus ArmbrusterQuoted 1 line · Markus Armbruster
Signed-off-by Markus Armbruster <[email protected]>
Signature · 1 line
Peter Xu
Re: [PATCH 4/5] error: error_free(NULL) is safe, drop unnecessary conditionals
In reply to Markus ArmbrusterQuoted 5 lines · Markus Armbruster
Date: Wed, 19 Nov 2025 14:08:54 +0100
From: Markus Armbruster <[email protected]>
Subject: [PATCH 4/5] error:error_free(NULL)is safe, drop unnecessary
conditionals
Quoted 8 lines
Signed-off-by Markus Armbruster <[email protected]>--- hw/acpi/pcihp.c | 4 +--- io/channel-websock.c | 4 +--- io/task.c | 4 +--- migration/migration.c | 6 ++---- tests/unit/test-smp-parse.c | 5 +---- 5 files changed, 6 insertions(+), 17 deletions(-)
PATCH 5/5 error: Consistently name Error * objects err, and not errp
In reply to Markus ArmbrusterThis touches code in xen_enable_tpm() that is obviously wrong. Since I don't know how to fix it properly, I'm adding a FIXME there.
--- block/crypto.c | 8 ++++---- hw/acpi/ghes.c | 8 ++++---- hw/ppc/spapr.c | 16 ++++++++-------- hw/xen/xen-pvh-common.c | 13 ++++++++++--- nbd/common.c | 6 +++--- 5 files changed, 29 insertions(+), 22 deletions(-)
block/crypto.c +4 -4
@@ -938,14 +938,14 @@ static void GRAPH_RDLOCK block_crypto_amend_cleanup(BlockDriverState *bs) {BlockCrypto *crypto = bs->opaque; -Error *errp =NULL ; +Error *err =NULL ;/* release exclusive read/write permissions to the underlying file */ crypto->updating_keys = false; -bdrv_child_refresh_perms (bs, bs->file, &errp); +bdrv_child_refresh_perms (bs, bs->file, &err); -if (errp) { -error_report_err (errp); +if (err) { +error_report_err (err); } }
hw/acpi/ghes.c +4 -4
@@ -563,7 +563,7 @@ int acpi_ghes_memory_errors(AcpiGhesState *ags, uint16_t source_id,const uint8_t guid[] =UUID_LE (0xA5BC1114 ,0x6F64 ,0x4EDE ,0xB8 ,0x63 ,0x3E ,0x83 , \0xED ,0x7C ,0x83 ,0xB1 ); -Error *errp =NULL ; +Error *err =NULL ;int data_length;GArray *block;
@@ -583,12 +583,12 @@ int acpi_ghes_memory_errors(AcpiGhesState *ags, uint16_t source_id,acpi_ghes_build_append_mem_cper (block, physical_address);/* Report the error */ -ghes_record_cper_errors (ags, block->data, block->len, source_id, &errp); +ghes_record_cper_errors (ags, block->data, block->len, source_id, &err);g_array_free (block, true); -if (errp) { -error_report_err (errp); +if (err) { +error_report_err (err);return -1 ; }
hw/ppc/spapr.c +8 -8
@@ -2847,7 +2847,7 @@ static void spapr_machine_init(MachineState *machine)int i;MemoryRegion *sysmem =get_system_memory ();long load_limit, fw_size; -Error *errp =NULL ; +Error *err =NULL ;NICInfo *nd;if (!filename) {
@@ -2871,7 +2871,7 @@ static void spapr_machine_init(MachineState *machine)/* Determine capabilities to run with */ spapr_caps_init (spapr); -kvmppc_check_papr_resize_hpt (&errp); +kvmppc_check_papr_resize_hpt (&err);if (spapr->resize_hpt ==SPAPR_RESIZE_HPT_DEFAULT ) {/* * If the user explicitly requested a mode we should either
@@ -2879,10 +2879,10 @@ static void spapr_machine_init(MachineState *machine)* it's not set explicitly, we reset our mode to something * that works */ -if (errp) { +if (err) { spapr->resize_hpt =SPAPR_RESIZE_HPT_DISABLED ; -error_free (errp); - errp =NULL ; +error_free (err); + err =NULL ; }else { spapr->resize_hpt = smc->resize_hpt_default; }
@@ -2890,14 +2890,14 @@ static void spapr_machine_init(MachineState *machine)assert (spapr->resize_hpt !=SPAPR_RESIZE_HPT_DEFAULT ); -if ((spapr->resize_hpt !=SPAPR_RESIZE_HPT_DISABLED ) && errp) { +if ((spapr->resize_hpt !=SPAPR_RESIZE_HPT_DISABLED ) && err) {/* * User requested HPT resize, but this host can't supply it. Bail out */ -error_report_err (errp); +error_report_err (err);exit (1 ); } -error_free (errp); +error_free (err); spapr->rma_size =spapr_rma_size (spapr, &error_fatal);
hw/xen/xen-pvh-common.c +10 -3
@@ -101,7 +101,7 @@ static void xen_create_virtio_mmio_devices(XenPVHMachineState *s)#ifdef CONFIG_TPM static void xen_enable_tpm (XenPVHMachineState *s) { -Error *errp =NULL ; +Error *err =NULL ;DeviceState *dev;SysBusDevice *busdev;
@@ -111,8 +111,15 @@ static void xen_enable_tpm(XenPVHMachineState *s)return ; } dev =qdev_new (TYPE_TPM_TIS_SYSBUS ); -object_property_set_link (OBJECT (dev),"tpmdev" ,OBJECT (be), &errp); -object_property_set_str (OBJECT (dev),"tpmdev" , be->id, &errp); +/* +* FIXME This use of &err is is wrong. If both calls fail, the +* second will trip error_setv()'s assertion. If just one call +* fails, we leak an Error object. Setting the same property +* twice (first to a QOM path, then to an ID string) is almost +* certainly wrong, too. +*/ +object_property_set_link (OBJECT (dev),"tpmdev" ,OBJECT (be), &err); +object_property_set_str (OBJECT (dev),"tpmdev" , be->id, &err); busdev =SYS_BUS_DEVICE (dev);sysbus_realize_and_unref (busdev, &error_fatal);sysbus_mmio_map (busdev,0 , s->cfg.tpm.base);
nbd/common.c +3 -3
@@ -282,10 +282,10 @@ void nbd_set_socket_send_buffer(QIOChannelSocket *sioc)#ifdef UNIX_STREAM_SOCKET_SEND_BUFFER_SIZE if (sioc->localAddr.ss_family ==AF_UNIX ) {size_t size =UNIX_STREAM_SOCKET_SEND_BUFFER_SIZE ; -Error *errp =NULL ; +Error *err =NULL ; -if (qio_channel_socket_set_send_buffer (sioc, size, &errp) <0 ) { -warn_report_err (errp); +if (qio_channel_socket_set_send_buffer (sioc, size, &err) <0 ) { +warn_report_err (err); } }#endif /* UNIX_STREAM_SOCKET_SEND_BUFFER_SIZE */
Signature · 1 line
2.49.0
Re: [PATCH 5/5] error: Consistently name Error * objects err, and not errp
Quoted 18 lines · Markus Armbruster
hw/xen/xen-pvh-common.c +1
-2@@ -101,7 +101,7 @@ static void xen_create_virtio_mmio_devices(XenPVHMachineState *s)#ifdef CONFIG_TPM static void xen_enable_tpm (XenPVHMachineState *s) { -Error *errp =NULL ; +Error *err =NULL ;DeviceState *dev;SysBusDevice *busdev;@@ -111,8 +111,15 @@ static void xen_enable_tpm(XenPVHMachineState *s)return ; } dev =qdev_new (TYPE_TPM_TIS_SYSBUS ); -object_property_set_link (OBJECT (dev),"tpmdev" ,OBJECT (be), &errp);
hw/xen/xen-pvh-common.c static void xen_enable_tpm(XenPVHMachineState *s) · line 122
Quoted 10 lines
hw/xen/xen-pvh-common.c +9
-1-object_property_set_str (OBJECT (dev),"tpmdev" , be->id, &errp); +/* +* FIXME This use of &err is is wrong. If both calls fail, the +* second will trip error_setv()'s assertion. If just one call +* fails, we leak an Error object. Setting the same property +* twice (first to a QOM path, then to an ID string) is almost +* certainly wrong, too. +*/ +object_property_set_link (OBJECT (dev),"tpmdev" ,OBJECT (be), &err); +object_property_set_str (OBJECT (dev),"tpmdev" , be->id, &err);
To your question, I don't know the answer, but I think it's far more likely that the original author didn't grok the proper use of &errp, than for this behaviour to be intentional.
Surely we just want a failure path and abort the construction if this goes wrong?
~Andrew
Re: [PATCH 5/5] error: Consistently name Error * objects err, and not errp
Quoted 34 lines · Andrew Cooper
Quoted 18 lines · Markus Armbruster
hw/xen/xen-pvh-common.c +1
-2@@ -101,7 +101,7 @@ static void xen_create_virtio_mmio_devices(XenPVHMachineState *s)#ifdef CONFIG_TPM static void xen_enable_tpm (XenPVHMachineState *s) { -Error *errp =NULL ; +Error *err =NULL ;DeviceState *dev;SysBusDevice *busdev;@@ -111,8 +111,15 @@ static void xen_enable_tpm(XenPVHMachineState *s)return ; } dev =qdev_new (TYPE_TPM_TIS_SYSBUS ); -object_property_set_link (OBJECT (dev),"tpmdev" ,OBJECT (be), &errp);hw/xen/xen-pvh-common.c static void xen_enable_tpm(XenPVHMachineState *s) · line 122
Quoted 10 lines
hw/xen/xen-pvh-common.c +9
-1-object_property_set_str (OBJECT (dev),"tpmdev" , be->id, &errp); +/* +* FIXME This use of &err is is wrong. If both calls fail, the +* second will trip error_setv()'s assertion. If just one call +* fails, we leak an Error object. Setting the same property +* twice (first to a QOM path, then to an ID string) is almost +* certainly wrong, too. +*/ +object_property_set_link (OBJECT (dev),"tpmdev" ,OBJECT (be), &err); +object_property_set_str (OBJECT (dev),"tpmdev" , be->id, &err);To your question, I don't know the answer, but I think it's far more likely that the original author didn't grok the proper use of &errp, than for this behaviour to be intentional.
Quoted 2 lines
Surely we just want a failure path and abort the construction if this goes wrong?
In the caller of xen_enable_tpm, we just have error_report+exit calls, so there's no error propagation ability in the call chain.
The caller will also skip xen_enable_tpm unless a TPM was explicitly requested in the config.
Given that, I'm inclined to say that the object_property_set_* calls in xen_enable_tpm should be using &error_abort, as a failure to setup the explicitly requested TPM should be fatal.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 5/5] error: Consistently name Error * objects err, and not errp
Quoted 41 lines · Daniel P. Berrangé
Quoted 34 lines · Andrew Cooper
Quoted 18 lines · Markus Armbruster
hw/xen/xen-pvh-common.c +1
-2@@ -101,7 +101,7 @@ static void xen_create_virtio_mmio_devices(XenPVHMachineState *s)#ifdef CONFIG_TPM static void xen_enable_tpm (XenPVHMachineState *s) { -Error *errp =NULL ; +Error *err =NULL ;DeviceState *dev;SysBusDevice *busdev;@@ -111,8 +111,15 @@ static void xen_enable_tpm(XenPVHMachineState *s)return ; } dev =qdev_new (TYPE_TPM_TIS_SYSBUS ); -object_property_set_link (OBJECT (dev),"tpmdev" ,OBJECT (be), &errp);hw/xen/xen-pvh-common.c static void xen_enable_tpm(XenPVHMachineState *s) · line 122
Quoted 10 lines
hw/xen/xen-pvh-common.c +9
-1-object_property_set_str (OBJECT (dev),"tpmdev" , be->id, &errp); +/* +* FIXME This use of &err is is wrong. If both calls fail, the +* second will trip error_setv()'s assertion. If just one call +* fails, we leak an Error object. Setting the same property +* twice (first to a QOM path, then to an ID string) is almost +* certainly wrong, too. +*/ +object_property_set_link (OBJECT (dev),"tpmdev" ,OBJECT (be), &err); +object_property_set_str (OBJECT (dev),"tpmdev" , be->id, &err);To your question, I don't know the answer, but I think it's far more likely that the original author didn't grok the proper use of &errp, than for this behaviour to be intentional.
Quoted 2 lines
Surely we just want a failure path and abort the construction if this goes wrong?
In the caller of
xen_enable_tpm, we just have error_report+exit calls, so there's no error propagation ability in the call chain.
Quoted 6 lines
The caller will also skip
xen_enable_tpmunless a TPM was explicitly requested in the config.Given that, I'm inclined to say that the object_property_set_* calls in
xen_enable_tpmshould be using&error_abort, as a failure to setup the explicitly requested TPM should be fatal.
I *suspect* that the first call always fails, and the second one always works. If that's the case, the fix is to delete the first call, and pass &error_abort to the second.
Re: [PATCH 5/5] error: Consistently name Error * objects err, and not errp
In reply to Markus ArmbrusterQuoted 8 lines · Markus Armbruster
Date: Wed, 19 Nov 2025 14:08:55 +0100
From: Markus Armbruster <[email protected]>
Subject: [PATCH 5/5] error: Consistently name Error * objects err, and not
errpThis touches code in
xen_enable_tpm()that is obviously wrong. Since I don't know how to fix it properly, I'm adding a FIXME there.
Quoted 8 lines
Signed-off-by Markus Armbruster <[email protected]>--- block/crypto.c | 8 ++++---- hw/acpi/ghes.c | 8 ++++---- hw/ppc/spapr.c | 16 ++++++++-------- hw/xen/xen-pvh-common.c | 13 ++++++++++--- nbd/common.c | 6 +++--- 5 files changed, 29 insertions(+), 22 deletions(-)