From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from integral2.. (unknown [180.246.147.8]) by gnuweeb.org (Postfix) with ESMTPSA id 2C45D7E7B1; Fri, 29 Apr 2022 00:47:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1651193257; bh=wVUYuYjrBClYx4+2eUIdL3VhE7RbWuoQEVepQKvoDok=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Nz2cWU5MeeleRbJK5nYBFxrQ75c5H1Ozo4Ul/stQBVzZJBSddHkeKJFQKrzgCSHGE xU3b5nsU3qGPBg8aH7nc0wdcZozxK5H0tvJDW30+FZ4uWCH1R6C8oa4DVhYmKDSnSQ vndlxpYc2B/TzCFPpJBp5A1GRUepAStvLevSBQWGnl2b8uaFbZl7ZiMGvTF1fux0Fu gvueDdoW7e4z1+vPVQFlKO18YlVnr4/pfv3hpRIZsKvTnf62Vv9kxaduf/b6v4a09Y LSYnByjQhYQZxZHIzchBOsZvEXU2lgS2E9+XJ7A9aSu4QiJ4RApSvj5QfIAvR7z96c bhlQB0EGZpl/A== From: Ammar Faizi To: Jens Axboe Cc: Ammar Faizi , Alviro Iskandar Setiawan , Niklas Cassel , fio Mailing List , GNU/Weeb Mailing List Subject: [PATCH v1 7/8] client: Add ENOMEM handling on `realloc()` calls Date: Fri, 29 Apr 2022 07:47:04 +0700 Message-Id: <20220429004705.260034-8-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220429004705.260034-1-ammarfaizi2@gnuweeb.org> References: <20220429004705.260034-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: From: Ammar Faizi We need to use a temporary variable when calling `realloc()`. If we do: ptr = realloc(ptr, new_size); the `ptr` will be NULL when the `realloc()` hits ENOMEM and the old pointer will be leaked. This fixes several places that do the above `realloc()` call. Signed-off-by: Ammar Faizi --- client.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/client.c b/client.c index 605a3ce5..c93c90eb 100644 --- a/client.c +++ b/client.c @@ -338,9 +338,16 @@ static void __fio_client_add_cmd_option(struct fio_client *client, const char *opt) { int index; + void *tmp; index = client->argc++; - client->argv = realloc(client->argv, sizeof(char *) * client->argc); + tmp = realloc(client->argv, sizeof(char *) * client->argc); + if (!tmp) { + log_err("fio: cannot reallocate client->argv in %s\n", __func__); + return; + } + + client->argv = tmp; client->argv[index] = strdup(opt); dprint(FD_NET, "client: add cmd %d: %s\n", index, opt); } @@ -1535,12 +1542,17 @@ static void handle_start(struct fio_client *client, struct fio_net_cmd *cmd) client->nr_stat = le32_to_cpu(pdu->stat_outputs); if (client->jobs) { + void *tmp; int i; - if (client->opt_lists) + tmp = realloc(client->opt_lists, + client->jobs * sizeof(struct flist_head)); + if (!tmp) { free(client->opt_lists); + return; + } + client->opt_lists = tmp; - client->opt_lists = malloc(client->jobs * sizeof(struct flist_head)); for (i = 0; i < client->jobs; i++) INIT_FLIST_HEAD(&client->opt_lists[i]); } @@ -1750,7 +1762,11 @@ fail: } size += sb.st_size; - rep = realloc(rep, size); + tmp = realloc(rep, size); + if (!tmp) + goto fail; + + rep = tmp; rep->size = cpu_to_le32((uint32_t) sb.st_size); fd = open((char *)pdu->path, O_RDONLY); -- Ammar Faizi