Skip to content

re2: fix TerminateNumber OOB pointer arithmetic / whitespace float parse and max_mem overflow - #705

Open
hyc-alt wants to merge 2 commits into
google:mainfrom
hyc-alt:fix/re2-terminate-number-and-max-mem
Open

hyc-alt wants to merge 2 commits into
google:mainfrom
hyc-alt:fix/re2-terminate-number-and-max-mem

Conversation

@hyc-alt

@hyc-alt hyc-alt commented Nov 27, 2026

Copy link
Copy Markdown

Summary

This PR fixes two bugs in re2/re2.cc:

  1. TerminateNumber() out-of-bounds pointer arithmetic and whitespace-only float misparse (re2/re2.cc:1119-1314):

    • TerminateNumber() previously returned "" on empty/invalid inputs without clearing *np, causing all 6 Parse<>() callers to compute out-of-bounds pointers "" + n (C++ UB [expr.add]/4).
    • For Parse<float> / Parse<double> (accept_spaces == true), whitespace-only strings stripped n to 0 and returned "", where strtof("", &end) / strtod("", &end) returned 0.0 with end == str + 0 and errno == 0, causing RE2::FullMatch(" ", "(.*)", &f) to return true with 0.0.
    • Fixed by returning NULL from TerminateNumber() on empty/invalid inputs (including after stripping leading whitespace) and checking if (str == NULL) return false; in all 6 Parse<>() callers.
  2. Signed 64-bit integer overflow in RE2::Init options_.max_mem() * 2 / 3 (re2/re2.cc:264):

    • 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.
    • Fixed with (max_mem / 3) * 2 + (max_mem % 3) * 2 / 3, which is algebraically identical to trunc(2 * max_mem / 3) across [INT64_MIN, INT64_MAX].

Verification

  • Built with Clang -fsanitize=address,undefined -fno-sanitize-recover=all -O1 -g -DNDEBUG.
  • Before: RE2::FullMatch(" ", "(.*)", &f) returned true (0.0); opt.set_max_mem(INT64_MAX) aborted with UBSan signed integer overflow: 9223372036854775807 * 2 cannot be represented in type 'int64_t'.
  • After: 0 compiler warnings, 0 ASan/UBSan errors; re2_test passes 79/79 (including new whitespace float/double and RE2.MaxMemNoOverflow tests).

…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<float>/Parse<double>
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.
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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant