diff mbox series

bpf, selftests: fix a resource leak in main()

Message ID 20240711074437.2283810-1-make24@iscas.ac.cn (mailing list archive)
State New
Headers show
Series bpf, selftests: fix a resource leak in main() | expand

Commit Message

Ma Ke July 11, 2024, 7:44 a.m. UTC
The requested resources should be closed before return in main(), otherwise
resource leak will occur. Add a check of cgroup_fd and close().

Fixes: 4939b2847d26 ("bpf, selftests: Use single cgroup helpers for both test_sockmap/progs")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
 tools/testing/selftests/bpf/test_dev_cgroup.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Eduard Zingerman July 11, 2024, 7:52 a.m. UTC | #1
On Thu, 2024-07-11 at 15:44 +0800, Ma Ke wrote:
> The requested resources should be closed before return in main(), otherwise
> resource leak will occur. Add a check of cgroup_fd and close().

Note: it is better to group related patches to a patch-set.

> 
> Fixes: 4939b2847d26 ("bpf, selftests: Use single cgroup helpers for both test_sockmap/progs")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
>  tools/testing/selftests/bpf/test_dev_cgroup.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/tools/testing/selftests/bpf/test_dev_cgroup.c b/tools/testing/selftests/bpf/test_dev_cgroup.c
> index adeaf63cb6fa..e97fc061fab2 100644
> --- a/tools/testing/selftests/bpf/test_dev_cgroup.c
> +++ b/tools/testing/selftests/bpf/test_dev_cgroup.c
> @@ -81,5 +81,7 @@ int main(int argc, char **argv)
>  	cleanup_cgroup_environment();
>  
>  out:
> +	if (cgroup_fd >= 0)
> +		close(cgroup_fd);

This is a standalone program, and Linux closes file descriptors on process exit,
why do you think it's necessary to close the fd here?

>  	return error;
>  }
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/test_dev_cgroup.c b/tools/testing/selftests/bpf/test_dev_cgroup.c
index adeaf63cb6fa..e97fc061fab2 100644
--- a/tools/testing/selftests/bpf/test_dev_cgroup.c
+++ b/tools/testing/selftests/bpf/test_dev_cgroup.c
@@ -81,5 +81,7 @@  int main(int argc, char **argv)
 	cleanup_cgroup_environment();
 
 out:
+	if (cgroup_fd >= 0)
+		close(cgroup_fd);
 	return error;
 }