fixes #22387; Undefined behavior when with hash(...) (#22404)

* fixes #22387; Undefined behavior when with hash(...)

* fixes vm

* fixes nimscript

(cherry picked from commit 47d06d3d4c)
This commit is contained in:
ringabout
2023-08-08 13:42:08 +08:00
committed by narimiran
parent f553288db3
commit 287fbc5fb5

View File

@@ -319,16 +319,24 @@ proc murmurHash(x: openArray[byte]): Hash =
h1: uint32
i = 0
template impl =
var j = stepSize
while j > 0:
dec j
k1 = (k1 shl 8) or (ord(x[i+j])).uint32
# body
while i < n * stepSize:
var k1: uint32
when defined(js) or defined(sparc) or defined(sparc64):
var j = stepSize
while j > 0:
dec j
k1 = (k1 shl 8) or (ord(x[i+j])).uint32
when nimvm:
impl()
else:
k1 = cast[ptr uint32](unsafeAddr x[i])[]
when declared(copyMem):
copyMem(addr k1, addr x[i], 4)
else:
impl()
inc i, stepSize
k1 = imul(k1, c1)