Skip to content

Commit

Permalink
block/gluster: Use g_autofree for string in qemu_gluster_parse_json()
Browse files Browse the repository at this point in the history
In the loop in qemu_gluster_parse_json() we do:

    char *str = NULL;
    for(...) {
        str = g_strdup_printf(...);
        ...
        if (various errors) {
            goto out;
        }
        ...
        g_free(str);
        str = NULL;
    }
    return 0;
out:
    various cleanups;
    g_free(str);
    ...
    return -errno;

Coverity correctly complains that the assignment "str = NULL" at the
end of the loop is unnecessary, because we will either go back to the
top of the loop and overwrite it, or else we will exit the loop and
then exit the function without ever reading str again. The assignment
is there as defensive coding to ensure that str is only non-NULL if
it's a live allocation, so this is intentional.

We can make Coverity happier and simplify the code here by using
g_autofree, since we never need 'str' outside the loop.

Resolves: Coverity CID 1527385
Signed-off-by: Peter Maydell <[email protected]>
Reviewed-by: Kevin Wolf <[email protected]>
Reviewed-by: Philippe Mathieu-Daudé <[email protected]>
Message-ID: <[email protected]>
Reviewed-by: Richard Henderson <[email protected]>
Signed-off-by: Kevin Wolf <[email protected]>
  • Loading branch information
pm215 authored and kevmw committed Oct 22, 2024
1 parent 6f625ce commit 7520070
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions block/gluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,6 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
SocketAddressList **tail;
QDict *backing_options = NULL;
Error *local_err = NULL;
char *str = NULL;
const char *ptr;
int i, type, num_servers;

Expand Down Expand Up @@ -547,7 +546,8 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
tail = &gconf->server;

for (i = 0; i < num_servers; i++) {
str = g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN"%d.", i);
g_autofree char *str = g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN"%d.",
i);
qdict_extract_subqdict(options, &backing_options, str);

/* create opts info from runtime_type_opts list */
Expand Down Expand Up @@ -658,8 +658,6 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,

qobject_unref(backing_options);
backing_options = NULL;
g_free(str);
str = NULL;
}

return 0;
Expand All @@ -668,7 +666,6 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
error_propagate(errp, local_err);
qapi_free_SocketAddress(gsconf);
qemu_opts_del(opts);
g_free(str);
qobject_unref(backing_options);
errno = EINVAL;
return -errno;
Expand Down

0 comments on commit 7520070

Please sign in to comment.