summary refs log tree commit diff stats
path: root/lib/pure/collections
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-04-29 07:42:47 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-04-29 07:42:47 +0200
commitc8b2e65dbb13ea3e301e26a75557966d7d06df64 (patch)
tree923494d48a32b347ec85eb496d10c0422e4dd9d7 /lib/pure/collections
parentb899713832a9fd9523117dfccc3b893825bad454 (diff)
downloadNim-c8b2e65dbb13ea3e301e26a75557966d7d06df64.tar.gz
critbits: don't rely on terminating zero
Diffstat (limited to 'lib/pure/collections')
-rw-r--r--lib/pure/collections/critbits.nim7
1 files changed, 4 insertions, 3 deletions
diff --git a/lib/pure/collections/critbits.nim b/lib/pure/collections/critbits.nim
index ed8583ae5..95fffdf88 100644
--- a/lib/pure/collections/critbits.nim
+++ b/lib/pure/collections/critbits.nim
@@ -74,8 +74,9 @@ proc rawInsert[T](c: var CritBitTree[T], key: string): Node[T] =
     var newByte = 0
     block blockX:
       while newbyte < key.len:
-        if newbyte >= it.key.len or it.key[newbyte] != key[newbyte]:
-          newotherbits = it.key[newbyte].ord xor key[newbyte].ord
+        let ch = if newbyte < it.key.len: it.key[newbyte] else: '\0'
+        if ch != key[newbyte]:
+          newotherbits = ch.ord xor key[newbyte].ord
           break blockX
         inc newbyte
       if newbyte < it.key.len:
@@ -85,7 +86,7 @@ proc rawInsert[T](c: var CritBitTree[T], key: string): Node[T] =
     while (newOtherBits and (newOtherBits-1)) != 0:
       newOtherBits = newOtherBits and (newOtherBits-1)
     newOtherBits = newOtherBits xor 255
-    let ch = it.key[newByte]
+    let ch = if newByte < it.key.len: it.key[newByte] else: '\0'
     let dir = (1 + (ord(ch) or newOtherBits)) shr 8
 
     var inner: Node[T]