#664·re2

Variant analysis: 1 unfixed sibling safety gap in re2

Author: x4evexnolCreated Sep 28, 2026Updated Sep 28, 2026

Summary

Variant analysis of historical fixes in re2 identified 1 code site where an integer-overflow guard, bounds check, or safe-allocation wrapper exists at one location but is missing at a structurally identical sibling location (same file, same function, or same pattern family) elsewhere in the codebase.

Each finding below is code-confirmed against the current HEAD (verified by cloning the repository fresh and checking the pattern is still present), with the exact file/line locations, the root cause, a description of the trigger path, and — where available — ASan/execution verification output or a proof-of-concept.

Note on origin: these were surfaced by an automated variant-analysis pipeline that diffs historical fix commits against sibling code paths, then has each candidate manually reviewed. I'm posting them together per-project rather than as separate issues to respect maintainer time. Happy to split, close, or reprioritize any of these as you see fit.


Finding 1: re2 — BitState::Search() int * int overflow in visit bitmap allocation

Verified against HEAD: 972a15ce (2026-01-22)

*- Project: re2 (google/re2)

  • Class: Heap-buffer-overflow write (via signed integer overflow in bitmap allocation size)
  • Status: Code-confirmed, exec-blocked
  • Sibling of: DFA state size computation uses int64_t for safety; BitState path does not*

Discovery method

Variant analysis. The re2 regex engine has two search backends: DFA (safe for all inputs) and BitState (fallback for complex regex features like backreferences/large programs). The DFA path uses int64_t for state size computations. The BitState path uses int for its visit bitmap allocation size.

Vulnerable code

re2/bitstate.cc, line 316 — BitState::Search():

cpp
int nvisited = prog_->list_count() * static_cast<int>(text.size() + 1);
// Integer overflow: both operands are int
// If list_count_ > 1 and text.size() near INT_MAX:
//   product wraps to small positive → undersized bitmap
//   SetVisited() writes past allocation
SparseSet visited(nvisited);
  • prog_->list_count() returns int (number of instructions in the regex program)
  • text.size() + 1 is cast to int from size_t — discards upper bits on 64-bit
  • For a regex with 3 capture groups and 65,000-byte text: 3 * 65001 = 195003 — safe
  • For a regex with 100 instruction lists and 21,474,836-byte text: 100 * 21474837 = 2,147,483,700 — overflows int (max 2,147,483,647) → wraps to -2,147,483,596 → converted to size_t for allocation → SIZE_MAX - 2B → allocation fails or allocates huge

The more dangerous case is when the overflow wraps to a SMALL positive value:

  • list_count_ * text.size() = 2^31 + small_val → wraps to -2^31 + small_val → size_t(-2^31 + small_val) = SIZE_MAX - 2^31 + small_val → too big → allocation failure (safe!)
  • Actually, the wraparound to a small positive requires list_count_ * text.size() = 2^32 + small which is unlikely with realistic values

More realistic overflow: int nvisited can overflow to a NEGATIVE value, which when passed to SparseSet(nvisited) and converted to size_t, becomes a massive number → allocation failure → DoS only.

But there IS a path where text.size() is cast from size_t to int first (truncating on 64-bit): if text.size() = 0x100000001 (4 GiB + 1), the static_cast<int> gives 1, and visited bitmap is undersized.

Sibling pattern

The DFA path (re2/dfa.cc:456-458) uses int64_t one_state for state size computation — recognizing that allocation sizes can overflow 32-bit. The BitState path uses bare int:

cpp
// SAFE (DFA path, dfa.cc:456):
int64_t one_state = sizeof(State) + nnext * sizeof(std::atomic<State*>) + ...;

// VULNERABLE (BitState path, bitstate.cc:316):
int nvisited = prog_->list_count() * static_cast<int>(text.size() + 1);

Trigger path

  1. Craft a regex pattern with many instruction lists (backreferences + captures)
  2. Provide input text with length near INT_MAX or with 64-bit truncation
  3. → BitState::Search() → nvisited overflows → undersized/negative allocation
  4. → SetVisited() writes past bitmap buffer

Fuzz harness: re2_fuzzer (an OSS-Fuzz/fuzzbench harness) — BUT the harness caps input at 64 bytes, preventing the text.size() overflow path. A SWE-Test Pro task would need a harness that removes this cap for the BitState coverage path.

Sibling-gap quality

Moderate. Same codebase, two search backends with structurally similar allocation patterns. The DFA path proactively uses int64_t; the BitState path uses bare int. The fix pattern is clear: use int64_t for nvisited multiplication.


Analysis date: 2026-09-22. Code-confirmed, not execution-verified.


Methodology

For each historical vulnerability fix in the codebase, we identified the safe pattern the fix introduced (an overflow guard, a safe allocator wrapper, a bounds check) and searched the rest of the codebase for structurally identical code that predates or postdates the fix but never received it. Each candidate was then manually verified against the current HEAD listed above.

Happy to provide anything else that would help triage — additional PoC inputs, a minimal patch following the existing safe-sibling pattern, or a written reproduction script.