| 58 | } |
| 59 | |
| 60 | void array_reserve(Context & context, Array & arr, uint64_t newCapacity, uint32_t stride, LineInfo * at) { |
| 61 | if ( arr.isLocked() ) context.throw_error_at(at, "can't change capacity of a locked array"); |
| 62 | if ( arr.capacity >= newCapacity ) return; |
| 63 | // Explicit mul_overflow guard: stride is uint32, newCapacity is uint64. |
| 64 | // The product can overflow uint64 when newCapacity > UINT64_MAX / stride |
| 65 | // (with stride > 0). Panic before passing a wrap-around byte count to |
| 66 | // the heap allocator (where it would silently allocate a small block |
| 67 | // and the next memcpy would overrun random memory). |
| 68 | if ( stride && newCapacity > UINT64_MAX / uint64_t(stride) ) { |
| 69 | context.throw_error_at(at, "array_reserve: capacity*stride overflows uint64 [capacity=%llu] [stride=%u]", (unsigned long long)newCapacity, stride); |
| 70 | } |
| 71 | uint64_t memSize64 = newCapacity * uint64_t(stride); |
| 72 | const char * prev_comment = arr.data ? context.heap->get_comment(arr.data) : nullptr; |
| 73 | char * newData = nullptr; |
| 74 | if ( context.verySafeContext ) { |
| 75 | newData = (char *)context.allocate(memSize64, at); |
| 76 | if ( newData && arr.data ) { |
| 77 | memcpy(newData, arr.data, arr.size*stride); |
| 78 | } |
| 79 | } else { |
| 80 | newData = (char *)context.reallocate(arr.data, arr.capacity*stride, memSize64, at); |
| 81 | } |
| 82 | context.heap->mark_comment(newData, prev_comment ? prev_comment : "array"); |
| 83 | if ( newData != arr.data ) { |
| 84 | // memcpy(newData, arr.data, arr.capacity); |
| 85 | arr.data = newData; |
| 86 | } |
| 87 | arr.capacity = newCapacity; |
| 88 | } |
| 89 | |
| 90 | void array_resize ( Context & context, Array & arr, uint64_t newSize, uint32_t stride, bool zero, LineInfo * at ) { |
| 91 | if ( arr.isLocked() ) context.throw_error_at(at, "can't resize locked array"); |
no test coverage detected