Skip to content

Commit b949a7a

Browse files
committed
Fix recovery for UTF-8 decoding of continuation byte
The check here should be inverted.
1 parent 2d42e8d commit b949a7a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Sources/SwiftParser/Lexer/UnicodeScalarExtensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ extension Unicode.Scalar {
181181
if encodedBytes == 1 || !Unicode.Scalar(curByte).isStartOfUTF8Character {
182182
// Skip until we get the start of another character. This is guaranteed to
183183
// at least stop at the nul at the end of the buffer.
184-
while let peeked = peek(), Unicode.Scalar(peeked).isStartOfUTF8Character {
184+
while let peeked = peek(), !Unicode.Scalar(peeked).isStartOfUTF8Character {
185185
_ = advance()
186186
}
187187
return nil

Tests/SwiftParserTest/LexerTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,23 @@ public class LexerTests: XCTestCase {
886886
}
887887
}
888888

889+
func testInvalidUtf8_3() {
890+
let sourceBytes: [UInt8] = [0xfd, 0x41] // 0x41 == "A"
891+
892+
lex(sourceBytes) { lexemes in
893+
guard lexemes.count == 2 else {
894+
return XCTFail("Expected 2 lexemes, got \(lexemes.count)")
895+
}
896+
AssertRawBytesLexeme(
897+
lexemes[0],
898+
kind: .identifier,
899+
leadingTrivia: [0xfd],
900+
text: [0x41],
901+
error: TokenDiagnostic(.invalidUtf8, byteOffset: 0)
902+
)
903+
}
904+
}
905+
889906
func testInterpolatedString() {
890907
AssertLexemes(
891908
#"""

0 commit comments

Comments
 (0)