Skip to content

Commit

Permalink
Fix OOB read in JSLexer from invalid continuation sequence
Browse files Browse the repository at this point in the history
Summary:
For the JS source input shown from xxd:
```
2222 efbb bfef
```
which is two double quotes followed by the UTF-8 Byte Order Mark (BOM):
0xef 0xbb 0xbf

followed by the beginning of a second BOM. The second BOM is unterminated, and
the bug was that we were using the original start of the end of the directive to check
the second BOM instead of the current point in the buffer.

Reviewed By: tmikov

Differential Revision: D22187723

fbshipit-source-id: 21b4a91b38637a15972c37fbbadf1ca5b8721c4c
  • Loading branch information
Riley Dulin authored and facebook-github-bot committed Jun 23, 2020
1 parent 31e82ef commit e145200
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/Parser/JSLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,7 @@ bool JSLexer::isCurrentTokenADirective() {

// Byte-order mark \uFEFF is encoded as: ef bb bf
case 0xef:
if ((unsigned char)curCharPtr_[1] == 0xbb &&
(unsigned char)curCharPtr_[2] == 0xbf) {
if ((unsigned char)ptr[1] == 0xbb && (unsigned char)ptr[2] == 0xbf) {
ptr += 3;
continue;
} else {
Expand Down
11 changes: 11 additions & 0 deletions unittests/Parser/JSParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,15 @@ TEST(JSParserTest, TestRegExp) {
ASSERT_TRUE(parsed.hasValue());
}

TEST(JSParserTest, TestUnterminatedBOMInDirective) {
Context context;
// Begin an unterminated Byte Order Mark (BOM) after the first one.
JSParser parser(context, "\"\"\xef\xbb\xbf\xef");
auto parsed = parser.parse();
ASSERT_FALSE(parsed.hasValue());

SourceErrorManager &sm = context.getSourceErrorManager();
EXPECT_EQ(sm.getErrorCount(), 2);
}

}; // anonymous namespace

0 comments on commit e145200

Please sign in to comment.