Merge pull request #2053 from kitkatt7/formattime-usec-pad-underflow

utils: fix size_t underflow in FormatTime %f padding
This commit is contained in:
Alexey Sokolov authored and GitHub committed 2026-10-18 00:53:43 +01:00
commit 881de2f4d5
2 files changed
+15 -1

No files matched your search

+3 -1
View File
@@ -563,7 +563,9 @@ CString CUtils::FormatTime(const timeval& tv, const CString& sFormat,
sFormat2 += sFormat.substr(uLastCopied,
uFormatStart - uLastCopied);
CString sVal = CString(iVal);
sFormat2 += CString(iDigits - sVal.length(), '0');
if (static_cast<CString::size_type>(iDigits) >
sVal.length())
sFormat2 += CString(iDigits - sVal.length(), '0');
sFormat2 += sVal;
uLastCopied = i + 1;
bInFormat = false;
+12
View File
@@ -307,4 +307,16 @@ TEST(UtilsTest, FormatTime) {
// Test suffix
CString str3 = CUtils::FormatTime(tv2, "a%fb", "UTC");
EXPECT_EQ(str3, "a123b");
// The zero-pad width in the %f handler is (iDigits - sVal.length())
// computed in size_t. A zero-width %0f field, and a negative tv_usec
// (which ParseServerTime() hands back for a pre-1970 IRCv3 @time tag),
// both make sVal at least as long as iDigits and used to underflow that
// width, aborting via std::length_error.
EXPECT_EQ(CUtils::FormatTime(timeval{42, 123456}, "%s.%0f", "UTC"), "42.0");
timeval tvNeg;
tvNeg.tv_sec = 0;
tvNeg.tv_usec = -500000;
EXPECT_NO_THROW(CUtils::FormatTime(tvNeg, "[%H:%M:%S.%f]", "UTC"));
}