ANT-2026-KKC4QS4S Str partial-match stale capture registers underflow
Str.string_partial_match records the beginning and end of a captured group.
There was an implicit assumption that beginning <= end.
However if it reached the end of the matched string but not the end of the regexp, it was possible for the beginning of the group to be matched at iteration N of the star operator, and the end of the group to be matched at iteration N-1, resulting in beginning > end.
Str.matched_group is safe, it fails with an Invalid_argument on groups with beginning > end, but Str.replace_matched can crash, trying to memcpy end - beginning bytes.
The example in the ANT is ab partially matched against \(\(a\)b\)+. Group 2 has beginning = 2 and end = 1.
The fix is to record groups with beginning > end as not matched.
All of the following conditions are necessary:
Str.string_partial_match
Str.{replace_matched,global_replace,replace_first} on the same regexp after a call to Str.string_partial_match.
Str.group_beginning and Str.group_end and do your own string manipulation that is safe, because the String module would raise on out of bounds accesses. Similarly Str.matched_group is safe (uses String functions in its OCaml implementation)For example {|\(\(a\)b\)+|} as the regular expression in a call to Str.string_partial_match, followed by a reference to \2 in the argument of Str.replace_matched could be affected.
{
"cwe": [
"CWE-122"
],
"human_link": "https://github.com/ocaml/security-advisories/tree/main/advisories/2026/OSEC-2026-21.md",
"osv": "https://github.com/ocaml/security-advisories/tree/generated-osv/2026/OSEC-2026-21.json"
}