From 795a42110d9eba6ea183833839015d953ac31d11 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Fri, 11 Feb 2022 23:49:09 -0500
Subject: [PATCH] When a double is cast to an int, but the double has a larger
 value than the int supports, it's set to 0x80000000 to indicate overflow.

msclock() is *always* overflowing, and clock_t is only 32-bits on
some platforms (specifically FreeBSD).  To "avoid" problems, just
keep subtracting UIN32_MAX from the value until it's less than INT_MAX
then cast.

This function is, of course, terrible and shouldn't actually be used,
but it should at least sorta kinda workish.
---
 src/xpdev/genwrap.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/xpdev/genwrap.c b/src/xpdev/genwrap.c
index 31b53f50cf..0fed58b590 100644
--- a/src/xpdev/genwrap.c
+++ b/src/xpdev/genwrap.c
@@ -696,7 +696,14 @@ char* os_cmdshell(void)
 #ifdef __unix__
 clock_t msclock(void)
 {
-	return (clock_t)(xp_timer() * 1000);
+        long double t = roundl(xp_timer() * 1000);
+
+        if (sizeof(clock_t) < 8) {
+                while (t > INT32_MAX)
+                        t -= UINT32_MAX;
+        }
+
+	return (clock_t)t;
 }
 #endif
 
-- 
GitLab