Skip to contentSkip to conversation

qtest: ensure socket_accept() return value is initialized

PATCH In review Daniel P. Berrangé 2 participants 4 messages Oct 26

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qtest: ensure socket_accept() return value is initialized

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

Fixes c12ad5378739 (qtest: fail fast when QEMU terminates early)
---
 tests/qtest/libqtest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/libqtest.c +1 -1
index 9e7a2d43a18..899727d6d35 100644
@@ -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,
Signature · 1 line
2.56.0
#1 lore raw
Quoted 17 lines · Daniel P. Berrangé

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

Fixes c12ad5378739 (qtest: fail fast when QEMU terminates early)
---
 tests/qtest/libqtest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/libqtest.c +0 -0
index 9e7a2d43a18..899727d6d35 100644

tests/qtest/libqtest.c static int qtest_socket_accept(QTestState *s, int sock) · line 127

Quoted 8 lines
tests/qtest/libqtest.c +1 -1
@@ -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.

#2 lore raw
Quoted 74 lines · Markus Armbruster
Quoted 17 lines · Daniel P. Berrangé

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

Fixes c12ad5378739 (qtest: fail fast when QEMU terminates early)
---
 tests/qtest/libqtest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/libqtest.c +0 -0
index 9e7a2d43a18..899727d6d35 100644

tests/qtest/libqtest.c static int qtest_socket_accept(QTestState *s, int sock) · line 127

Quoted 8 lines
tests/qtest/libqtest.c +1 -1
@@ -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));
               }
           }
Quoted 3 lines

(C) Return @ret.

Hmm, should we close(sock) on failure here?

Opps, yes, you are right - we should in fact fall through to the cleanup: label, as the intention is 'sock' is closed in all, as the caller expects socket_accept() to close the sock in all cases. That's a second bug in my original patch.

This is pretty unpleasant semantics though - the caller owns the socket and should own closing it.

Quoted 7 lines
           return ret;
       }

    cleanup:
       close(sock);

       return ret;
Quoted 6 lines

}

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.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#3 lore raw
Quoted 1 line · Daniel P. Berrangé

On Mon, Oct 26, 2026 at 02:54:32PM +0100, Markus Armbruster wrote:

[...]

Quoted 9 lines
Quoted 1 line

Hmm, should we close(sock) on failure here?

Opps, yes, you are right - we should in fact fall through to the cleanup: label, as the intention is 'sock' is closed in all, as the caller expects socket_accept() to close the sock in all cases. That's a second bug in my original patch.

This is pretty unpleasant semantics though - the caller owns the socket and should own closing it.

Oh yes. Even more so since the function is called *_accept().

[...]