varints module: critical bugfix

This commit is contained in:
Andreas Rumpf 2018-06-22 03:19:30 +02:00
commit cc11aa9698

View file

@ -19,7 +19,7 @@ proc readVu64*(z: openArray[byte]; pResult: var uint64): int =
return 1 return 1
if z[0] <= 248: if z[0] <= 248:
if z.len < 2: return 0 if z.len < 2: return 0
pResult = (uint64 z[0] - 241) * 256 + uint64 z[1] + 240 pResult = (uint64 z[0] - 241) * 256 + z[1].uint64 + 240
return 2 return 2
if z.len < int(z[0]-246): return 0 if z.len < int(z[0]-246): return 0
if z[0] == 249: if z[0] == 249:
@ -135,6 +135,13 @@ when isMainModule:
if encodeZigzag(decodeZigzag(test)) != test: if encodeZigzag(decodeZigzag(test)) != test:
echo "Failure for ", test, " ", encodeZigzag(decodeZigzag(test)), " ", decodeZigzag(test) echo "Failure for ", test, " ", encodeZigzag(decodeZigzag(test)), " ", decodeZigzag(test)
for test in 0u64..300u64:
let wrLen = writeVu64(dest, test)
let rdLen = readVu64(dest, got)
assert wrLen == rdLen
if got != test:
echo "BUG! expected: ", test, " got: ", got, " z0: ", dest[0]
# check this also works for floats: # check this also works for floats:
for test in [0.0, 0.1, 2.0, +Inf, Nan, NegInf]: for test in [0.0, 0.1, 2.0, +Inf, Nan, NegInf]:
let t = cast[uint64](test) let t = cast[uint64](test)