* [PATCH] selftests/timens: Add fclose(proc) to prevent memory leaks
@ 2024-11-28 6:05 liujing
2024-12-04 23:09 ` Shuah Khan
0 siblings, 1 reply; 2+ messages in thread
From: liujing @ 2024-11-28 6:05 UTC (permalink / raw)
To: shuah; +Cc: linux-kselftest, linux-kernel, liujing
If fopen succeeds, the fscanf function is called to read the data.
Regardless of whether fscanf is successful, you need to run
fclose(proc) to prevent memory leaks.
Signed-off-by: liujing <liujing@cmss.chinamobile.com>
diff --git a/tools/testing/selftests/timens/procfs.c b/tools/testing/selftests/timens/procfs.c
index 1833ca97eb24..e47844a73c31 100644
--- a/tools/testing/selftests/timens/procfs.c
+++ b/tools/testing/selftests/timens/procfs.c
@@ -79,9 +79,11 @@ static int read_proc_uptime(struct timespec *uptime)
if (fscanf(proc, "%lu.%02lu", &up_sec, &up_nsec) != 2) {
if (errno) {
pr_perror("fscanf");
+ fclose(proc);
return -errno;
}
pr_err("failed to parse /proc/uptime");
+ fclose(proc);
return -1;
}
fclose(proc);
--
2.27.0
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] selftests/timens: Add fclose(proc) to prevent memory leaks
2024-11-28 6:05 [PATCH] selftests/timens: Add fclose(proc) to prevent memory leaks liujing
@ 2024-12-04 23:09 ` Shuah Khan
0 siblings, 0 replies; 2+ messages in thread
From: Shuah Khan @ 2024-12-04 23:09 UTC (permalink / raw)
To: liujing, shuah; +Cc: linux-kselftest, linux-kernel, Shuah Khan
On 11/27/24 23:05, liujing wrote:
> If fopen succeeds, the fscanf function is called to read the data.
> Regardless of whether fscanf is successful, you need to run
> fclose(proc) to prevent memory leaks.
>
> Signed-off-by: liujing <liujing@cmss.chinamobile.com>
>
> diff --git a/tools/testing/selftests/timens/procfs.c b/tools/testing/selftests/timens/procfs.c
> index 1833ca97eb24..e47844a73c31 100644
> --- a/tools/testing/selftests/timens/procfs.c
> +++ b/tools/testing/selftests/timens/procfs.c
> @@ -79,9 +79,11 @@ static int read_proc_uptime(struct timespec *uptime)
> if (fscanf(proc, "%lu.%02lu", &up_sec, &up_nsec) != 2) {
> if (errno) {
> pr_perror("fscanf");
> + fclose(proc);
> return -errno;
> }
> pr_err("failed to parse /proc/uptime");
> + fclose(proc);
> return -1;
> }
> fclose(proc);
How did you find this problem? This file will be closed when
test exits? What does strace show you?
thanks,
-- Shuah
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-12-04 23:09 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-28 6:05 [PATCH] selftests/timens: Add fclose(proc) to prevent memory leaks liujing
2024-12-04 23:09 ` 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®