[PATCH] posix: Fix check_matching return value on allocation failure (bug 34949)

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Thu Nov 12 13:59:14 GMT 2026



On 12/11/26 10:54, Adhemerval Zanella Netto wrote:
> 
> 
> On 10/11/26 11:24, Yuvraj Opawat wrote:
>> From 040433e0aff08a32cda6c2611de0fbbe159287cb Mon Sep 17 00:00:00 2001
>> From: Yuvraj Opawat <opawatyuvraj@gmail.com <mailto:opawatyuvraj@gmail.com>>
>> Date: Tue, 10 Nov 2026 19:04:28 +0530
>> Subject: [PATCH] posix: Fix check_matching return value on allocation failure
>>  (bug 34949)
>>
>> check_matching returns the match position as an Idx; its documented
>> protocol is -1 for no match and -2 for an internal error.  However,
>> if the pattern contains a backreference (dfa->nbackref > 0) and an
>> allocation failure occurs in check_subexp_matching_top or
>> transit_state_bkref in the initialization block of check_matching,
>> the raw reg_errcode_t value (REG_ESPACE = 12) is returned instead of
>> -2.
>>
>> re_search_internal only guards against -2, so 12 passes the check and
>> is treated as a real match position: mctx.state_log[12] is read from
>> a slot that was never written (state_log is allocated with re_malloc
>> and filled only up to the highest index actually visited), and the
>> resulting NULL or uninitialized state pointer is dereferenced in
>> check_halt_state_context, causing a segmentation fault under memory
>> allocation failure.  This is reproducible, for example, with the
>> pattern "\(a\)*\1" and an interposed malloc made to fail at the
>> allocation of re_sub_match_top_t in match_ctx_add_subtop.
>>
>> Replace the two "return err;" statements with "return -2;", matching
>> every other error path in check_matching and restoring the documented
>> return protocol.
>>
>> Add bug-regex40 which forks a child for each of the first 64
>> allocations performed by regexec on the backreference pattern
>> "\(a\)*\1" with an empty subject string, makes exactly that
>> allocation fail via an in-process malloc/calloc/realloc interposer
>> (no LD_PRELOAD), and verifies that every child exits normally instead
>> of crashing and that the injected failures are actually exercised
>> (regexec surfaces allocation failures as REG_NOMATCH through its
>> return value).
>>
>> Checked on x86_64-linux-gnu.
>>
>> Signed-off-by: Yuvraj Opawat <opawatyuvraj@gmail.com <mailto:opawatyuvraj@gmail.com>>
> 
> LGTM, thanks.
> 
> Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
Just a note, this patch was mangled because it was sent as HTML. The differences only
affect whitespace and the trailing context line, and the code itself matches.

Please next time send as only text, otherwise patchwork [1] get confused.

[1] https://patchwork.sourceware.org/project/glibc/patch/CAOksv7dcDOvyho7WoVt8tvOwzVTJJ4QBwDt4dPrZXhggBeqeQg@mail.gmail.com/


More information about the Libc-alpha mailing list