From d6633ae1da1d441a3a641320340e602a2be2cb60 Mon Sep 17 00:00:00 2001 From: Tomohiro Date: Tue, 8 Oct 2024 06:18:11 +0900 Subject: [PATCH] Change how to multiply 1.5 to ints to reduce overflow (#24257) --- lib/pure/base64.nim | 2 +- lib/pure/collections/hashcommon.nim | 4 ++-- lib/pure/nimprof.nim | 2 +- lib/system/cellseqs_v1.nim | 2 +- lib/system/cellseqs_v2.nim | 2 +- lib/system/cellsets.nim | 2 +- lib/system/orc.nim | 3 ++- lib/system/strs_v2.nim | 2 +- lib/system/sysstr.nim | 2 +- 9 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/pure/base64.nim b/lib/pure/base64.nim index 591d22cc00..10fe9ee171 100644 --- a/lib/pure/base64.nim +++ b/lib/pure/base64.nim @@ -73,7 +73,7 @@ const const invalidChar = 255 -template encodeSize(size: int): int = (size * 4 div 3) + 6 +template encodeSize(size: int): int = (size div 3 + size) + 6 template encodeInternal(s, alphabet: typed): untyped = ## encodes `s` into base64 representation. diff --git a/lib/pure/collections/hashcommon.nim b/lib/pure/collections/hashcommon.nim index 17785c8c70..cf98f0f38b 100644 --- a/lib/pure/collections/hashcommon.nim +++ b/lib/pure/collections/hashcommon.nim @@ -32,11 +32,11 @@ proc nextTry(h, maxHash: Hash): Hash {.inline.} = proc mustRehash[T](t: T): bool {.inline.} = # If this is changed, make sure to synchronize it with `slotsNeeded` below assert(t.dataLen > t.counter) - result = (t.dataLen * 2 < t.counter * 3) or (t.dataLen - t.counter < 4) + result = (t.dataLen < (t.counter + t.counter div 2)) or (t.dataLen - t.counter < 4) proc slotsNeeded(count: Natural): int {.inline.} = # Make sure to synchronize with `mustRehash` above - result = nextPowerOfTwo(count * 3 div 2 + 4) + result = nextPowerOfTwo(count div 2 + count + 4) template rawGetKnownHCImpl() {.dirty.} = if t.dataLen == 0: diff --git a/lib/pure/nimprof.nim b/lib/pure/nimprof.nim index bf8367d1de..cba4cd9cf9 100644 --- a/lib/pure/nimprof.nim +++ b/lib/pure/nimprof.nim @@ -54,7 +54,7 @@ proc `==`(a, b: StackTrace): bool = # However a chain length of over 3000 is suspicious... var profileData: ProfileData - emptySlots = profileData.len * 3 div 2 + emptySlots = profileData.len div 2 + profileData.len maxChainLen = 0 totalCalls = 0 diff --git a/lib/system/cellseqs_v1.nim b/lib/system/cellseqs_v1.nim index 1a305aa428..b043dcaac7 100644 --- a/lib/system/cellseqs_v1.nim +++ b/lib/system/cellseqs_v1.nim @@ -22,7 +22,7 @@ proc contains(s: CellSeq, c: PCell): bool {.inline.} = return false proc resize(s: var CellSeq) = - s.cap = s.cap * 3 div 2 + s.cap = s.cap div 2 + s.cap let d = cast[PCellArray](alloc(s.cap * sizeof(PCell))) copyMem(d, s.d, s.len * sizeof(PCell)) dealloc(s.d) diff --git a/lib/system/cellseqs_v2.nim b/lib/system/cellseqs_v2.nim index c6c7b1a8e2..3339fb8eb0 100644 --- a/lib/system/cellseqs_v2.nim +++ b/lib/system/cellseqs_v2.nim @@ -17,7 +17,7 @@ type d: CellArray[T] proc resize[T](s: var CellSeq[T]) = - s.cap = s.cap * 3 div 2 + s.cap = s.cap div 2 + s.cap var newSize = s.cap * sizeof(CellTuple[T]) when compileOption("threads"): s.d = cast[CellArray[T]](reallocShared(s.d, newSize)) diff --git a/lib/system/cellsets.nim b/lib/system/cellsets.nim index 92036c2262..7815f928bd 100644 --- a/lib/system/cellsets.nim +++ b/lib/system/cellsets.nim @@ -144,7 +144,7 @@ proc cellSetPut(t: var CellSet, key: uint): PPageDesc = if x.key == key: return x h = nextTry(h, t.max) - if ((t.max+1)*2 < t.counter*3) or ((t.max+1)-t.counter < 4): + if ((t.max+1) < t.counter div 2 + t.counter) or ((t.max+1)-t.counter < 4): cellSetEnlarge(t) inc(t.counter) h = cast[int](key) and t.max diff --git a/lib/system/orc.nim b/lib/system/orc.nim index c02a249895..351dbf26d4 100644 --- a/lib/system/orc.nim +++ b/lib/system/orc.nim @@ -424,7 +424,8 @@ proc collectCycles() = rootsThreshold = 0 #cfprintf(cstderr, "[collectCycles] freed %ld, touched %ld new threshold %ld\n", j.freed, j.touched, rootsThreshold) elif rootsThreshold < high(int) div 4: - rootsThreshold = (if rootsThreshold <= 0: defaultThreshold else: rootsThreshold) * 3 div 2 + rootsThreshold = (if rootsThreshold <= 0: defaultThreshold else: rootsThreshold) + rootsThreshold = rootsThreshold div 2 + rootsThreshold when logOrc: cfprintf(cstderr, "[collectCycles] end; freed %ld new threshold %ld touched: %ld mem: %ld rcSum: %ld edges: %ld\n", j.freed, rootsThreshold, j.touched, getOccupiedMem(), j.rcSum, j.edges) diff --git a/lib/system/strs_v2.nim b/lib/system/strs_v2.nim index 404b4f78dc..c77ed6ad1e 100644 --- a/lib/system/strs_v2.nim +++ b/lib/system/strs_v2.nim @@ -61,7 +61,7 @@ template reallocPayload0(p: pointer; oldLen, newLen: int): ptr NimStrPayload = proc resize(old: int): int {.inline.} = if old <= 0: result = 4 elif old <= high(int16): result = old * 2 - else: result = old * 3 div 2 # for large arrays * 3/2 is better + else: result = old div 2 + old # for large arrays * 3/2 is better proc prepareAdd(s: var NimStringV2; addLen: int) {.compilerRtl.} = let newLen = s.len + addLen diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim index 3621c4960a..8cefe7601f 100644 --- a/lib/system/sysstr.nim +++ b/lib/system/sysstr.nim @@ -25,7 +25,7 @@ proc dataPointer(a: PGenericSeq, elemAlign, elemSize, index: int): pointer = proc resize(old: int): int {.inline.} = if old <= 0: result = 4 elif old < 65536: result = old * 2 - else: result = old * 3 div 2 # for large arrays * 3/2 is better + else: result = old div 2 + old # for large arrays * 3/2 is better when declared(allocAtomic): template allocStr(size: untyped): untyped =