authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-19 18:57:57-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-21 12:30:37-07:00
logd99f55b7cf3b3b15ccbef224f157725b91e40bea
tree20e375d2731789ae1634b1015baed484ce961f86
parent2561b1eb6729a000bfa77f2dc0d242ddcc9c4866

std.ArrayList: add missing assertion in appendSliceAssumeCapacity


1 files changed, 10 insertions(+), 9 deletions(-)

lib/std/array_list.zig+10-9
...@@ -227,10 +227,11 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {...@@ -227,10 +227,11 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
227 /// Append the slice of items to the list, asserting the capacity is already227 /// Append the slice of items to the list, asserting the capacity is already
228 /// enough to store the new items. **Does not** invalidate pointers.228 /// enough to store the new items. **Does not** invalidate pointers.
229 pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void {229 pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void {
230 const oldlen = self.items.len;230 const old_len = self.items.len;
231 const newlen = self.items.len + items.len;231 const new_len = old_len + items.len;
232 self.items.len = newlen;232 assert(new_len <= self.capacity);
233 mem.copy(T, self.items[oldlen..], items);233 self.items.len = new_len;
234 mem.copy(T, self.items[old_len..], items);
234 }235 }
235236
236 pub usingnamespace if (T != u8) struct {} else struct {237 pub usingnamespace if (T != u8) struct {} else struct {
...@@ -570,11 +571,11 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ...@@ -570,11 +571,11 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
570 /// Append the slice of items to the list, asserting the capacity is enough571 /// Append the slice of items to the list, asserting the capacity is enough
571 /// to store the new items.572 /// to store the new items.
572 pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void {573 pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void {
573 const oldlen = self.items.len;574 const old_len = self.items.len;
574 const newlen = self.items.len + items.len;575 const new_len = old_len + items.len;
575576 assert(new_len <= self.capacity);
576 self.items.len = newlen;577 self.items.len = new_len;
577 mem.copy(T, self.items[oldlen..], items);578 mem.copy(T, self.items[old_len..], items);
578 }579 }
579580
580 /// Append a value to the list `n` times.581 /// Append a value to the list `n` times.