Skip to content

Commit

Permalink
(perl #131836) avoid a use-after-free after parsing a "sub" keyword
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tonycoz committed Aug 28, 2017
1 parent 43272d2 commit 3b8804a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 9 additions & 1 deletion t/comp/parser_run.t
Expand Up @@ -10,7 +10,7 @@ BEGIN {
}

require './test.pl';
plan(2);
plan(3);

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

fresh_perl_is(<<'EOS', <<'EXPECTED', {}, "use after free (#131836)");
${sub#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EOS
Missing right curly or square bracket at - line 1, at end of line
syntax error at - line 1, at EOF
Execution of - aborted due to compilation errors.
EXPECTED

__END__
# ex: set ts=8 sts=4 sw=4 et:
2 changes: 2 additions & 0 deletions toke.c
Expand Up @@ -6222,8 +6222,10 @@ Perl_yylex(pTHX)
break;
}
if (strEQs(s, "sub")) {
PL_bufptr = s;
d = s + 3;
d = skipspace(d);
s = PL_bufptr;
if (*d == ':') {
PL_expect = XTERM;
break;
Expand Down

0 comments on commit 3b8804a

Please sign in to comment.