[libc-commits] [libc] [libc] Fix gmtime test on systems with sizeof(time_t) == 4 (PR #65388)

Mikhail R. Gadelha via libc-commits libc-commits at lists.llvm.org
Wed Sep 6 11:50:37 PDT 2023


https://github.com/mikhailramalho updated https://github.com/llvm/llvm-project/pull/65388:

>From 66502dbb2c25f4f9bf5190bd25107a88248d9db9 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Fri, 1 Sep 2023 10:40:43 -0300
Subject: [PATCH 1/2] [libc] Fix gmtime test on systems with sizeof(time_t) ==
 4

This test creates a time_t variable and assigns 0xfffffffffe1d7b01 which
overflows the maximum time_t value for 64-bit time_t, then checks if the
syscall fails and errno was set.

In systems with sizeof(time_t) == 4, the value is narrowed down to 0xfe1d7b01
and doesn't overflow, causing the test to fail.

This patch then disables the test on systems with 32 bits long time_t.
---
 libc/test/src/time/gmtime_test.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libc/test/src/time/gmtime_test.cpp b/libc/test/src/time/gmtime_test.cpp
index 07e3950bdcf24a..e502f1d65e892a 100644
--- a/libc/test/src/time/gmtime_test.cpp
+++ b/libc/test/src/time/gmtime_test.cpp
@@ -20,6 +20,8 @@ using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
 using __llvm_libc::time_utils::TimeConstants;
 
 TEST(LlvmLibcGmTime, OutOfRange) {
+  if(sizeof(time_t) == 4)
+    return;
   time_t seconds =
       1 + INT_MAX * static_cast<int64_t>(
                         TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR);

>From bfbe2fff21286e03bb1f8a2c80dceb1f3cd312b0 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Wed, 6 Sep 2023 15:43:55 -0300
Subject: [PATCH 2/2] Changed if condition

Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
 libc/test/src/time/gmtime_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/src/time/gmtime_test.cpp b/libc/test/src/time/gmtime_test.cpp
index e502f1d65e892a..4d990b3da5bc15 100644
--- a/libc/test/src/time/gmtime_test.cpp
+++ b/libc/test/src/time/gmtime_test.cpp
@@ -20,7 +20,7 @@ using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
 using __llvm_libc::time_utils::TimeConstants;
 
 TEST(LlvmLibcGmTime, OutOfRange) {
-  if(sizeof(time_t) == 4)
+  if (sizeof(time_t) < sizeof(int64_t))
     return;
   time_t seconds =
       1 + INT_MAX * static_cast<int64_t>(



More information about the libc-commits mailing list