hw/nvram/fw_cfg: Let fw_cfg_add_from_generator() return boolean value
v2 cover hw/nvram/fw_cfg: Let fw_cfg_add_from_generator() return boolean value
Since v1:
- Addressed Markus review comments
- Added patch to clarify fw_cfg_add_from_generator (Markus)
Philippe Mathieu-Daudé (2):
hw/nvram/fw_cfg: Simplify fw_cfg_add_from_generator() error
propagation
hw/nvram/fw_cfg: Let fw_cfg_add_from_generator() return boolean value
include/hw/nvram/fw_cfg.h | 8 ++++++-- hw/nvram/fw_cfg.c | 13 +++++++------ softmmu/vl.c | 6 +----- 3 files changed, 14 insertions(+), 13 deletions(-)
Signature · 1 line
2.21.3
PATCH v2 1/2 hw/nvram/fw_cfg: Simplify fw_cfg_add_from_generator() error propagation
Document FWCfgDataGeneratorClass::get_data() return NULL on error, and non-NULL on success. This allow us to simplify fw_cfg_add_from_generator(). Since we don't need a local variable to propagate the error, we can remove the ERRP_GUARD() macro.
--- include/hw/nvram/fw_cfg.h | 4 +++- hw/nvram/fw_cfg.c | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-)
include/hw/nvram/fw_cfg.h +3 -1
@@ -32,7 +32,9 @@ typedef struct FWCfgDataGeneratorClass {* @obj: the object implementing this interface * @errp: pointer to a NULL-initialized error object * -* Returns: reference to a byte array containing the data. +* Returns: reference to a byte array containing the data on success, +* or NULL on error. +* * The caller should release the reference when no longer * required. */
hw/nvram/fw_cfg.c +1 -2
@@ -1035,7 +1035,6 @@ void *fw_cfg_modify_file(FWCfgState *s, const char *filename,void fw_cfg_add_from_generator (FWCfgState *s,const char *filename,const char *gen_id,Error **errp) { -ERRP_GUARD ();FWCfgDataGeneratorClass *klass;GByteArray *array;Object *obj;
@@ -1053,7 +1052,7 @@ void fw_cfg_add_from_generator(FWCfgState *s, const char *filename, } klass =FW_CFG_DATA_GENERATOR_GET_CLASS (obj); array = klass->get_data (obj, errp); -if (*errp) { +if (!array) {return ; } size = array->len;
Signature · 1 line
2.21.3
Re: [PATCH v2 1/2] hw/nvram/fw_cfg: Simplify fw_cfg_add_from_generator() error propagation
Quoted 18 lines · Philippe Mathieu-Daudé
Document
FWCfgDataGeneratorClass::get_data()return NULL on error, and non-NULL on success. This allow us to simplifyfw_cfg_add_from_generator(). Since we don't need a local variable to propagate the error, we can remove theERRP_GUARD()macro.Suggested-by Markus Armbruster <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/hw/nvram/fw_cfg.h | 4 +++- hw/nvram/fw_cfg.c | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-)include/hw/nvram/fw_cfg.h +0
-0
include/hw/nvram/fw_cfg.h typedef struct FWCfgDataGeneratorClass { · line 40
Quoted 10 lines
include/hw/nvram/fw_cfg.h +3
-1@@ -32,7 +32,9 @@ typedef struct FWCfgDataGeneratorClass {* @obj: the object implementing this interface * @errp: pointer to a NULL-initialized error object * -* Returns: reference to a byte array containing the data. +* Returns: reference to a byte array containing the data on success, +* or NULL on error. +* * The caller should release the reference when no longer * required. */
Thanks for clarifying the doc comment!
As far as I can tell, the only implementation is qcrypto_tls_cipher_suites_fw_cfg_gen_data(), which is a trivial wrapper around qcrypto_tls_cipher_suites_get_data(). I checked it conforms.
Quoted 13 lines
hw/nvram/fw_cfg.c +0
-1@@ -1035,7 +1035,6 @@ void *fw_cfg_modify_file(FWCfgState *s, const char *filename,void fw_cfg_add_from_generator (FWCfgState *s,const char *filename,const char *gen_id,Error **errp) { -ERRP_GUARD ();FWCfgDataGeneratorClass *klass;GByteArray *array;Object *obj;
hw/nvram/fw_cfg.c void fw_cfg_add_from_generator(FWCfgState *s, const char *filename, · line 1058
Quoted 8 lines
hw/nvram/fw_cfg.c +1
-1@@ -1053,7 +1052,7 @@ void fw_cfg_add_from_generator(FWCfgState *s, const char *filename, } klass =FW_CFG_DATA_GENERATOR_GET_CLASS (obj); array = klass->get_data (obj, errp); -if (*errp) { +if (!array) {return ; } size = array->len;
PATCH v2 2/2 hw/nvram/fw_cfg: Let fw_cfg_add_from_generator() return boolean value
In reply to Philippe Mathieu-DaudéCommits b6d7e9b66f..a43770df5d simplified the error propagation. Similarly to commit 6fd5bef10b "qom: Make functions taking Error** return bool, not void", let fw_cfg_add_from_generator() return a boolean value, not void.
This allow to simplify parse_fw_cfg() and fixes the error handling issue reported by Coverity (CID 1430396):
In parse_fw_cfg():
Variable assigned once to a constant guards dead code.
Local variable local_err is assigned only once, to a constant value, making it effectively constant throughout its scope. If this is not the intent, examine the logic to see if there is a missing assignment that would make local_err not remain constant.
It's the call of fw_cfg_add_from_generator():
Error *local_err = NULL;
fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp);
if (local_err) {
error_propagate(errp, local_err);
return -1;
}
return 0;If it fails, parse_fw_cfg() sets an error and returns 0, which is wrong. Harmless, because the only caller passes &error_fatal.
---
v2: Added impact assessment (Markus)
--- include/hw/nvram/fw_cfg.h | 4 +++- hw/nvram/fw_cfg.c | 10 ++++++---- softmmu/vl.c | 6 +----- 3 files changed, 10 insertions(+), 10 deletions(-)
include/hw/nvram/fw_cfg.h +3 -1
@@ -304,8 +304,10 @@ void *fw_cfg_modify_file(FWCfgState *s, const char *filename, void *data,* will be used; also, a new entry will be added to the file directory * structure residing at key value FW_CFG_FILE_DIR, containing the item name, * data size, and assigned selector key value. +* +* Returns: %true on success, %false on error. */ -void fw_cfg_add_from_generator (FWCfgState *s,const char *filename, +bool fw_cfg_add_from_generator (FWCfgState *s,const char *filename,const char *gen_id,Error **errp); FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase,
hw/nvram/fw_cfg.c +6 -4
@@ -1032,7 +1032,7 @@ void *fw_cfg_modify_file(FWCfgState *s, const char *filename,return NULL ; } -void fw_cfg_add_from_generator (FWCfgState *s,const char *filename, +bool fw_cfg_add_from_generator (FWCfgState *s,const char *filename,const char *gen_id,Error **errp) {FWCfgDataGeneratorClass *klass;
@@ -1043,20 +1043,22 @@ void fw_cfg_add_from_generator(FWCfgState *s, const char *filename, obj =object_resolve_path_component (object_get_objects_root (), gen_id);if (!obj) {error_setg (errp,"Cannot find object ID '%s'" , gen_id); -return ; +return false; }if (!object_dynamic_cast (obj,TYPE_FW_CFG_DATA_GENERATOR_INTERFACE )) {error_setg (errp,"Object ID '%s' is not a '%s' subclass" , gen_id,TYPE_FW_CFG_DATA_GENERATOR_INTERFACE ); -return ; +return false; } klass =FW_CFG_DATA_GENERATOR_GET_CLASS (obj); array = klass->get_data (obj, errp);if (!array) { -return ; +return false; } size = array->len;fw_cfg_add_file (s, filename,g_byte_array_free (array, TRUE), size); + +return true; }static void fw_cfg_machine_reset (void *opaque)
softmmu/vl.c +1 -5
@@ -2070,11 +2070,7 @@ static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp) size =strlen (str);/* NUL terminator NOT included in fw_cfg blob */ buf =g_memdup (str, size); }else if (nonempty_str (gen_id)) { -Error *local_err =NULL ; - -fw_cfg_add_from_generator (fw_cfg, name, gen_id, errp); -if (local_err) { -error_propagate (errp, local_err); +if (!fw_cfg_add_from_generator (fw_cfg, name, gen_id, errp)) {return -1 ; }return 0 ;
Signature · 1 line
2.21.3
Quoted 9 lines · Philippe Mathieu-Daudé
Since v1:
- Addressed Markus review comments
- Added patch to clarifyfw_cfg_add_from_generator(Markus)Philippe Mathieu-Daudé (2):
hw/nvram/fw_cfg: Simplifyfw_cfg_add_from_generator()error
propagation
hw/nvram/fw_cfg: Letfw_cfg_add_from_generator()return boolean value
Quoted 5 lines
include/hw/nvram/fw_cfg.h | 8 ++++++-- hw/nvram/fw_cfg.c | 13 +++++++------ softmmu/vl.c | 6 +----- 3 files changed, 14 insertions(+), 13 deletions(-)
Thanks for the reviews, patches applied to fw_cfg-next tree.
Phil.