From 0c34ad0de2ca05d22efb8acaec6bc7f858358095 Mon Sep 17 00:00:00 2001 From: hyc-alt <250169095+hyc-alt@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:48:45 +0800 Subject: [PATCH 1/2] re2: return NULL from TerminateNumber on error and reject whitespace-only floats TerminateNumber() previously returned "" on empty/invalid inputs without clearing *np, causing numeric Parse<>() callers to form out-of-bounds pointers "" + n (C++ UB [expr.add]/4) and allowing Parse/Parse to accept whitespace-only strings as 0.0. Return NULL from TerminateNumber() on empty/invalid input (including after stripping leading whitespace) and check for NULL in all six Parse<>() callers, with regression tests in re2/testing/re2_test.cc. --- re2/re2.cc | 14 +++++++++++--- re2/testing/re2_test.cc | 8 ++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/re2/re2.cc b/re2/re2.cc index 2e00d2285..14c9eb044 100644 --- a/re2/re2.cc +++ b/re2/re2.cc @@ -1116,20 +1116,22 @@ static const int kMaxNumberLength = 32; // REQUIRES "buf" must have length at least nbuf. // Copies "str" into "buf" and null-terminates. // Overwrites *np with the new length. +// Returns NULL if the input is empty or invalid. static const char* TerminateNumber(char* buf, size_t nbuf, const char* str, size_t* np, bool accept_spaces) { size_t n = *np; - if (n == 0) return ""; + if (n == 0) return NULL; if (n > 0 && absl::ascii_isspace(*str)) { // We are less forgiving than the strtoxxx() routines and do not // allow leading spaces. We do allow leading spaces for floats. if (!accept_spaces) { - return ""; + return NULL; } while (n > 0 && absl::ascii_isspace(*str)) { n--; str++; } + if (n == 0) return NULL; } // Although buf has a fixed maximum size, we can still handle @@ -1159,7 +1161,7 @@ static const char* TerminateNumber(char* buf, size_t nbuf, const char* str, str--; } - if (n > nbuf-1) return ""; + if (n > nbuf-1) return NULL; memmove(buf, str, n); if (neg) { @@ -1176,6 +1178,7 @@ bool Parse(const char* str, size_t n, float* dest) { static const int kMaxLength = 200; char buf[kMaxLength+1]; str = TerminateNumber(buf, sizeof buf, str, &n, true); + if (str == NULL) return false; char* end; errno = 0; float r = strtof(str, &end); @@ -1192,6 +1195,7 @@ bool Parse(const char* str, size_t n, double* dest) { static const int kMaxLength = 200; char buf[kMaxLength+1]; str = TerminateNumber(buf, sizeof buf, str, &n, true); + if (str == NULL) return false; char* end; errno = 0; double r = strtod(str, &end); @@ -1207,6 +1211,7 @@ bool Parse(const char* str, size_t n, long* dest, int radix) { if (n == 0) return false; char buf[kMaxNumberLength+1]; str = TerminateNumber(buf, sizeof buf, str, &n, false); + if (str == NULL) return false; char* end; errno = 0; long r = strtol(str, &end, radix); @@ -1222,6 +1227,7 @@ bool Parse(const char* str, size_t n, unsigned long* dest, int radix) { if (n == 0) return false; char buf[kMaxNumberLength+1]; str = TerminateNumber(buf, sizeof buf, str, &n, false); + if (str == NULL) return false; if (str[0] == '-') { // strtoul() will silently accept negative numbers and parse // them. This module is more strict and treats them as errors. @@ -1283,6 +1289,7 @@ bool Parse(const char* str, size_t n, long long* dest, int radix) { if (n == 0) return false; char buf[kMaxNumberLength+1]; str = TerminateNumber(buf, sizeof buf, str, &n, false); + if (str == NULL) return false; char* end; errno = 0; long long r = strtoll(str, &end, radix); @@ -1298,6 +1305,7 @@ bool Parse(const char* str, size_t n, unsigned long long* dest, int radix) { if (n == 0) return false; char buf[kMaxNumberLength+1]; str = TerminateNumber(buf, sizeof buf, str, &n, false); + if (str == NULL) return false; if (str[0] == '-') { // strtoull() will silently accept negative numbers and parse // them. This module is more strict and treats them as errors. diff --git a/re2/testing/re2_test.cc b/re2/testing/re2_test.cc index 5c1822d7f..cb16c868a 100644 --- a/re2/testing/re2_test.cc +++ b/re2/testing/re2_test.cc @@ -904,6 +904,10 @@ TEST(RE2, FloatingPointFullMatchTypes) { ASSERT_TRUE(RE2::FullMatch("-100.", "(.*)", &v)); ASSERT_EQ(v, -100); ASSERT_TRUE(RE2::FullMatch("1e23", "(.*)", &v)); ASSERT_EQ(v, float{1e23}); ASSERT_TRUE(RE2::FullMatch(" 100", "(.*)", &v)); ASSERT_EQ(v, 100); + ASSERT_FALSE(RE2::FullMatch("", "(.*)", &v)); + ASSERT_FALSE(RE2::FullMatch(" ", "(.*)", &v)); + ASSERT_FALSE(RE2::FullMatch(" ", "(.*)", &v)); + ASSERT_FALSE(RE2::FullMatch("\t\n", "(.*)", &v)); ASSERT_TRUE(RE2::FullMatch(zeros + "1e23", "(.*)", &v)); ASSERT_EQ(v, float{1e23}); @@ -937,6 +941,10 @@ TEST(RE2, FloatingPointFullMatchTypes) { ASSERT_TRUE(RE2::FullMatch("-100.", "(.*)", &v)); ASSERT_EQ(v, -100); ASSERT_TRUE(RE2::FullMatch("1e23", "(.*)", &v)); ASSERT_EQ(v, double{1e23}); ASSERT_TRUE(RE2::FullMatch(" 100", "(.*)", &v)); ASSERT_EQ(v, 100); + ASSERT_FALSE(RE2::FullMatch("", "(.*)", &v)); + ASSERT_FALSE(RE2::FullMatch(" ", "(.*)", &v)); + ASSERT_FALSE(RE2::FullMatch(" ", "(.*)", &v)); + ASSERT_FALSE(RE2::FullMatch("\t\n", "(.*)", &v)); ASSERT_TRUE(RE2::FullMatch(zeros + "1e23", "(.*)", &v)); ASSERT_EQ(v, double{1e23}); From 696726f11529f5dd995a084196dddfcfcb44c563 Mon Sep 17 00:00:00 2001 From: hyc-alt <250169095+hyc-alt@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:48:45 +0800 Subject: [PATCH 2/2] re2: avoid int64_t overflow in RE2::Init max_mem * 2 / 3 options_.max_mem() * 2 / 3 overflows int64_t for max_mem > INT64_MAX / 2 (or < INT64_MIN / 2), triggering UBSan signed integer overflow or wrapping negative. Compute (max_mem / 3) * 2 + (max_mem % 3) * 2 / 3 instead and add RE2.MaxMemNoOverflow regression test. --- re2/re2.cc | 4 +++- re2/testing/re2_test.cc | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/re2/re2.cc b/re2/re2.cc index 14c9eb044..afe722ca5 100644 --- a/re2/re2.cc +++ b/re2/re2.cc @@ -261,7 +261,9 @@ void RE2::Init(absl::string_view pattern, const Options& options) { // Two thirds of the memory goes to the forward Prog, // one third to the reverse prog, because the forward // Prog has two DFAs but the reverse prog has one. - prog_ = suffix_regexp_->CompileToProg(options_.max_mem()*2/3); + int64_t max_mem = options_.max_mem(); + prog_ = suffix_regexp_->CompileToProg( + (max_mem / 3) * 2 + (max_mem % 3) * 2 / 3); if (prog_ == NULL) { if (options_.log_errors()) ABSL_LOG(ERROR) << "Error compiling '" << trunc(*pattern_) << "'"; diff --git a/re2/testing/re2_test.cc b/re2/testing/re2_test.cc index cb16c868a..5451106ef 100644 --- a/re2/testing/re2_test.cc +++ b/re2/testing/re2_test.cc @@ -1396,6 +1396,20 @@ TEST(RE2, BitstateCaptureBug) { EXPECT_FALSE(re.Match(s, 0, s.size(), RE2::UNANCHORED, NULL, 0)); } +TEST(RE2, MaxMemNoOverflow) { + RE2::Options opt; + opt.set_max_mem(INT64_MAX); + RE2 re("abc", opt); + ASSERT_TRUE(re.ok()); + EXPECT_TRUE(RE2::FullMatch("abc", re)); + EXPECT_TRUE(RE2::PartialMatch("xxabcyy", re)); + + opt.set_max_mem(INT64_MIN); + RE2 re_neg("abc", opt); + ASSERT_TRUE(re_neg.ok()); + EXPECT_TRUE(RE2::FullMatch("abc", re_neg)); +} + // C++ version of bug 609710. TEST(RE2, UnicodeClasses) { const std::string str = "ABCDEFGHI譚永鋒";