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;
     size_t i;
 #ifndef _WIN32
     struct timeval timeout = { .tv_sec = 0,
@@ -147,14 +147,13 @@ static int qtest_socket_accept(QTestState *s, int sock)
 
         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) {
+        fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
     }
 
  cleanup:
-- 
2.56.0


Reply via email to