portable icon indicating copy to clipboard operation
portable copied to clipboard

gettimeofday implementation for MSVC cannot handle dates beyond 2038

Open LainOTN2 opened this issue 1 year ago • 4 comments

MSVC gettimeofday implementation rely on the definition of timeval provided by winsock2.h https://learn.microsoft.com/en-us/windows/win32/api/winsock2/ns-winsock2-timeval

typedef struct timeval { long tv_sec; long tv_usec; } TIMEVAL, *PTIMEVAL, *LPTIMEVAL;

this give us potential errors when dealing with dates bigger than the year 2038, i.e. OpenSSH-Portable hangs if the machine date is set to a year bigger than 2039

Connection is stuck when time on SSH Server is set after the Year-2038 Problem

LainOTN2 avatar Aug 02 '24 11:08 LainOTN2

Thanks. Looks like that would need some compat implementation of gettimeofday via GetSystemTime and SystemTimeToFileTime or something like that.

Would be nice if the OS provided a fix on the system level rather than requiring downstreams to come up with workarounds for its brokenness...

botovq avatar Aug 02 '24 11:08 botovq

The funcion is already compat implemented here: https://github.com/libressl/portable/blob/cd0ae0ef32d308a4704006b4514e2d065ed8df3c/crypto/compat/posix_win.c#L292

but when declaring

https://github.com/libressl/portable/blob/cd0ae0ef32d308a4704006b4514e2d065ed8df3c/include/compat/sys/time.h#L11

we are taking the timeval declaration from winsock2.h, that is a signed 32 bits.

LainOTN2 avatar Aug 02 '24 16:08 LainOTN2

Right. I need to fix the aggressive .gitignore...

If we're lucky, the diff below works. I'm unsure how posix_win.c ends up pulling in sys/time.h, perhaps it needs an explicit such include somewhere at the top.

diff --git a/include/compat/sys/time.h b/include/compat/sys/time.h
index 76428c1..2448969 100644
--- a/include/compat/sys/time.h
+++ b/include/compat/sys/time.h
@@ -8,6 +8,15 @@
 
 #ifdef _MSC_VER
 #include <winsock2.h>
+
+#define timeval libressl_timeval
+#define gettimeofday libressl_gettimeofday
+
+struct timeval {
+	long long	tv_sec;
+	long		tv_usec;
+};
+
 int gettimeofday(struct timeval *tp, void *tzp);
 #else
 #include_next <sys/time.h>

botovq avatar Aug 02 '24 16:08 botovq

See #1078. It would be great if you could check if this addresses the issue.

botovq avatar Aug 02 '24 20:08 botovq