linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Haofeng Li <920484857@qq.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	David Hildenbrand <david@redhat.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
	Zi Yan <ziy@nvidia.com>,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-mm@kvack.org, Haofeng Li <13266079573@163.com>,
	Haofeng Li <lihaofeng@kylinos.cn>
Subject: [PATCH] mm: transhuge-stress: fix potential memory leak on realloc failure
Date: Fri, 12 Sep 2025 16:20:24 +0800	[thread overview]
Message-ID: <tencent_CA0C296180B9D75FF6A53D6B4F2130888D05@qq.com> (raw)

From: Haofeng Li <lihaofeng@kylinos.cn>

When realloc() fails in transhuge-stress test, the original code
exits immediately without freeing the previously allocated memory,
causing a memory leak. This patch introduces a temporary pointer
to hold the realloc result, ensuring proper cleanup by freeing
the original map before exiting on allocation failure.

Signed-off-by: Haofeng Li <lihaofeng@kylinos.cn>
---
 tools/testing/selftests/mm/transhuge-stress.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/mm/transhuge-stress.c b/tools/testing/selftests/mm/transhuge-stress.c
index 68201192e37c..cbe86c5b8de0 100644
--- a/tools/testing/selftests/mm/transhuge-stress.c
+++ b/tools/testing/selftests/mm/transhuge-stress.c
@@ -30,7 +30,7 @@ int main(int argc, char **argv)
 	int i = 0;
 	char *name = NULL;
 	double s;
-	uint8_t *map;
+	uint8_t *map, *map_tmp;
 	size_t map_len;
 	int pagemap_fd;
 	int duration = 0;
@@ -107,9 +107,12 @@ int main(int argc, char **argv)
 
 				nr_succeed++;
 				if (idx >= map_len) {
-					map = realloc(map, idx + 1);
-					if (!map)
+					map_tmp = realloc(map, idx + 1);
+					if (!map_tmp) {
+						free(map);
 						ksft_exit_fail_msg("map realloc\n");
+					}
+					map = map_tmp;
 					memset(map + map_len, 0, idx + 1 - map_len);
 					map_len = idx + 1;
 				}
-- 
2.25.1



             reply	other threads:[~2025-09-12  8:26 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-12  8:20 Haofeng Li [this message]
2025-09-12  9:28 ` David Hildenbrand
2025-09-12 10:10   ` Haofeng Li
2025-09-12 11:36     ` David Hildenbrand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tencent_CA0C296180B9D75FF6A53D6B4F2130888D05@qq.com \
    --to=920484857@qq.com \
    --cc=13266079573@163.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@redhat.com \
    --cc=lihaofeng@kylinos.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox