Skip to content

Commit 3b8804a

Browse files
committed
(perl #131836) avoid a use-after-free after parsing a "sub" keyword
The: d = skipspace(d); can reallocate linestr in the test case, invalidating s. This would end up in PL_bufptr from the embedded (PL_bufptr = s) in the TOKEN() macro. Assigning s to PL_bufptr and restoring s from PL_bufptr allows lex_next_chunk() to adjust the pointer to the reallocated buffer.
1 parent 43272d2 commit 3b8804a

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

t/comp/parser_run.t

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ BEGIN {
1010
}
1111

1212
require './test.pl';
13-
plan(2);
13+
plan(3);
1414

1515
# [perl #130814] can reallocate lineptr while looking ahead for
1616
# "Missing $ on loop variable" diagnostic.
@@ -31,5 +31,13 @@ EOS
3131
Unrecognized character \xD5; marked by <-- HERE after ${ <-- HERE near column 4 at - line 1.
3232
EXPECT
3333

34+
fresh_perl_is(<<'EOS', <<'EXPECTED', {}, "use after free (#131836)");
35+
${sub#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
36+
EOS
37+
Missing right curly or square bracket at - line 1, at end of line
38+
syntax error at - line 1, at EOF
39+
Execution of - aborted due to compilation errors.
40+
EXPECTED
41+
3442
__END__
3543
# ex: set ts=8 sts=4 sw=4 et:

toke.c

+2
Original file line numberDiff line numberDiff line change
@@ -6222,8 +6222,10 @@ Perl_yylex(pTHX)
62226222
break;
62236223
}
62246224
if (strEQs(s, "sub")) {
6225+
PL_bufptr = s;
62256226
d = s + 3;
62266227
d = skipspace(d);
6228+
s = PL_bufptr;
62276229
if (*d == ':') {
62286230
PL_expect = XTERM;
62296231
break;

0 commit comments

Comments
 (0)