From 9b67d883adebfb3bbe594ca5c2e3edf1cd77693f Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Sun, 24 May 2020 13:35:54 -0700 Subject: [PATCH] client: Fix two memory leaks in handle_job_opt() 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 --- client.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/client.c b/client.c index 81d23663..134497cb 100644 --- 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); } } -- 2.25.1