client: Fix two memory leaks in handle_job_opt()
authorBart Van Assche <bvanassche@acm.org>
Sun, 24 May 2020 20:35:54 +0000 (13:35 -0700)
committerBart Van Assche <bvanassche@acm.org>
Sun, 24 May 2020 22:11:33 +0000 (15:11 -0700)
Do not leak p if pdu->global != 0.

This is an improvement for a previous attempt to fix handle_job_opt(). See
also commit ebae36a28aee ("client: Fix memory leaks in handle_job_opt()").

Do not leak strdup(pdu->name) when calling json_object_add_value_string().
That function namely (indirectly) duplicates its 'name' argument.

This patch fixes the following Coverity complaint:

CID 169311 (#1 of 1): Resource leak (RESOURCE_LEAK)
9. leaked_storage: Variable p going out of scope leaks the storage it points to.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
client.c

index 81d23663770c7624297c1a9ed4cd3596b731a226..134497cb99c1e5b2dce58a6415106146a7e98661 100644 (file)
--- a/client.c
+++ b/client.c
@@ -1140,7 +1140,6 @@ static void handle_gs(struct fio_client *client, struct fio_net_cmd *cmd)
 static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
 {
        struct cmd_job_option *pdu = (struct cmd_job_option *) cmd->payload;
-       struct print_option *p;
 
        if (!job_opt_object)
                return;
@@ -1149,21 +1148,19 @@ static void handle_job_opt(struct fio_client *client, struct fio_net_cmd *cmd)
        pdu->truncated = le16_to_cpu(pdu->truncated);
        pdu->groupid = le32_to_cpu(pdu->groupid);
 
-       p = malloc(sizeof(*p));
-       p->name = strdup((char *) pdu->name);
-
        if (pdu->global) {
-               json_object_add_value_string(job_opt_object, p->name,
+               json_object_add_value_string(job_opt_object,
+                                            (const char *)pdu->name,
                                             (const char *)pdu->value);
        } else if (client->opt_lists) {
                struct flist_head *opt_list = &client->opt_lists[pdu->groupid];
+               struct print_option *p;
 
+               p = malloc(sizeof(*p));
+               p->name = strdup((const char *)pdu->name);
                p->value = pdu->value[0] ? strdup((const char *)pdu->value) :
                        NULL;
                flist_add_tail(&p->list, opt_list);
-       } else {
-               free(p->name);
-               free(p);
        }
 }