commit 6ceb2ebc958eda812b5656a13aadb41a622fbc7e
parent c33bb747ce4428caec05c620d81dc1fbf8aaaa37
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date: Thu, 23 Apr 2026 11:06:21 +0200
libc/time: Fix signees of the returned value
Difftime is protected against unsigned time_t definitions (even when
we don't currently support any platform where time_t is unsigned)
but the explicit unary - was in the wrong branch of the ternary. It
was not detected before because the test testing it was wrong too.
Diffstat:
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/libc/time/difftime.c b/src/libc/time/difftime.c
@@ -5,5 +5,5 @@
double
difftime(time_t t1, time_t t2)
{
- return t2 <= t1 ? -(double) (t1 - t2) : (double) (t2 - t1);
+ return t2 <= t1 ? (double) (t1 - t2) : -(double) (t2 - t1);
}
diff --git a/tests/libc/execute/0070-difftime.c b/tests/libc/execute/0070-difftime.c
@@ -15,12 +15,12 @@ test(void)
double d1, d2;
time_t t1, t2;
- for (t1 = t2 = time(NULL); difftime(t1, t2) < 1; t2 = time(NULL))
+ for (t1 = t2 = time(NULL); difftime(t2, t1) < 1; t2 = time(NULL))
;
d1 = difftime(t1, t2);
d2 = difftime(t2, t1);
- assert(d1 + d2 < 0.0005);
+ assert(d1 + d2 < 0.00005);
}
int