Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp530664rdh; Sun, 24 Sep 2023 00:10:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEHJf4qsc4jG18KOgY+/M4M2iR8FzUd7fXTbQ5gM1oOVD/gtdP9zQzmX/oJxg1/CULdduNH X-Received: by 2002:aca:d15:0:b0:3ab:8574:e8aa with SMTP id 21-20020aca0d15000000b003ab8574e8aamr5358374oin.25.1695539423824; Sun, 24 Sep 2023 00:10:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695539423; cv=none; d=google.com; s=arc-20160816; b=XZjNchE1ZRavLDRNQ34lYQSM0p2rreoIA+pwhicA2Ik7QdtABq9s5qf7ihSDiA/oLP 2TCE4MlpioYSC50VbDGjXoR7PDMDjvgprSparF/ik0GRWQ2cUqnFdoGv7SogOxcMEMI2 KZpDQ/nAcYRN5s0zIc3G46EoAFNcUKtC4EG8dAw21yGm943zrVGThfP3VKshosdGiNIo UqaRXpaIVbjUi0GHtCft1XtIRsNd9n5P5m/gIfuCoMAUFx+9YKXSvy6h8X6pG47H1leb 0G8WryM64Qu73l9iDTingGnW9/uhhtc48RYjJvXzR2fjk4WPpdvl79fmqRj+8J7qxMu7 ek+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=dcQLWA0BLZ69vvjFwwdHdjp5Y4U2eIKLK0g6iLOyzUg=; fh=/qqo7lApuksAoQE6agOliD0ZW/KthZTtxB+aCkweeA4=; b=uDr2iAEiWs5tRU49g64et0TLCq6PyAkx8JyS/4OY6sGnyANUcqFz37vubs3mWBSczx V32ChG6PtTQQ/VJnotQ5YQ16vyG5RUdpjfL/BGuxLuH9X9rGzH4Bk/egLWFgn/Tgx560 OJ7wnlhSxQNZq65T/sgt5ff0wO+ptmcnOvL6iUELSQZ4ahc0KBq0SFNcA67d9iT8AdVC 1nQz9vGK/7t4QJDeivVpMOFsErQdVXXkq0PLkQxKevML78DFdi8OaG1uPX+Wp9xA8fkR p6Ag64e/T2U5mpbnbQPNxY7Z+aJL1AEqOemObtdNIpQudsg/u7mlVPBhw5MfNNn0Eadm gcbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=LmSboI+m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id u21-20020a656715000000b00565e92e8734si6977005pgf.769.2023.09.24.00.10.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Sep 2023 00:10:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=LmSboI+m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id C6F7C80F5878; Sat, 23 Sep 2023 22:52:24 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229482AbjIXFwD (ORCPT + 99 others); Sun, 24 Sep 2023 01:52:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229437AbjIXFwD (ORCPT ); Sun, 24 Sep 2023 01:52:03 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48E24EE; Sat, 23 Sep 2023 22:51:56 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-68cc1b70e05so1124857b3a.1; Sat, 23 Sep 2023 22:51:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695534716; x=1696139516; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=dcQLWA0BLZ69vvjFwwdHdjp5Y4U2eIKLK0g6iLOyzUg=; b=LmSboI+mUoRsji3R7nYbEVVtz1EzZe4k5BcPC5pbKQD7dX/3gXnpbxH+WQFpQutUxH lMLALthUyTSegpQSykClv4jKa3XW05V1SPmJ0wKx78aPt0Y0QvdXUbNhui7eyBeHMytv C/SVtrgdc4VF/+7A3aN9rCTNLrulDIDOOc5TLziGxw1UIea9Hr9SMyVvmc3iuEQUptmP Jov7kwtUtS2l1Zzc9nLnwutg+IVCvIIoFUzeG2Btdm3AFqvbjw9TaWLz5Iptndtx1fqS FQsCJb4FoyzwLKeU/eLdTfLUmNRfdGbrYGqsilvQWjMER5h4+SJoNWddWcRR+s5JRKgv aBIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695534716; x=1696139516; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dcQLWA0BLZ69vvjFwwdHdjp5Y4U2eIKLK0g6iLOyzUg=; b=pE0Klr5Rp+dC3cA94dx/CNmDow0vDV26cpf9g1HqbQOPQjQkJVV+s4+3R7xNxPIegC WJuoy7A0YGzVt1gHeSKTsIZcDpBurgmX1/FyDadJJdDGersku6zbZM+sCRx78v/gdOIq v90pXHY91W1t8bULivlzGtj5RmiJdBvqm1DlSZnh1ghMqiYC0b2ERekGEbibB6XYY6Rp 6i3qu8Kp1X+idwQIxLaJUb3wC6PWa69JuU3EwYMpHisWqwycSQrCHHFcyKMf8mSOhkI4 1PeWhtubgdWlSxK8TWz/K5EZnBHUkzFWb+eteaV6cGslurYePYaWM1KhTwru7NZEC2Ff XUlA== X-Gm-Message-State: AOJu0YwEt0uetRge2uQMGXJxkmhV6NOH3Rt0O7GfqDnEBMnQFHlIPaoy KATx9AGyg+/nnzgeng1rFFvGysmluhchaoRn X-Received: by 2002:a05:6a21:33a7:b0:13d:7aa3:aa6c with SMTP id yy39-20020a056a2133a700b0013d7aa3aa6cmr6059301pzb.0.1695534715472; Sat, 23 Sep 2023 22:51:55 -0700 (PDT) Received: from localhost.localdomain ([140.116.154.65]) by smtp.gmail.com with ESMTPSA id q9-20020a639809000000b00573f82bb00esm5649060pgd.2.2023.09.23.22.51.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 22:51:54 -0700 (PDT) From: Kuan-Wei Chiu To: kys@microsoft.com Cc: haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH] tools: hv: kvp: fix memory leak in realloc failure handling Date: Sun, 24 Sep 2023 13:51:48 +0800 Message-Id: <20230924055148.1074754-1-visitorckw@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Sat, 23 Sep 2023 22:52:24 -0700 (PDT) In the previous code, there was a memory leak issue where the previously allocated memory was not freed upon a failed realloc operation. This patch addresses the problem by releasing the old memory before setting the pointer to NULL in case of a realloc failure. This ensures that memory is properly managed and avoids potential memory leaks. Signed-off-by: Kuan-Wei Chiu --- tools/hv/hv_kvp_daemon.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c index 27f5e7dfc2f7..af180278d56d 100644 --- a/tools/hv/hv_kvp_daemon.c +++ b/tools/hv/hv_kvp_daemon.c @@ -209,11 +209,13 @@ static void kvp_update_mem_state(int pool) * We have more data to read. */ num_blocks++; - record = realloc(record, alloc_unit * num_blocks); + struct kvp_record *record_tmp = + realloc(record, alloc_unit * num_blocks); - if (record == NULL) { + if (record_tmp == NULL) { syslog(LOG_ERR, "malloc failed"); kvp_release_lock(pool); + free(record); exit(EXIT_FAILURE); } continue; @@ -345,11 +347,15 @@ static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size, */ if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) { /* Need to allocate a larger array for reg entries. */ - record = realloc(record, sizeof(struct kvp_record) * - ENTRIES_PER_BLOCK * (num_blocks + 1)); + struct kvp_record *record_tmp = realloc( + record, sizeof(struct kvp_record) * ENTRIES_PER_BLOCK * + (num_blocks + 1)); - if (record == NULL) + if (record_tmp == NULL) { + free(record); return 1; + } + record = record_tmp; kvp_file_info[pool].num_blocks++; } -- 2.25.1