diff --git a/core/libs/prx/libc/src/Time.cpp b/core/libs/prx/libc/src/Time.cpp index 189bde0aa502..a50cce43e6c5 100644 --- a/core/libs/prx/libc/src/Time.cpp +++ b/core/libs/prx/libc/src/Time.cpp @@ -275,8 +275,10 @@ void addNumber(FormatBuffer& buffer, std::int64_t value, int modulus, const char } const char* tableName(int index, const char* const* names, const char* const* shortNames, int count) { - if (index < 0) return shortNames[0]; - return index < count ? names[index] : ""; + const auto field = static_cast(static_cast(index) * 2u + (names == shortNames ? 0u : 1u)); + if (field < 0) return shortNames[0]; + if (field >= 2 * count) return ""; + return field % 2 == 0 ? shortNames[field / 2] : names[field / 2]; } int twoDigitYear(std::int64_t year) { diff --git a/core/libs/tests/GuestStrftime.cpp b/core/libs/tests/GuestStrftime.cpp index c7a55368ece1..cbe6d8d5956e 100644 --- a/core/libs/tests/GuestStrftime.cpp +++ b/core/libs/tests/GuestStrftime.cpp @@ -82,6 +82,23 @@ int main() { Check(name, time, "%a|%A|%b|%B|%m|%u|%w", expected[value + 3]); } + struct WrappedName { + int value; + const char* expected; + }; + constexpr WrappedName wrapped[] = { + {-2147483647 - 1, "Sun|Sunday|Jan|January"}, {-1884104312, "|||"}, {-1073741825, "|||"}, {-1073741824, "Sun|Sun|Jan|Jan"}, + {536870912, "|||"}, {1073741823, "|||"}, {1073741824, "Sun|Sun|Jan|Jan"}, {1526330116, "Sun|Sun|Jan|Jan"}, {2147483647, "Sun|Sun|Jan|Jan"}, + }; + for (const auto& item : wrapped) { + GuestTm time = base; + time.tm_wday = item.value; + time.tm_mon = item.value; + char name[32]; + std::snprintf(name, sizeof(name), "wrapped index %d", item.value); + Check(name, time, "%a|%A|%b|%B", item.expected); + } + struct Field { int value; const char* expected; @@ -254,6 +271,13 @@ int main() { past.tm_wday = 7; past.tm_mon = 12; CheckAsctime("names past the tables", past, " 2 13:05:09 2026\n"); + GuestTm wrappedNames = base; + wrappedNames.tm_wday = 1073741824; + wrappedNames.tm_mon = -1073741825; + CheckAsctime("wrapped names", wrappedNames, "Sun 2 13:05:09 2026\n"); + wrappedNames.tm_wday = -1073741825; + wrappedNames.tm_mon = -2147483647 - 1; + CheckAsctime("wrapped names from INT_MIN", wrappedNames, " Jan 2 13:05:09 2026\n"); GuestTm big = base; big.tm_mday = 40; big.tm_hour = 25;