Skip to content

Commit 449cda1

Browse files
committed
regcomp.c: incr ref cnt of re_intuit_string() return
This function returns an SV that it turns out may have its reference count decremented by a future call to re_intuit_start(). Thus, the caller doesn't get clear title to the returned SV. This is not documented. It is too late in the development cycle to properly fix this, but in this instance, it is a simple matter to increment the ref count of the returned scalar This fixes #17734.
1 parent f50fa03 commit 449cda1

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

regcomp.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21491,7 +21491,8 @@ SV *
2149121491
Perl_re_intuit_string(pTHX_ REGEXP * const r)
2149221492
{ /* Assume that RE_INTUIT is set */
2149321493
/* Returns an SV containing a string that must appear in the target for it
21494-
* to match */
21494+
* to match. CAUTION: the SV can be freed during execution of the regex
21495+
* engine */
2149521496

2149621497
struct regexp *const prog = ReANY(r);
2149721498
DECLARE_AND_GET_RE_DEBUG_FLAGS;
@@ -25077,7 +25078,9 @@ S_handle_names_wildcard(pTHX_ const char * wname, /* wildcard name to match */
2507725078

2507825079
/* Compile the subpattern consisting of the name being looked for */
2507925080
subpattern_re = compile_wildcard(wname, wname_len, FALSE /* /-i */ );
25080-
must = re_intuit_string(subpattern_re);
25081+
25082+
/* ref count incremented because regexec.c decrements it. GH #17734 */
25083+
must = SvREFCNT_inc(re_intuit_string(subpattern_re));
2508125084
prog = ReANY(subpattern_re);
2508225085

2508325086
/* If only nothing is matched, skip to where empty names are looked for */
@@ -25411,6 +25414,7 @@ S_handle_names_wildcard(pTHX_ const char * wname, /* wildcard name to match */
2541125414
#endif
2541225415

2541325416
SvREFCNT_dec_NN(subpattern_re);
25417+
SvREFCNT_dec(must);
2541425418
return found_matches;
2541525419
}
2541625420

t/re/pat_advanced.t

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,6 +2553,15 @@ EOF
25532553
{}, "Too large negative relative group number");
25542554
}
25552555

2556+
{ # GH #17734, ASAN use after free
2557+
fresh_perl_like('no warnings "experimental::uniprop_wildcards";
2558+
my $re = q<[[\p{name=/[Y-]+Z/}]]>;
2559+
eval { "\N{BYZANTINE MUSICAL SYMBOL PSILI}"
2560+
=~ /$re/ }; print $@ if $@; print "Done\n";',
2561+
qr/Done/,
2562+
{}, "GH #17734");
2563+
}
2564+
25562565

25572566
# !!! NOTE that tests that aren't at all likely to crash perl should go
25582567
# a ways above, above these last ones. There's a comment there that, like

0 commit comments

Comments
 (0)