diff mbox series

[3/3] selftests: mptcp: Clean up resources properly in main_loop

Message ID 20250113085228.121778-4-liucong2@kylinos.cn (mailing list archive)
State Changes Requested
Delegated to: Geliang Tang
Headers show
Series selftests: mptcp: Fix various issues in main_loop | expand

Checks

Context Check Description
matttbe/build success Build and static analysis OK
matttbe/checkpatch success total: 0 errors, 0 warnings, 0 checks, 21 lines checked
matttbe/shellcheck success MPTCP selftests files have not been modified
matttbe/KVM_Validation__normal success Success! ✅
matttbe/KVM_Validation__debug success Success! ✅
matttbe/KVM_Validation__btf-normal__only_bpftest_all_ success Success! ✅
matttbe/KVM_Validation__btf-debug__only_bpftest_all_ warning Unstable: 1 failed test(s): bpftest_test_progs_mptcp

Commit Message

Cong Liu Jan. 13, 2025, 8:52 a.m. UTC
Add proper cleanup of resources (file descriptors and address info)
in error paths to prevent resource leaks.

Signed-off-by: Cong Liu <liucong2@kylinos.cn>
---
 tools/testing/selftests/net/mptcp/mptcp_connect.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Comments

Geliang Tang Jan. 15, 2025, 1:13 a.m. UTC | #1
cc mptcp only.

On Mon, 2025-01-13 at 16:52 +0800, Cong Liu wrote:
> Add proper cleanup of resources (file descriptors and address info)
> in error paths to prevent resource leaks.
> 
> Signed-off-by: Cong Liu <liucong2@kylinos.cn>
> ---
>  tools/testing/selftests/net/mptcp/mptcp_connect.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/net/mptcp/mptcp_connect.c
> b/tools/testing/selftests/net/mptcp/mptcp_connect.c
> index 4d4ea4627daa..e82fde0411b2 100644
> --- a/tools/testing/selftests/net/mptcp/mptcp_connect.c
> +++ b/tools/testing/selftests/net/mptcp/mptcp_connect.c
> @@ -1271,8 +1271,13 @@ int main_loop(void)
>  	}
>  
>  	ret = copyfd_io(fd_in, fd, 1, 0, &winfo);
> -	if (ret)
> +	if (ret) {
> +		close(fd);
> +		if (cfg_input)
> +			close(fd_in);

Should 'listensock' be closed here?

> +		freeaddrinfo(peer);
>  		return ret;
> +	}
>  
>  	if (cfg_truncate > 0) {
>  		xdisconnect(fd, peer->ai_addrlen);
> @@ -1291,6 +1296,7 @@ int main_loop(void)
>  		goto again;
>  	} else {
>  		close(fd);

Should 'fd_in' be closed here? If so, is it possible to use a 'goto
out;' to reduce duplication of code?

Thanks,
-Geliang

> +		freeaddrinfo(peer);
>  	}
>  
>  	return 0;
diff mbox series

Patch

diff --git a/tools/testing/selftests/net/mptcp/mptcp_connect.c b/tools/testing/selftests/net/mptcp/mptcp_connect.c
index 4d4ea4627daa..e82fde0411b2 100644
--- a/tools/testing/selftests/net/mptcp/mptcp_connect.c
+++ b/tools/testing/selftests/net/mptcp/mptcp_connect.c
@@ -1271,8 +1271,13 @@  int main_loop(void)
 	}
 
 	ret = copyfd_io(fd_in, fd, 1, 0, &winfo);
-	if (ret)
+	if (ret) {
+		close(fd);
+		if (cfg_input)
+			close(fd_in);
+		freeaddrinfo(peer);
 		return ret;
+	}
 
 	if (cfg_truncate > 0) {
 		xdisconnect(fd, peer->ai_addrlen);
@@ -1291,6 +1296,7 @@  int main_loop(void)
 		goto again;
 	} else {
 		close(fd);
+		freeaddrinfo(peer);
 	}
 
 	return 0;