From 16e364c62989ca0c6a2c5a04dfea0b304d9f03ed Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 11 Jul 2023 21:22:09 -0700 Subject: [PATCH] time_t size introspection --- c/driver/sqlite/CMakeLists.txt | 4 ++++ c/driver/sqlite/statement_reader.c | 22 ++++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/c/driver/sqlite/CMakeLists.txt b/c/driver/sqlite/CMakeLists.txt index eb5a8453c1..3914ee8e89 100644 --- a/c/driver/sqlite/CMakeLists.txt +++ b/c/driver/sqlite/CMakeLists.txt @@ -56,6 +56,10 @@ foreach(LIB_TARGET ${ADBC_LIBRARIES}) target_compile_definitions(${LIB_TARGET} PRIVATE ADBC_EXPORTING) endforeach() +include(CheckTypeSize) +check_type_size("time_t" SIZEOF_TIME_T) +add_definitions(-DSIZEOF_TIME_T=${SIZEOF_TIME_T}) + if(ADBC_TEST_LINKAGE STREQUAL "shared") set(TEST_LINK_LIBS adbc_driver_sqlite_shared) else() diff --git a/c/driver/sqlite/statement_reader.c b/c/driver/sqlite/statement_reader.c index e503cd9c67..09df02be56 100644 --- a/c/driver/sqlite/statement_reader.c +++ b/c/driver/sqlite/statement_reader.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -94,7 +95,6 @@ AdbcStatusCode AdbcSqliteBinderSetArrayStream(struct AdbcSqliteBinder* binder, /* caller is responsible for freeing data */ static const char* ArrowTimestampToIsoString(int64_t value, enum ArrowTimeUnit unit) { - int64_t seconds; int scale = 1; int strlen = 20; int rem = 0; @@ -115,7 +115,20 @@ static const char* ArrowTimestampToIsoString(int64_t value, enum ArrowTimeUnit u strlen = 30; break; } - seconds = value / scale; + + int64_t seconds = value / scale; + time_t time; + +#if SIZEOF_TIME_T < 8 + if ((seconds > INT32_MAX) || (seconds < INT32_MIN)) { + // TODO: give better error visibility + return NULL; + } + time = (time_t)seconds; +#else + time = seconds; +#endif + rem = value % scale; if (rem < 0) { @@ -123,12 +136,13 @@ static const char* ArrowTimestampToIsoString(int64_t value, enum ArrowTimeUnit u } struct tm broken_down_time; + #if defined(_WIN32) - if (gmtime_s(&broken_down_time, &seconds) != 0) { + if (gmtime_s(&broken_down_time, &time) != 0) { return NULL; } #else - if (gmtime_r(&seconds, &broken_down_time) != &broken_down_time) { + if (gmtime_r(&time, &broken_down_time) != &broken_down_time) { return NULL; } #endif