Skip to content

Commit 5b90d28

Browse files
committed
Prevent string manipulation in the utf-16 view
1 parent ed00e70 commit 5b90d28

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -375,19 +375,11 @@ extension WindowBaseCommandHandler {
375375
guard selection.start.character > 0,
376376
selection.start.character < line.utf16.count
377377
else { return false }
378-
let substring = String(
379-
line.utf16[line.utf16.startIndex..<(line.index(
380-
line.utf16.startIndex,
381-
offsetBy: selection.start.character,
382-
limitedBy: line.utf16.endIndex
383-
) ?? line.utf16.endIndex)]
384-
) ?? String(
385-
line[line.startIndex..<(line.index(
386-
line.startIndex,
387-
offsetBy: selection.start.character,
388-
limitedBy: line.endIndex
389-
) ?? line.endIndex)]
390-
)
378+
let substring = line[line.utf16.startIndex..<(line.index(
379+
line.utf16.startIndex,
380+
offsetBy: selection.start.character,
381+
limitedBy: line.utf16.endIndex
382+
) ?? line.utf16.endIndex)]
391383
return substring.allSatisfy { $0.isWhitespace }
392384
}()
393385

Tool/Sources/SuggestionModel/EditorInformation.swift

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,30 @@ public struct EditorInformation {
107107
}
108108
var content = rangeLines
109109
if !content.isEmpty {
110-
let dropLastCount = max(
111-
0,
112-
content[content.endIndex - 1].utf16.count - range.end.character
113-
)
114-
content[content.endIndex - 1] = String(
115-
content[content.endIndex - 1].utf16.dropLast(dropLastCount)
116-
) ?? String(
117-
content[content.endIndex - 1].dropLast(dropLastCount)
118-
)
119-
let dropFirstCount = max(0, range.start.character)
120-
content[0] = String(content[0].utf16.dropFirst(dropFirstCount))
121-
?? String(content[0].dropFirst(dropFirstCount))
110+
let lastLine = content[content.endIndex - 1]
111+
let droppedEndIndex = lastLine.utf16.index(
112+
lastLine.utf16.startIndex,
113+
offsetBy: range.end.character,
114+
limitedBy: lastLine.utf16.endIndex
115+
) ?? lastLine.utf16.endIndex
116+
content[content.endIndex - 1] = if droppedEndIndex > lastLine.utf16.startIndex {
117+
String(lastLine[..<droppedEndIndex])
118+
} else {
119+
""
120+
}
121+
122+
let firstLine = content[0]
123+
let droppedStartIndex = firstLine.utf16.index(
124+
firstLine.utf16.startIndex,
125+
offsetBy: range.start.character,
126+
limitedBy: firstLine.utf16.endIndex
127+
) ?? firstLine.utf16.endIndex
128+
129+
content[0] = if droppedStartIndex < firstLine.utf16.endIndex {
130+
String(firstLine[droppedStartIndex...])
131+
} else {
132+
""
133+
}
122134
}
123135
return (content.joined(), rangeLines)
124136
}

Tool/Sources/WorkspaceSuggestionService/Filespace+SuggestionService.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,22 @@ public extension Filespace {
8282
"Generating suggestion with invalid range"
8383
)
8484

85-
let uff16View = editingLine.utf16
85+
let utf16View = editingLine.utf16
8686

87-
let startIndex = uff16View.index(
88-
uff16View.startIndex,
87+
let startIndex = utf16View.index(
88+
utf16View.startIndex,
8989
offsetBy: max(0, presentingSuggestion.range.start.character),
90-
limitedBy: uff16View.endIndex
91-
) ?? uff16View.startIndex
90+
limitedBy: utf16View.endIndex
91+
) ?? utf16View.startIndex
9292

93-
let endIndex = uff16View.index(
94-
uff16View.startIndex,
93+
let endIndex = utf16View.index(
94+
utf16View.startIndex,
9595
offsetBy: cursorPosition.character,
96-
limitedBy: uff16View.endIndex
97-
) ?? uff16View.endIndex
96+
limitedBy: utf16View.endIndex
97+
) ?? utf16View.endIndex
9898

9999
if endIndex > startIndex {
100-
return String(uff16View[startIndex..<endIndex]) ?? ""
100+
return String(editingLine[startIndex..<endIndex])
101101
}
102102

103103
return ""

0 commit comments

Comments
 (0)