From b0cee7c0c5077cc65f220574a60a522dd268ba00 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Tue, 27 Jun 2023 08:20:02 +0200 Subject: [PATCH] uint arithmetic for pointers (#22159) pointers are not signed and arithmetic may correctly cross int.max threshold this PR only fixes 2 occurances - there are plenty however in the std lib (cherry picked from commit cb40f11e6c0a8f8df8b429f239483c408c7cc668) --- lib/system/alloc.nim | 2 +- lib/system/mm/malloc.nim | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim index 06d7de090a..f54acd0d82 100644 --- a/lib/system/alloc.nim +++ b/lib/system/alloc.nim @@ -992,7 +992,7 @@ template instantiateForRegion(allocator: untyped) {.dirty.} = proc realloc0Impl(p: pointer, oldSize, newSize: Natural): pointer = result = realloc(allocator, p, newSize) if newSize > oldSize: - zeroMem(cast[pointer](cast[int](result) + oldSize), newSize - oldSize) + zeroMem(cast[pointer](cast[uint](result) + uint(oldSize)), newSize - oldSize) when false: proc countFreeMem(): int = diff --git a/lib/system/mm/malloc.nim b/lib/system/mm/malloc.nim index d41dce705f..b24b6f1e0c 100644 --- a/lib/system/mm/malloc.nim +++ b/lib/system/mm/malloc.nim @@ -22,7 +22,7 @@ proc reallocImpl(p: pointer, newSize: Natural): pointer = proc realloc0Impl(p: pointer, oldsize, newSize: Natural): pointer = result = realloc(p, newSize.csize_t) if newSize > oldSize: - zeroMem(cast[pointer](cast[int](result) + oldSize), newSize - oldSize) + zeroMem(cast[pointer](cast[uint](result) + uint(oldSize)), newSize - oldSize) proc deallocImpl(p: pointer) = c_free(p)