Re: libc difftime() miscomputes negative representable time_t values on amd64

Todd C. Miller <[email protected]>
Newsgroups gmane.os.openbsd.bugs
Message-ID <[email protected]>
Your analysis is correct.  The result of the bitwise AND is unsigned
so we need to cast back to time_t before the cast to double.

With this change I get the expected result from your test
program:

    sizeof(time_t)=8
    difftime(0, -86400) = 86400

 - todd

Index: lib/libc/time/difftime.c
===================================================================
RCS file: /cvs/src/lib/libc/time/difftime.c,v
diff -u -p -u -r1.13 difftime.c
--- lib/libc/time/difftime.c	21 May 2025 01:27:29 -0000	1.13
+++ lib/libc/time/difftime.c	22 Apr 2026 16:09:02 -0000
@@ -3,8 +3,8 @@
 
 #include "private.h"
 
-#define HI(t) ((double)(t & 0xffffffff00000000LL))
-#define LO(t) ((double)(t & 0x00000000ffffffffLL))
+#define HI(t) ((double)(time_t)(t & 0xffffffff00000000LL))
+#define LO(t) ((double)(time_t)(t & 0x00000000ffffffffLL))
 
 double __pure
 difftime(time_t t1, time_t t0)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.