Skip to content

Commit a63ff98

Browse files
authored
Merge pull request #271 from ahoppen/pr/range-in-text-precondition
Add precondition that a token’s range returned by the Parser is in the source file
2 parents cb7298f + a098d1b commit a63ff98

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Sources/SwiftSyntax/RawSyntax.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,14 @@ fileprivate struct TokenData {
155155
if hasCustomText {
156156
// Copy the full token text, including trivia.
157157
let startOffset = Int(data.range.offset)
158-
var charPtr = UnsafeMutableRawPointer(curPtr).assumingMemoryBound(to: UInt8.self)
158+
let length = Int(data.range.length)
159159
let utf8 = source.utf8
160+
precondition(startOffset <= utf8.count)
161+
precondition(startOffset + length <= utf8.count)
160162
let begin = utf8.index(utf8.startIndex, offsetBy: startOffset)
161-
let end = utf8.index(begin, offsetBy: Int(data.range.length))
163+
let end = utf8.index(begin, offsetBy: length)
164+
165+
var charPtr = UnsafeMutableRawPointer(curPtr).assumingMemoryBound(to: UInt8.self)
162166
for ch in utf8[begin..<end] {
163167
charPtr.pointee = ch
164168
charPtr = charPtr.successor()

0 commit comments

Comments
 (0)