diff mbox series

selftests/media_tests: fix a resource leak

Message ID 20231114093812.7169-1-zhujun2@cmss.chinamobile.com (mailing list archive)
State New
Headers show
Series selftests/media_tests: fix a resource leak | expand

Commit Message

Zhu Jun Nov. 14, 2023, 9:38 a.m. UTC
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(+)

Comments

Mathieu Desnoyers Nov. 15, 2023, 3:34 p.m. UTC | #1
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
diff mbox series

Patch

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);
 }