Skip to content

Commit 739b4b4

Browse files
committed
Fix handling of unterminated comment with trailing newline
Fixes nikic#688.
1 parent 17f4781 commit 739b4b4

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Version 4.7.0-dev
77
* [PHP 8.0] Added support for match expressions. These are represented using a new `Expr\Match_`
88
containing `MatchArm`s.
99

10+
### Fixed
11+
12+
* Fixed missing error for unterminated comment with trailing newline (#688).
13+
1014
Version 4.6.0 (2020-07-02)
1115
--------------------------
1216

lib/PhpParser/Lexer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ protected function postprocessTokens(ErrorHandler $errorHandler) {
152152
$this->handleInvalidCharacterRange($filePos, $filePos + 1, $line, $errorHandler);
153153
}
154154

155-
if ($token[0] === \T_COMMENT && preg_match('/(\r\n|\n|\r)$/D', $token[1], $matches)) {
155+
if ($token[0] === \T_COMMENT && substr($token[1], 0, 2) !== '/*'
156+
&& preg_match('/(\r\n|\n|\r)$/D', $token[1], $matches)) {
156157
$trailingNewline = $matches[0];
157158
$token[1] = substr($token[1], 0, -strlen($trailingNewline));
158159
$this->tokens[$i] = $token;

test/PhpParser/LexerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function testError($code, $messages) {
3535
public function provideTestError() {
3636
return [
3737
["<?php /*", ["Unterminated comment from 1:7 to 1:9"]],
38+
["<?php /*\n", ["Unterminated comment from 1:7 to 2:1"]],
3839
["<?php \1", ["Unexpected character \"\1\" (ASCII 1) from 1:7 to 1:7"]],
3940
["<?php \0", ["Unexpected null byte from 1:7 to 1:7"]],
4041
// Error with potentially emulated token

0 commit comments

Comments
 (0)