mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] selftests/media_tests: fix a resource leak
@ 2023-11-14  9:38 zhujun2
  2023-11-15 15:34 ` Mathieu Desnoyers
  0 siblings, 1 reply; 6+ messages in thread
From: zhujun2 @ 2023-11-14  9:38 UTC (permalink / raw)
  To: shuah
  Cc: mathieu.desnoyers, zhujun2, ivan.orlov0322, linux-kselftest,
	linux-kernel

The opened file should be closed in main(), otherwise resource
leak will occur that this problem was discovered by code reading

Signed-off-by: zhujun2 <zhujun2@cmss.chinamobile.com>
---
 tools/testing/selftests/media_tests/media_device_open.c | 3 +++
 tools/testing/selftests/media_tests/media_device_test.c | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/tools/testing/selftests/media_tests/media_device_open.c b/tools/testing/selftests/media_tests/media_device_open.c
index 93183a37b133..2dfb2a11b148 100644
--- a/tools/testing/selftests/media_tests/media_device_open.c
+++ b/tools/testing/selftests/media_tests/media_device_open.c
@@ -70,6 +70,7 @@ int main(int argc, char **argv)
 	fd = open(media_device, O_RDWR);
 	if (fd == -1) {
 		printf("Media Device open errno %s\n", strerror(errno));
+		close(fd);
 		exit(-1);
 	}
 
@@ -79,4 +80,6 @@ int main(int argc, char **argv)
 	else
 		printf("Media device model %s driver %s\n",
 			mdi.model, mdi.driver);
+
+	close(fd);
 }
diff --git a/tools/testing/selftests/media_tests/media_device_test.c b/tools/testing/selftests/media_tests/media_device_test.c
index 4b9953359e40..7cabb62535a7 100644
--- a/tools/testing/selftests/media_tests/media_device_test.c
+++ b/tools/testing/selftests/media_tests/media_device_test.c
@@ -79,6 +79,7 @@ int main(int argc, char **argv)
 	fd = open(media_device, O_RDWR);
 	if (fd == -1) {
 		printf("Media Device open errno %s\n", strerror(errno));
+		close(fd);
 		exit(-1);
 	}
 
@@ -100,4 +101,6 @@ int main(int argc, char **argv)
 		sleep(10);
 		count--;
 	}
+
+	close(fd);
 }
-- 
2.17.1




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] selftests/media_tests: fix a resource leak
  2023-11-14  9:38 [PATCH] selftests/media_tests: fix a resource leak zhujun2
@ 2023-11-15 15:34 ` Mathieu Desnoyers
  2023-11-21  2:59   ` zhujun2
  0 siblings, 1 reply; 6+ messages in thread
From: Mathieu Desnoyers @ 2023-11-15 15:34 UTC (permalink / raw)
  To: zhujun2, shuah; +Cc: ivan.orlov0322, linux-kselftest, linux-kernel

On 2023-11-14 04:38, zhujun2 wrote:
> The opened file should be closed in main(), otherwise resource
> leak will occur that this problem was discovered by code reading

Fixing resource leaks for one-off test cases (processes execute and
then immediately exit) seems to be something that would fit in the
definition of "trivial", so I would advise to perhaps send those
patches to kernel-janitors@vger.kernel.org ?

[...]

> +
> +	close(fd);

These added calls to close(2) miss handling of possible close errors
(check return value and use errno to print an error).

Thanks,

Mathieu

-- 
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] selftests/media_tests: fix a resource leak
  2023-11-15 15:34 ` Mathieu Desnoyers
@ 2023-11-21  2:59   ` zhujun2
  2023-11-21  7:47     ` Dan Carpenter
  0 siblings, 1 reply; 6+ messages in thread
From: zhujun2 @ 2023-11-21  2:59 UTC (permalink / raw)
  To: kernel-janitors
  Cc: mathieu.desnoyers, ivan.orlov0322, linux-kernel, linux-kselftest,
	shuah, zhujun2

The opened file should be closed in main(), otherwise resource
leak will occur that this problem was discovered by code reading

Signed-off-by: zhujun2 <zhujun2@cmss.chinamobile.com>
---
 tools/testing/selftests/media_tests/media_device_open.c | 3 +++
 tools/testing/selftests/media_tests/media_device_test.c | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/tools/testing/selftests/media_tests/media_device_open.c b/tools/testing/selftests/media_tests/media_device_open.c
index 93183a37b133..2dfb2a11b148 100644
--- a/tools/testing/selftests/media_tests/media_device_open.c
+++ b/tools/testing/selftests/media_tests/media_device_open.c
@@ -70,6 +70,7 @@ int main(int argc, char **argv)
 	fd = open(media_device, O_RDWR);
 	if (fd == -1) {
 		printf("Media Device open errno %s\n", strerror(errno));
+		close(fd);
 		exit(-1);
 	}
 
@@ -79,4 +80,6 @@ int main(int argc, char **argv)
 	else
 		printf("Media device model %s driver %s\n",
 			mdi.model, mdi.driver);
+
+	close(fd);
 }
diff --git a/tools/testing/selftests/media_tests/media_device_test.c b/tools/testing/selftests/media_tests/media_device_test.c
index 4b9953359e40..7cabb62535a7 100644
--- a/tools/testing/selftests/media_tests/media_device_test.c
+++ b/tools/testing/selftests/media_tests/media_device_test.c
@@ -79,6 +79,7 @@ int main(int argc, char **argv)
 	fd = open(media_device, O_RDWR);
 	if (fd == -1) {
 		printf("Media Device open errno %s\n", strerror(errno));
+		close(fd);
 		exit(-1);
 	}
 
@@ -100,4 +101,6 @@ int main(int argc, char **argv)
 		sleep(10);
 		count--;
 	}
+
+	close(fd);
 }
-- 
2.17.1




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] selftests/media_tests: fix a resource leak
  2023-11-21  2:59   ` zhujun2
@ 2023-11-21  7:47     ` Dan Carpenter
  2023-11-21  9:32       ` [PATCH v1] " Zhu Jun
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Carpenter @ 2023-11-21  7:47 UTC (permalink / raw)
  To: zhujun2
  Cc: kernel-janitors, mathieu.desnoyers, ivan.orlov0322, linux-kernel,
	linux-kselftest, shuah

On Mon, Nov 20, 2023 at 06:59:18PM -0800, zhujun2 wrote:
> The opened file should be closed in main(), otherwise resource
> leak will occur that this problem was discovered by code reading
> 
> Signed-off-by: zhujun2 <zhujun2@cmss.chinamobile.com>
> ---
>  tools/testing/selftests/media_tests/media_device_open.c | 3 +++
>  tools/testing/selftests/media_tests/media_device_test.c | 3 +++
>  2 files changed, 6 insertions(+)
> 
> diff --git a/tools/testing/selftests/media_tests/media_device_open.c b/tools/testing/selftests/media_tests/media_device_open.c
> index 93183a37b133..2dfb2a11b148 100644
> --- a/tools/testing/selftests/media_tests/media_device_open.c
> +++ b/tools/testing/selftests/media_tests/media_device_open.c
> @@ -70,6 +70,7 @@ int main(int argc, char **argv)
>  	fd = open(media_device, O_RDWR);
>  	if (fd == -1) {
>  		printf("Media Device open errno %s\n", strerror(errno));
> +		close(fd);

Open failed so there is nothing to close.

>  		exit(-1);

When we exit() then all the resources are automatically reclaimed by the
operating system so we really don't worry about leaks at all in short
running programs.  It's different for an operating system or a web
server which is expected to have a long uptime.  But these programs are
going to run quickly and then exit so resource leaks are not an issue.

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v1] selftests/media_tests: fix a resource leak
  2023-11-21  7:47     ` Dan Carpenter
@ 2023-11-21  9:32       ` Zhu Jun
  2023-11-21  9:42         ` Dan Carpenter
  0 siblings, 1 reply; 6+ messages in thread
From: Zhu Jun @ 2023-11-21  9:32 UTC (permalink / raw)
  To: dan.carpenter
  Cc: ivan.orlov0322, kernel-janitors, linux-kernel, linux-kselftest,
	mathieu.desnoyers, shuah, zhujun2

From: zhujun2 <zhujun2@cmss.chinamobile.com>

The opened file should be closed in main(), otherwise resource
leak will occur that this problem was discovered by code reading

Signed-off-by: zhujun2 <zhujun2@cmss.chinamobile.com>
---

Hi Dan Carpenter 
	
	I believe that the Linux kernel code is sacred and should strictly adhere to C code conventions

thanks,
[Zhu Jun]

 tools/testing/selftests/media_tests/media_device_open.c | 2 ++
 tools/testing/selftests/media_tests/media_device_test.c | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/tools/testing/selftests/media_tests/media_device_open.c b/tools/testing/selftests/media_tests/media_device_open.c
index 93183a37b133..ae263eb78a2c 100644
--- a/tools/testing/selftests/media_tests/media_device_open.c
+++ b/tools/testing/selftests/media_tests/media_device_open.c
@@ -79,4 +79,6 @@ int main(int argc, char **argv)
 	else
 		printf("Media device model %s driver %s\n",
 			mdi.model, mdi.driver);
+
+	close(fd);
 }
diff --git a/tools/testing/selftests/media_tests/media_device_test.c b/tools/testing/selftests/media_tests/media_device_test.c
index 4b9953359e40..65888ce5c89f 100644
--- a/tools/testing/selftests/media_tests/media_device_test.c
+++ b/tools/testing/selftests/media_tests/media_device_test.c
@@ -100,4 +100,6 @@ int main(int argc, char **argv)
 		sleep(10);
 		count--;
 	}
+
+	close(fd);
 }
-- 
2.17.1




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1] selftests/media_tests: fix a resource leak
  2023-11-21  9:32       ` [PATCH v1] " Zhu Jun
@ 2023-11-21  9:42         ` Dan Carpenter
  0 siblings, 0 replies; 6+ messages in thread
From: Dan Carpenter @ 2023-11-21  9:42 UTC (permalink / raw)
  To: Zhu Jun
  Cc: ivan.orlov0322, kernel-janitors, linux-kernel, linux-kselftest,
	mathieu.desnoyers, shuah

On Tue, Nov 21, 2023 at 01:32:38AM -0800, Zhu Jun wrote:
> From: zhujun2 <zhujun2@cmss.chinamobile.com>
> 
> The opened file should be closed in main(), otherwise resource
> leak will occur that this problem was discovered by code reading
> 
> Signed-off-by: zhujun2 <zhujun2@cmss.chinamobile.com>
> ---
> 
> Hi Dan Carpenter 
> 	
> 	I believe that the Linux kernel code is sacred and should strictly adhere to C code conventions
> 

*sigh*.  You do you, I guess.  This patch is pointless but I don't care
whether it's merged or not.

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-11-21  9:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-14  9:38 [PATCH] selftests/media_tests: fix a resource leak zhujun2
2023-11-15 15:34 ` Mathieu Desnoyers
2023-11-21  2:59   ` zhujun2
2023-11-21  7:47     ` Dan Carpenter
2023-11-21  9:32       ` [PATCH v1] " Zhu Jun
2023-11-21  9:42         ` Dan Carpenter

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®