Re: [PATCH] selftests/media_tests: fix a resource leak

From: Dan Carpenter
Date: Tue Nov 21 2023 - 02:47:58 EST


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@xxxxxxxxxxxxxxxxxxxx>
> ---
> 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