Hi,

I finally managed to get back to this and arrived at the patch bundle
below.  The first patch fixes the actual problem for me, the second one
aligns the corresponding debug logs (which aren't enabled in the current
build, but were useful during debugging), and the third one fixes a
compilation error with debugging enabled.  The explanation is in the
commit message.  I didn't test on 32-bit architectures, only on 64-bit
under UML, but briefly contemplated adding an autopkgtest.  Nothing too
involved, because I doubt slirp sees wide use, but we can talk about
this later if you're interested.

Please review and consider incorporating the fix, or I can do an NMU if
you prefer that.

Thanks,
Feri.

>From 247d6e3563512cf41d8af279a8be23d22699f80d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ferenc=20W=C3=A1gner?= <wf...@debian.org>
Date: Fri, 20 Oct 2023 21:05:31 +0200
Subject: [PATCH 1/3] Do not convert tmp_time to unsiged before assigning to
 tv_usec

When tmp_time is set to the sentinel value -1 that conversion results
2^32-1, which is out of range for suseconds_t on 32-bit platforms, so
the assignment invokes undefined behaviour (which apparently happened
to give -1, working good enough for the task by chance).  However, on
64-bit platforms 2^32-1 fits in the range of suseconds_t (long int)
and definitely does not equal -1 in the following check, leading to
EINVAL when passed into the select() call and immediately exiting
slirp on startup.
---
 src/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main.c b/src/main.c
index 6e15a74..1c01732 100644
--- a/src/main.c
+++ b/src/main.c
@@ -933,7 +933,7 @@ cont_1:
         * Take the minimum of the above calculated timeouts
         */
        if ((timeout.tv_usec < 0) || (tmp_time >= 0 && tmp_time < 
timeout.tv_usec))
-               timeout.tv_usec = (u_int)tmp_time;
+               timeout.tv_usec = tmp_time;
 #endif
        DEBUG_MISC((dfd, " timeout.tv_usec = %u",
                    (u_int)timeout.tv_usec));
-- 
2.39.2


>From d20acd8f16cc884611bcf3fd8ad5665876a22f0a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ferenc=20W=C3=A1gner?= <wf...@debian.org>
Date: Fri, 20 Oct 2023 21:22:46 +0200
Subject: [PATCH 2/3] Log tv_usec as the signed long it is

---
 src/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/main.c b/src/main.c
index 1c01732..6384810 100644
--- a/src/main.c
+++ b/src/main.c
@@ -935,8 +935,8 @@ cont_1:
        if ((timeout.tv_usec < 0) || (tmp_time >= 0 && tmp_time < 
timeout.tv_usec))
                timeout.tv_usec = tmp_time;
 #endif
-       DEBUG_MISC((dfd, " timeout.tv_usec = %u",
-                   (u_int)timeout.tv_usec));
+       DEBUG_MISC((dfd, " timeout.tv_usec = %ld",
+                   (long)timeout.tv_usec));
        if (time_fasttimo) {
                DEBUG_MISC((dfd, ", need fasttimo\n"));
        } else {
-- 
2.39.2


>From ed538e8fae21a6f802e412b5b38a19944ac5d631 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ferenc=20W=C3=A1gner?= <wf...@debian.org>
Date: Sat, 21 Oct 2023 17:06:00 +0200
Subject: [PATCH 3/3] Add missing FILE argument to DEBUG_ERROR macro invocation

---
 src/misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/misc.c b/src/misc.c
index e2849b8..dc6e593 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -988,7 +988,7 @@ static int slirp_vsnprintf(char *str, size_t size,
     int rv = vsnprintf(str, size, format, args);
 
     if (rv < 0) {
-        DEBUG_ERROR(("vsnprintf() failed: %s", strerror(errno)));
+        DEBUG_ERROR((dfd, "vsnprintf() failed: %s", strerror(errno)));
     }
 
     return rv;
-- 
2.39.2

Reply via email to