Skip to contentSkip to conversation

selftests/mm/cow: fix minor memory leak in child_vmsplice_memcmp_fn()

PATCH In review Malaya Kumar Rout 2 participants 2 messages Mar 25, 2025 – Mar 26, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/mm/cow: fix minor memory leak in child_vmsplice_memcmp_fn()

Static Analyis for cow.c: error
Exception branch exits without releasing memory 'old' and 'new'

fix the issue by releasing the allocated memory.

---
 tools/testing/selftests/mm/cow.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
tools/testing/selftests/mm/cow.c +9 -4
index 9446673645eb..fdee42850548 100644
@@ -168,18 +168,18 @@ static int child_vmsplice_memcmp_fn(char *mem, size_t size,
 	memcpy(old, mem, size);
 
 	if (pipe(fds) < 0)
-		return -errno;
+		goto out;
 
 	/* Trigger a read-only pin. */
 	transferred = vmsplice(fds[1], &iov, 1, 0);
 	if (transferred < 0)
-		return -errno;
+		goto out;
 	if (transferred == 0)
 		return -EINVAL;
 
 	/* Unmap it from our page tables. */
 	if (munmap(mem, size) < 0)
-		return -errno;
+		goto out;
 
 	/* Wait until the parent modified it. */
 	write(comm_pipes->child_ready[1], "0", 1);
@@ -190,10 +190,15 @@ static int child_vmsplice_memcmp_fn(char *mem, size_t size,
 	for (total = 0; total < transferred; total += cur) {
 		cur = read(fds[0], new + total, transferred - total);
 		if (cur < 0)
-			return -errno;
+			goto out;
 	}
 
 	return memcmp(old, new, transferred);
+
+out:
+	free(old);
+	free(new);
+	return -errno;
 }
 
 typedef int (*child_fn)(char *mem, size_t size, struct comm_pipes *comm_pipes);
Signature · 1 line
2.43.0