Skip to content

Commit

Permalink
Fixed bug where, in some situations, scanner allowed invalid characte…
Browse files Browse the repository at this point in the history
…rs after a slash. Fixes #67
  • Loading branch information
mck89 committed Feb 14, 2024
1 parent 63dee90 commit f6e6810
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.16.0-dev"
"dev-master": "1.16.1-dev"
}
}
}
3 changes: 3 additions & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Changelog
==========

#### 1.16.1
* Fixed bug where, in some situations, scanner allowed invalid characters after a slash

#### 1.16.0
* Implemented ES2024 parser, no new syntax features have been introduced
* Fixed bug when parsing sequence expressions using older ES versions
Expand Down
30 changes: 19 additions & 11 deletions lib/Peast/Syntax/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -976,22 +976,30 @@ protected function consumeCommentsForCurrentToken()
}

/**
* Checks if the consumed or the scanned position follow a slash.
* Checks if the given position follows a slash.
*
* @param Position $position Additional position to check
* @param Position $position Position to check
*
* @return bool
*/
protected function isAfterSlash($position = null)
protected function isAfterSlash($position)
{
$consumedIndex = $this->getPosition()->getIndex();
$checkIndices = array($consumedIndex, $consumedIndex + 1);
if ($position) {
$checkIndices[] = $position->getIndex() - 1;
}
foreach ($checkIndices as $i) {
if ($i >= 0 && $this->charAt($i) === "/") {
return true;
//Start from the previous index and loop until the begin of the file is reached
$idx = $position->getIndex() - 1;
while ($idx >= 0) {
//Get the char at the index to check
$char = $this->charAt($idx);
//If the char is actually a slash check that it's not a multiline comment closing slash
if ($char === "/") {
return $idx === 0 || $this->charAt($idx - 1) !== "*";
}
//Skip whitespaces but not line terminators since regexps can't be multiline
elseif (in_array($char, $this->whitespaces) && !in_array($char, $this->lineTerminators)) {
$idx--;
}
//Different character, return
else {
break;
}
}
return false;
Expand Down
1 change: 1 addition & 0 deletions test/Peast/Syntax/ES2015/files/Literal/InvalidRegexp4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/**/@
5 changes: 5 additions & 0 deletions test/Peast/Syntax/ES2015/files/Literal/Regexp9.Render.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/ \ /.source;
/**************************************************/
/ \ /.source;
/**************************************************/
/ \ /.source;
50 changes: 50 additions & 0 deletions test/Peast/Syntax/ES2015/files/Literal/Regexp9.Tokens.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"type": "RegularExpression",
"value": "/ \\ /",
"location": {
"start": {
"line": 1,
"column": 0,
"index": 0
},
"end": {
"line": 1,
"column": 5,
"index": 5
}
}
},
{
"type": "Punctuator",
"value": ".",
"location": {
"start": {
"line": 1,
"column": 5,
"index": 5
},
"end": {
"line": 1,
"column": 6,
"index": 6
}
}
},
{
"type": "Identifier",
"value": "source",
"location": {
"start": {
"line": 1,
"column": 6,
"index": 6
},
"end": {
"line": 1,
"column": 12,
"index": 12
}
}
}
]
1 change: 1 addition & 0 deletions test/Peast/Syntax/ES2015/files/Literal/Regexp9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/ \ /.source
96 changes: 96 additions & 0 deletions test/Peast/Syntax/ES2015/files/Literal/Regexp9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"type": "Program",
"location": {
"start": {
"line": 1,
"column": 0,
"index": 0
},
"end": {
"line": 1,
"column": 12,
"index": 12
}
},
"leadingComments": [],
"trailingComments": [],
"body": [
{
"type": "ExpressionStatement",
"location": {
"start": {
"line": 1,
"column": 0,
"index": 0
},
"end": {
"line": 1,
"column": 12,
"index": 12
}
},
"leadingComments": [],
"trailingComments": [],
"expression": {
"type": "MemberExpression",
"location": {
"start": {
"line": 1,
"column": 0,
"index": 0
},
"end": {
"line": 1,
"column": 12,
"index": 12
}
},
"leadingComments": [],
"trailingComments": [],
"optional": false,
"object": {
"type": "RegExpLiteral",
"location": {
"start": {
"line": 1,
"column": 0,
"index": 0
},
"end": {
"line": 1,
"column": 5,
"index": 5
}
},
"leadingComments": [],
"trailingComments": [],
"value": "/ \\ /",
"raw": "/ \\ /",
"flags": "",
"pattern": " \\ "
},
"property": {
"type": "Identifier",
"location": {
"start": {
"line": 1,
"column": 6,
"index": 6
},
"end": {
"line": 1,
"column": 12,
"index": 12
}
},
"leadingComments": [],
"trailingComments": [],
"name": "source",
"rawName": "source"
},
"computed": false
}
}
],
"sourceType": "script"
}

0 comments on commit f6e6810

Please sign in to comment.