selftests/mm: fix memory leak in child_memcmp_fn
Subsystems (3)
PATCH 1/1 selftests/mm: fix memory leak in child_memcmp_fn
The allocated memory should be freed on return.
--- tools/testing/selftests/mm/cow.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/mm/cow.c +8 -1
@@ -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,
Signature · 1 line
2.39.2
Quoted 25 lines · Feng Jiang
The allocated memory should be freed on return.
Signed-off-by Feng Jiang <[email protected]>Suggested-by Ming Xie <[email protected]>--- tools/testing/selftests/mm/cow.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)tools/testing/selftests/mm/cow.c +4
-0@@ -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, ;
tools/testing/selftests/mm/cow.c static int child_memcmp_fn(char *mem, size_t size, · line 185
Quoted 10 lines
tools/testing/selftests/mm/cow.c +4
-1/* 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,
NAK, the whole point of this function is that the child process will exit immediately after executing this function, cleaning up automatically.
Signature · 3 lines
Thanks, David / dhildenb
Quoted 27 lines · David Hildenbrand
Quoted 26 lines · Feng Jiang
The allocated memory should be freed on return.
Signed-off-by Feng Jiang <[email protected]>Suggested-by Ming Xie <[email protected]>---
tools/testing/selftests/mm/cow.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)tools/testing/selftests/mm/cow.c +4
-1@@ -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);
Quoted 9 lines
tools/testing/selftests/mm/cow.c static int child_memcmp_fn(char *mem, size_t size, · line 185
Quoted 6 lines
tools/testing/selftests/mm/cow.c +4
-0+ ret = memcmp(old, mem, size); + free(old); + + return ret; } static int child_vmsplice_memcmp_fn(char *mem, size_t size,NAK, the whole point of this function is that the child process will exit immediately after executing this function, cleaning up automatically.
Hi David, thanks very much for your review and I think you are right.
While the OS provides a cleanup mechanism to underwrite this, I think it makes sense for the application to ensure that its own logic is complete and correct.
Quoted 40 lines · Feng Jiang
Quoted 30 lines · David Hildenbrand
Quoted 29 lines · Feng Jiang
The allocated memory should be freed on return.
Signed-off-by Feng Jiang <[email protected]>Suggested-by Ming Xie <[email protected]>--- tools/testing/selftests/mm/cow.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)tools/testing/selftests/mm/cow.c +6
-1@@ -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);Quoted 9 lines
tools/testing/selftests/mm/cow.c static int child_memcmp_fn(char *mem, size_t size, · line 185
Quoted 4 lines
tools/testing/selftests/mm/cow.c +2
-0+ + return ret; } static int child_vmsplice_memcmp_fn(char *mem, size_t size,NAK, the whole point of this function is that the child process will exit immediately after executing this function, cleaning up automatically.
Quoted 5 lines
Hi David, thanks very much for your review and I think you are right.
While the OS provides a cleanup mechanism to underwrite this, I think it makes sense for the application to ensure that its own logic is complete and correct.
Maybe on a per-testcase basis (which we do), such that each testcase leaves the process in a clean slate for the next test case. But not for such simplistic things where an exit() immediately follows. If we'd want to cleanup any state before we call exit(), we'd end up in a lot of unnecessarily over-complicating the code.
Just look at how many exit()/ksft_exit_fail_msg()/... we have in our test cases. Cleaning up in these cases would be close to madness ;)
Signature · 3 lines
Thanks, David / dhildenb