Skip to content

Fix GH-10634: Lexing memory corruption #10866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Zend/tests/gh10634.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
GH-10634 (Lexing memory corruption)
--FILE--
<?php
function test_input($input) {
try {
eval($input);
} catch(Throwable $e) {
var_dump($e->getMessage());
}
}

test_input("y&/*");
test_input("y&/**");
test_input("y&#");
test_input("y&# ");
test_input("y&//");
?>
--EXPECT--
string(36) "Unterminated comment starting line 1"
string(36) "Unterminated comment starting line 1"
string(36) "syntax error, unexpected end of file"
string(36) "syntax error, unexpected end of file"
string(36) "syntax error, unexpected end of file"
10 changes: 7 additions & 3 deletions Zend/zend_language_scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -1369,9 +1369,13 @@ TOKENS [;:,.|^&+-/*=%!~$<>?@]
ANY_CHAR [^]
NEWLINE ("\r"|"\n"|"\r\n")
OPTIONAL_WHITESPACE [ \n\r\t]*
MULTI_LINE_COMMENT "/*"([^*]*"*"+)([^*/][^*]*"*"+)*"/"
SINGLE_LINE_COMMENT "//".*[\n\r]
HASH_COMMENT "#"(([^[].*[\n\r])|[\n\r])
/* We don't use re2c with bounds checking, we just return 0 bytes if we read past the input.
* If we use wildcard matching for comments, we can read past the input, which crashes
* once we try to report a syntax error because the 0 bytes are not actually part of
* the token. We prevent this by not allowing 0 bytes, which already aren't valid anyway. */
MULTI_LINE_COMMENT "/*"([^*\x00]*"*"+)([^*/\x00][^*\x00]*"*"+)*"/"
SINGLE_LINE_COMMENT "//"[^\x00\n\r]*[\n\r]
HASH_COMMENT "#"(([^[\x00][^\x00\n\r]*[\n\r])|[\n\r])
WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_COMMENT}|{HASH_COMMENT})+
OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_COMMENT}|{HASH_COMMENT})*

Expand Down