[LTP] setsockopt10: Bind test sockets to any free port

linuxtestproject.agent@gmail.com linuxtestproject.agent@gmail.com
Mon Jul 20 17:53:51 CEST 2026


Hi Martin,

On Mon, Jul 20, 2026 at 16:09:22 +0200, Martin Doucha wrote:
> setsockopt10: Bind test sockets to any free port

> +	tcp0_sk = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
> +	tcp1_sk = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
>  	SAFE_BIND(tcp0_sk, (struct sockaddr *)&tcp0_addr, sizeof(tcp0_addr));
>  	SAFE_BIND(tcp1_sk, (struct sockaddr *)&tcp1_addr, sizeof(tcp1_addr));
> -	const pid_t child_pid = SAFE_FORK();
> +	child_pid = SAFE_FORK();
>  
>  	if (child_pid == 0) {
>  		child();
>  		return;
>  	}
>  
> +	addrlen = tst_get_connect_address(tcp0_sk, &sockaddr);
> +	SAFE_CLOSE(tcp0_sk);

This moves tcp0_sk creation into run() before SAFE_FORK(), which means
the parent now holds a live fd in tcp0_sk when SAFE_CLOSE() is called
here.

SAFE_CLOSE() closes the fd but leaves the tcp0_sk variable unchanged.
In the original code, tcp0_sk was only assigned inside child(), in the
child process, so the parent's tcp0_sk was always 0 and cleanup() skipped
it. Now cleanup() sees tcp0_sk > 0 and calls SAFE_CLOSE() on an already
closed fd, causing EBADF and a TBROK on every test run.

Setting tcp0_sk = -1 (or 0) after the SAFE_CLOSE() here should fix it.

Verdict - Needs revision

---
Note:

The agent can sometimes produce false positives although often its
findings are genuine. If you find issues with the review, please
comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer


More information about the ltp mailing list