commit c5c5a7371aa1eb55b6224186e329b8c5cd5e112c parent d4a76426e90c290714e5ff016b341cbf8e7da380 Author: Drew DeVault <sir@cmpwn.com> Date: Fri, 12 Feb 2021 14:10:46 -0500 rt::ensure: fix append to empty slice Diffstat:
M | rt/ensure.ha | | | 12 | +++++++++--- |
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/rt/ensure.ha b/rt/ensure.ha @@ -5,10 +5,16 @@ export type slice = struct { }; export fn ensure(s: *slice, membsz: size, length: size) void = { - // TODO: make sure length * membsz < SIZE_MAX - for (s.capacity < length) { - s.capacity *= 2; + let cap = s.capacity; + for (cap < length) { + assert(cap >= s.capacity, "slice out of memory (overflow)"); + if (cap == 0) { + cap = length; + } else { + cap *= 2; + }; }; + s.capacity = cap; let data = realloc(s.data, s.capacity * membsz); assert(data != null); s.data = data;