* [PATCH] selftests/memfd:Fix a resource leak
@ 2024-07-10 7:33 Zhu Jun
2024-07-10 16:07 ` Shuah Khan
0 siblings, 1 reply; 2+ messages in thread
From: Zhu Jun @ 2024-07-10 7:33 UTC (permalink / raw)
To: shuah, akpm
Cc: cyphar, jeffxu, sauravshah.31, zhujun2, gthelen, linux-kselftest,
linux-kernel
From a good programming practice perspective, especially in more
complex programs, explicitly freeing allocated memory is a good habit.
Signed-off-by: Zhu Jun <zhujun2@cmss.chinamobile.com>
---
tools/testing/selftests/memfd/memfd_test.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c
index 95af2d78fd31..f842a4aeb47d 100644
--- a/tools/testing/selftests/memfd/memfd_test.c
+++ b/tools/testing/selftests/memfd/memfd_test.c
@@ -661,9 +661,11 @@ static void mfd_assert_grow_write(int fd)
l = pwrite(fd, buf, mfd_def_size * 8, 0);
if (l != (mfd_def_size * 8)) {
printf("pwrite() failed: %m\n");
+ free(buf);
abort();
}
+ free(buf);
mfd_assert_size(fd, mfd_def_size * 8);
}
@@ -685,8 +687,11 @@ static void mfd_fail_grow_write(int fd)
l = pwrite(fd, buf, mfd_def_size * 8, 0);
if (l == (mfd_def_size * 8)) {
printf("pwrite() didn't fail as expected\n");
+ free(buf);
abort();
}
+
+ free(buf);
}
static void mfd_assert_mode(int fd, int mode)
@@ -771,9 +776,11 @@ static pid_t spawn_thread(unsigned int flags, int (*fn)(void *), void *arg)
pid = clone(fn, stack + STACK_SIZE, SIGCHLD | flags, arg);
if (pid < 0) {
printf("clone() failed: %m\n");
+ free(stack);
abort();
}
+ free(stack);
return pid;
}
--
2.17.1
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] selftests/memfd:Fix a resource leak
2024-07-10 7:33 [PATCH] selftests/memfd:Fix a resource leak Zhu Jun
@ 2024-07-10 16:07 ` Shuah Khan
0 siblings, 0 replies; 2+ messages in thread
From: Shuah Khan @ 2024-07-10 16:07 UTC (permalink / raw)
To: Zhu Jun, shuah, akpm
Cc: cyphar, jeffxu, sauravshah.31, gthelen, linux-kselftest,
linux-kernel, Shuah Khan
On 7/10/24 01:33, Zhu Jun wrote:
> From a good programming practice perspective, especially in more
> complex programs, explicitly freeing allocated memory is a good habit.
>
The change looks good to me, however can you elaborate more on what
kind of leak your fixing here?
> Signed-off-by: Zhu Jun <zhujun2@cmss.chinamobile.com>
> ---
> tools/testing/selftests/memfd/memfd_test.c | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c
> index 95af2d78fd31..f842a4aeb47d 100644
> --- a/tools/testing/selftests/memfd/memfd_test.c
> +++ b/tools/testing/selftests/memfd/memfd_test.c
> @@ -661,9 +661,11 @@ static void mfd_assert_grow_write(int fd)
> l = pwrite(fd, buf, mfd_def_size * 8, 0);
> if (l != (mfd_def_size * 8)) {
> printf("pwrite() failed: %m\n");
> + free(buf);
> abort();
> }
>
> + free(buf);
> mfd_assert_size(fd, mfd_def_size * 8);
> }
>
> @@ -685,8 +687,11 @@ static void mfd_fail_grow_write(int fd)
> l = pwrite(fd, buf, mfd_def_size * 8, 0);
> if (l == (mfd_def_size * 8)) {
> printf("pwrite() didn't fail as expected\n");
> + free(buf);
> abort();
> }
> +
> + free(buf);
> }
>
> static void mfd_assert_mode(int fd, int mode)
> @@ -771,9 +776,11 @@ static pid_t spawn_thread(unsigned int flags, int (*fn)(void *), void *arg)
> pid = clone(fn, stack + STACK_SIZE, SIGCHLD | flags, arg);
> if (pid < 0) {
> printf("clone() failed: %m\n");
> + free(stack);
> abort();
> }
>
> + free(stack);
> return pid;
> }
>
thanks,
-- Shuah
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-07-10 16:07 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-10 7:33 [PATCH] selftests/memfd:Fix a resource leak Zhu Jun
2024-07-10 16:07 ` Shuah Khan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®