From c50ba2d1019b025d278e0644903b1358a70415b5 Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Fri, 19 Jan 2024 00:25:44 +0100 Subject: [PATCH] std.ArrayList.replaceRange: remove unneded overflow checks The code asserted that the range to be replaced is within bounds of `self.items`. This is now reflected in the doc comment. The old, wrong doc comment was copied from the `insert*` fns. With this assertion holding true, `start + len` is always within the address space and `start + new_items.len` is, at this point, always strictly within bounds of `self.items`. --- lib/std/array_list.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index 18b741f1c3..24d905454a 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -242,9 +242,9 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { /// Grows list if `len < new_items.len`. /// Shrinks list if `len > new_items.len`. /// Invalidates element pointers if this ArrayList is resized. - /// Asserts that the start index is in bounds or equal to the length. + /// Asserts that the range is in bounds. pub fn replaceRange(self: *Self, start: usize, len: usize, new_items: []const T) Allocator.Error!void { - const after_range = try addOrOom(start, len); + const after_range = start + len; const range = self.items[start..after_range]; if (range.len == new_items.len) @@ -257,7 +257,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { try self.insertSlice(after_range, rest); } else { @memcpy(range[0..new_items.len], new_items); - const after_subrange = try addOrOom(start, new_items.len); + const after_subrange = start + new_items.len; for (self.items[after_range..], 0..) |item, i| { self.items[after_subrange..][i] = item;