Skip to content

Commit

Permalink
hw/nvram/fw_cfg: Let fw_cfg_add_from_generator() return boolean value
Browse files Browse the repository at this point in the history
Commits b6d7e9b..a43770d simplified the error propagation.
Similarly to commit 6fd5bef "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.

Reported-by: Peter Maydell <[email protected]>
Fixes: Coverity CID 1430396: 'Constant' variable guards dead code (DEADCODE)
Fixes: 6552d87 ("softmmu/vl: Let -fw_cfg option take a 'gen_id' argument")
Reviewed-by: Laszlo Ersek <[email protected]>
Reviewed-by: Markus Armbruster <[email protected]>
Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
philmd committed Jul 21, 2020
1 parent a3ad583 commit 0771951
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
10 changes: 6 additions & 4 deletions hw/nvram/fw_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion include/hw/nvram/fw_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions softmmu/vl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0771951

Please sign in to comment.