Daniel P. Berrangé <[email protected]> writes:
> If the very first iteration of the loop breaks early due to the
> dead QEMU process, 'ret' is never initialized. Furthermore the
> listener socket is not closed on success and there is no error
> message printed if timing out after all iterations are complete.
> These problems were introduced by the recent refactoring to check
> QEMU is alive.
>
> CID 1688281
> Reported-by: Markus Armbruster <[email protected]>
> Fixes: c12ad5378739 ("qtest: fail fast when QEMU terminates early")
> Signed-off-by: Daniel P. Berrangé <[email protected]>
> ---
> tests/qtest/libqtest.c | 15 +++++++--------
> 1 file changed, 7 insertions(+), 8 deletions(-)
>
> diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
> index 9e7a2d43a18..50487739ef9 100644
> --- a/tests/qtest/libqtest.c
> +++ b/tests/qtest/libqtest.c
> @@ -121,7 +121,7 @@ static int qtest_socket_accept(QTestState *s, int sock)
> {
> struct sockaddr_un addr;
> socklen_t addrlen;
> - int ret;
> + int ret = -1;
You need this to make [*] work.
> size_t i;
> #ifndef _WIN32
> struct timeval timeout = { .tv_sec = 0,
> @@ -147,14 +147,13 @@ static int qtest_socket_accept(QTestState *s, int sock)
for (i = 0; i < ACCEPT_RETRIES ; i++) {
if (!qtest_probe_child(s)) {
fprintf(stderr,
"child process unexpectedly exited, skipping socket
accept\n");
[*] @ret comes from the initialization above in the first iteration
only. In later iterations, it comes from assignment [**] below.
goto cleanup;
}
>
> addrlen = sizeof(addr);
> ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
[**]
> - if (ret == -1) {
> - if (errno == EINTR || errno == EAGAIN) {
> - continue;
> - } else {
> - fprintf(stderr, "%s failed: %s\n", __func__,
> strerror(errno));
> - }
> + if (ret == -1 && (errno == EINTR || errno == EAGAIN)) {
> + continue;
> }
> - return ret;
> + break;
> + }
> + if (ret == -1) {
@ret comes from [**], because the loop runs at least once
(ACCEPT_RETRIES is positive).
> + fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
> }
>
> cleanup:
Reviewed-by: Markus Armbruster <[email protected]>