POSIX ftime() function can fail and return -1. So if the time value in
32-bit ftime call does not into struct __timeb32 then signal EOVERFLOW.

---
Kirill's run of CI tests passed on:
https://github.com/maiddaisuki/mingw-w64/actions/runs/22307392638
>From b40e5ee2edad015e70abe1f1394d056fc8f218d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Wed, 21 Jan 2026 21:16:40 +0100
Subject: [PATCH] crt: Check for overflows in POSIX 32-bit ftime() function

---
 mingw-w64-crt/misc/ftime32.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/mingw-w64-crt/misc/ftime32.c b/mingw-w64-crt/misc/ftime32.c
index 41cc31a6673c..2ff79bab457b 100644
--- a/mingw-w64-crt/misc/ftime32.c
+++ b/mingw-w64-crt/misc/ftime32.c
@@ -4,12 +4,31 @@
  * No warranty is given; refer to the file DISCLAIMER.PD within this package.
  */
 
+#include <stdint.h>
+#include <errno.h>
 #include <sys/timeb.h>
 
 int __cdecl ftime32(struct __timeb32 *tb32);
 int __cdecl ftime32(struct __timeb32 *tb32)
 {
-    _ftime32(tb32);
+    /*
+     * Both 32-bit and 64-bit MS _ftime functions have void return value and 
do not signal overflow.
+     * So if 32-bit POSIX ftime function wants to detect overflow it has to 
call 64-bit _ftime function.
+     * msvc defines ftime as alias to _ftime, which always fills all members 
even if they overflow.
+     * So for compatibility with application code written for msvc ftime 
function, always fill
+     * in our mingw-w64 POSIX ftime function all __timeb32 members, even if 
they are overflowed.
+     * And if overflow happens, correctly sets errno to EOVERFLOW and returns 
negative value.
+     */
+    struct __timeb64 tb64;
+    _ftime64(&tb64);
+    tb32->time = (__time32_t)tb64.time; /* truncate */
+    tb32->millitm = tb64.millitm;
+    tb32->timezone = tb64.timezone;
+    tb32->dstflag = tb64.dstflag;
+    if (tb64.time < INT32_MIN || tb64.time > INT32_MAX) {
+        errno = EOVERFLOW;
+        return -1;
+    }
     return 0;
 }
 
-- 
2.20.1

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to