Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

Commit 50d2422

Browse files
committed
improve the safety of arithmetics and indexed access
1 parent 8c60003 commit 50d2422

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

crates/rome_lsp/src/line_index.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ impl LineIndex {
174174
position += offset;
175175

176176
if line_index < prev_end {
177+
// SAFETY: `line_index` is checked to be less than `prev_end`.
178+
// Since `prev_end` is at most `end.line + 1`, and the value of
179+
// `end.line` returned by `.line_col()` is at most
180+
// `self.newlines.len() - 1`, the index is guaranteed to never
181+
// be out-of-bounds
177182
self.newlines[line_index] = position;
178183
} else {
179184
self.newlines.insert(line_index, position);
@@ -193,14 +198,18 @@ impl LineIndex {
193198
match prev_len.cmp(&next_len) {
194199
Ordering::Less => {
195200
while line_index < self.newlines.len() {
196-
self.newlines[line_index] += next_len - prev_len;
201+
self.newlines[line_index] = self.newlines[line_index]
202+
.checked_add(next_len - prev_len)
203+
.context("arithmetics overflow")?;
197204
line_index += 1;
198205
}
199206
}
200207
Ordering::Equal => {}
201208
Ordering::Greater => {
202209
while line_index < self.newlines.len() {
203-
self.newlines[line_index] -= prev_len - next_len;
210+
self.newlines[line_index] = self.newlines[line_index]
211+
.checked_sub(prev_len - next_len)
212+
.context("arithmetics overflow")?;
204213
line_index += 1;
205214
}
206215
}

0 commit comments

Comments
 (0)