From 2137aa0cc715e82e7bcc872d9a8fdb78f4b41898 Mon Sep 17 00:00:00 2001 From: Marc Gurevitx Date: Sun, 14 Jul 2024 12:26:51 +0300 Subject: [PATCH 1/3] Fix _dateStr() for large time_t values --- MiniScript-cpp/src/DateTimeUtils.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/MiniScript-cpp/src/DateTimeUtils.cpp b/MiniScript-cpp/src/DateTimeUtils.cpp index b0da71d..8aa2ace 100644 --- a/MiniScript-cpp/src/DateTimeUtils.cpp +++ b/MiniScript-cpp/src/DateTimeUtils.cpp @@ -32,6 +32,7 @@ static bool Match(const String s, size_t *posB, const String match) { String FormatDate(time_t t, String formatSpec) { tm dateTime; localtime_r(&t, &dateTime); + if (errno == EOVERFLOW) return ""; // arg t too large const int BUFSIZE = 128; char buffer[BUFSIZE]; From e3eec4b6a790851a0565a7b3bf9cd9b9b27f33fc Mon Sep 17 00:00:00 2001 From: Marc Gurevitx Date: Sun, 14 Jul 2024 14:28:01 +0300 Subject: [PATCH 2/3] Fix _dateStr() for large time_t values on Windows --- MiniScript-cpp/src/DateTimeUtils.cpp | 8 +++++--- MiniScript-cpp/tests/testDateTimeDateStr.ms | 7 +++++++ 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 MiniScript-cpp/tests/testDateTimeDateStr.ms diff --git a/MiniScript-cpp/src/DateTimeUtils.cpp b/MiniScript-cpp/src/DateTimeUtils.cpp index 8aa2ace..9c6a94b 100644 --- a/MiniScript-cpp/src/DateTimeUtils.cpp +++ b/MiniScript-cpp/src/DateTimeUtils.cpp @@ -13,7 +13,9 @@ #if _WIN32 || _WIN64 struct tm *localtime_r( const time_t *timer, struct tm *buf ) { - *buf = *_localtime64(timer); + struct tm *newtime = _localtime64(timer); + if (newtime == nullptr) return nullptr; + *buf = *newtime; return buf; } #endif @@ -31,8 +33,8 @@ static bool Match(const String s, size_t *posB, const String match) { String FormatDate(time_t t, String formatSpec) { tm dateTime; - localtime_r(&t, &dateTime); - if (errno == EOVERFLOW) return ""; // arg t too large + struct tm *newtime = localtime_r(&t, &dateTime); + if (errno or newtime == nullptr) return ""; // arg t too large const int BUFSIZE = 128; char buffer[BUFSIZE]; diff --git a/MiniScript-cpp/tests/testDateTimeDateStr.ms b/MiniScript-cpp/tests/testDateTimeDateStr.ms new file mode 100644 index 0000000..69d3176 --- /dev/null +++ b/MiniScript-cpp/tests/testDateTimeDateStr.ms @@ -0,0 +1,7 @@ +import "qa" + +testDateTimeDateStr = function + qa.assertEqual _dateStr(1e20), "" +end function + +if refEquals(locals, globals) then testDateTimeDateStr From f4a13e0c3b1c9f4e8deed9648f057bc8e0ba2be0 Mon Sep 17 00:00:00 2001 From: Marc Gurevitx Date: Sun, 14 Jul 2024 14:54:46 +0300 Subject: [PATCH 3/3] Fix _dateStr() for large time_t values (fixed regression on Linux) --- MiniScript-cpp/src/DateTimeUtils.cpp | 2 +- MiniScript-cpp/tests/testDateTimeDateStr.ms | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/MiniScript-cpp/src/DateTimeUtils.cpp b/MiniScript-cpp/src/DateTimeUtils.cpp index 9c6a94b..8cf5984 100644 --- a/MiniScript-cpp/src/DateTimeUtils.cpp +++ b/MiniScript-cpp/src/DateTimeUtils.cpp @@ -34,7 +34,7 @@ static bool Match(const String s, size_t *posB, const String match) { String FormatDate(time_t t, String formatSpec) { tm dateTime; struct tm *newtime = localtime_r(&t, &dateTime); - if (errno or newtime == nullptr) return ""; // arg t too large + if (newtime == nullptr) return ""; // arg t too large const int BUFSIZE = 128; char buffer[BUFSIZE]; diff --git a/MiniScript-cpp/tests/testDateTimeDateStr.ms b/MiniScript-cpp/tests/testDateTimeDateStr.ms index 69d3176..b2ff00a 100644 --- a/MiniScript-cpp/tests/testDateTimeDateStr.ms +++ b/MiniScript-cpp/tests/testDateTimeDateStr.ms @@ -1,6 +1,7 @@ import "qa" testDateTimeDateStr = function + qa.assertEqual _dateStr(0), "2000-01-01 00:00:00" qa.assertEqual _dateStr(1e20), "" end function