From: Markus Armbruster <armbru@redhat.com>
To: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: qemu-devel@nongnu.org, Fabiano Rosas <farosas@suse.de>,
Paolo Bonzini <pbonzini@redhat.com>,
Laurent Vivier <lvivier@redhat.com>
Subject: Re: [PATCH] qtest: ensure socket_accept() return value is initialized
Date: Mon, 26 Oct 2026 14:54:32 +0100 [thread overview]
Message-ID: <87ik2od0tj.fsf@pond.sub.org> (raw)
In-Reply-To: <20261026124043.770095-1-berrange@redhat.com> ("Daniel P. Berrangé"'s message of "Mon, 26 Oct 2026 12:40:43 +0000")
Daniel P. Berrangé <berrange@redhat.com> writes:
> If the very first iteration of the loop breaks early due to the
> dead QEMU process, 'ret' is never initialized. Ensure it is set
> to -1 to represent an invalid FD number.
>
> CID 1688281
> Reported-by: Markus Armbruster <armbru@redhat.com>
> Fixes: c12ad5378739 (qtest: fail fast when QEMU terminates early)
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
> tests/qtest/libqtest.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
> index 9e7a2d43a18..899727d6d35 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;
> size_t i;
> #ifndef _WIN32
> struct timeval timeout = { .tv_sec = 0,
Hmm, we do the same thing in several different ways:
static int qtest_socket_accept(QTestState *s, int sock)
{
struct sockaddr_un addr;
socklen_t addrlen;
int ret;
size_t i;
#ifndef _WIN32
struct timeval timeout = { .tv_sec = 0,
.tv_usec = ACCEPT_TIMEOUT_MS * 1000ul };
#else
DWORD timeout = ACCEPT_TIMEOUT_MS;
#endif
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
(void *)&timeout, sizeof(timeout))) {
fprintf(stderr, "%s failed to set SO_RCVTIMEO: %s\n",
__func__, strerror(errno));
close(sock);
return -1;
(A) Free resources and return failure.
}
for (i = 0; i < ACCEPT_RETRIES ; i++) {
if (!qtest_probe_child(s)) {
fprintf(stderr,
"child process unexpectedly exited, skipping socket accept\n");
goto cleanup;
(B) Jump to exit, where we free resources and return @ret.
}
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));
}
}
(C) Return @ret.
Hmm, should we close(sock) on failure here?
return ret;
}
cleanup:
close(sock);
return ret;
}
All three ways can work fine. But using all three next to each other I
find somewhat grating. Matter of taste. I'm not demanding you change
this.
next prev parent reply other threads:[~2026-10-26 13:55 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-26 12:40 [PATCH] qtest: ensure socket_accept() return value is initialized Daniel P. Berrangé
2026-10-26 13:54 ` Markus Armbruster [this message]
2026-10-26 14:32 ` Daniel P. Berrangé
2026-10-26 15:25 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ik2od0tj.fsf@pond.sub.org \
--to=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=farosas@suse.de \
--cc=lvivier@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.