Skip to content

Fix recovery for UTF-8 decoding of continuation byte #1430

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 2 commits into from
Mar 23, 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: 0 additions & 24 deletions Sources/SwiftParser/Lexer/Cursor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -649,26 +649,6 @@ extension Lexer.Cursor {
return nil
}

/// If this is the opening delimiter of a raw string literal, return the number
/// of `#` in the raw string delimiter.
/// Assumes that the parser is currently pointing at the character after the first `#`.
/// In other words, the first `#` is expected to already be consumed.
mutating func legacyAdvanceIfOpeningRawStringDelimiter() -> Int? {
assert(self.previous == UInt8(ascii: "#"))

var tmp = self
var length = 1
while tmp.advance(matching: "#") {
length += 1
}

if tmp.is(at: #"""#) {
self = tmp
return length
}
return nil
}

/// If we are positioned at the start of a multiline string delimiter, consume
/// that delimiter and return `true`, otherwise return `false`.
///
Expand Down Expand Up @@ -1515,8 +1495,6 @@ extension Lexer.Cursor {
/// Lexes a single character in a string literal, handling escape sequences
/// like `\n` or `\u{1234}` as a a single character.
mutating func lexCharacterInStringLiteral(stringLiteralKind: StringLiteralKind, delimiterLength: Int) -> CharacterLex {
let charStart = self

switch self.peek() {
case UInt8(ascii: #"""#):
let quote = Unicode.Scalar(self.advance()!)
Expand Down Expand Up @@ -1578,8 +1556,6 @@ extension Lexer.Cursor {
return .error(kind)
}
default:
_ = self.advance()
self = charStart
guard let charValue = self.advanceValidatingUTF8Character() else {
return .error(.invalidUtf8)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Lexer/UnicodeScalarExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ extension Unicode.Scalar {
if encodedBytes == 1 || !Unicode.Scalar(curByte).isStartOfUTF8Character {
// Skip until we get the start of another character. This is guaranteed to
// at least stop at the nul at the end of the buffer.
while let peeked = peek(), Unicode.Scalar(peeked).isStartOfUTF8Character {
while let peeked = peek(), !Unicode.Scalar(peeked).isStartOfUTF8Character {
_ = advance()
}
return nil
Expand Down
17 changes: 17 additions & 0 deletions Tests/SwiftParserTest/LexerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,23 @@ public class LexerTests: XCTestCase {
}
}

func testInvalidUtf8_3() {
let sourceBytes: [UInt8] = [0xfd, 0x41] // 0x41 == "A"

lex(sourceBytes) { lexemes in
guard lexemes.count == 2 else {
return XCTFail("Expected 2 lexemes, got \(lexemes.count)")
}
AssertRawBytesLexeme(
lexemes[0],
kind: .identifier,
leadingTrivia: [0xfd],
text: [0x41],
error: TokenDiagnostic(.invalidUtf8, byteOffset: 0)
)
}
}

func testInterpolatedString() {
AssertLexemes(
#"""
Expand Down