diff mbox series

selftests/mm: fix memory leak in child_memcmp_fn

Message ID 20230404031234.78272-1-jiangfeng@kylinos.cn
State New
Headers show
Series selftests/mm: fix memory leak in child_memcmp_fn | expand

Commit Message

Feng Jiang April 4, 2023, 3:12 a.m. UTC
The allocated memory should be freed on return.

Signed-off-by: Feng Jiang <jiangfeng@kylinos.cn>
Suggested-by: Ming Xie <xieming@kylinos.cn>
---
 tools/testing/selftests/mm/cow.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/mm/cow.c b/tools/testing/selftests/mm/cow.c
index 0eb2e8180aa5..c0dd2dfca51b 100644
--- a/tools/testing/selftests/mm/cow.c
+++ b/tools/testing/selftests/mm/cow.c
@@ -162,6 +162,10 @@  static int child_memcmp_fn(char *mem, size_t size,
 {
 	char *old = malloc(size);
 	char buf;
+	int ret;
+
+	if (!old)
+		return -ENOMEM;
 
 	/* Backup the original content. */
 	memcpy(old, mem, size);
@@ -172,7 +176,10 @@  static int child_memcmp_fn(char *mem, size_t size,
 		;
 
 	/* See if we still read the old values. */
-	return memcmp(old, mem, size);
+	ret = memcmp(old, mem, size);
+	free(old);
+
+	return ret;
 }
 
 static int child_vmsplice_memcmp_fn(char *mem, size_t size,