summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2023-08-08 13:42:08 +0800
committerGitHub <noreply@github.com>2023-08-08 13:42:08 +0800
commit47d06d3d4cb85a0c7c2273864f6088a5e8521f44 (patch)
tree90d14ae5ebe88ce6612265ee3cf63f751ca74cf7
parent0219c5a60740689f343f3261611219425e9f9531 (diff)
downloadNim-47d06d3d4cb85a0c7c2273864f6088a5e8521f44.tar.gz
fixes #22387; Undefined behavior when with hash(...) (#22404)
* fixes #22387; Undefined behavior when with hash(...)

* fixes vm

* fixes nimscript
-rw-r--r--lib/pure/hashes.nim20
1 files changed, 14 insertions, 6 deletions
diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim
index daa7f9366..ad164d6d3 100644
--- a/lib/pure/hashes.nim
+++ b/lib/pure/hashes.nim
@@ -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)