Skip to content

Commit

Permalink
fix: int.from_bytes: for signed=True: empty bytes casuses error; sign…
Browse files Browse the repository at this point in the history
… detect was wrong sometimes
  • Loading branch information
litlighilit committed Jul 18, 2024
1 parent f38c060 commit 1006e2c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/pylib/numTypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ proc transInOrder(outBuf: var openArray[char], inBuf: openArray[char], endiannes
else:
handleTargetOrder i

template highByte(b: PyBytes): uint8 =
template highByte(b: PyBytes, endian: Endianness, hi = b.len-1): uint8 =
uint8:
when BigEndian: b[0]
else: b[^1]
if endian == bigEndian : b.getChar 0
else: b.getChar hi

template signbitSet(b: uint8): bool =
(b and 0b1000_0000'u8) == 0b1000_0000'u8
Expand Down Expand Up @@ -156,8 +156,10 @@ proc from_bytes(res: var NimInt, bytes: PyBytes, byteorder: Endianness, signed=f
res = cast[NimInt](holder)
if signed:
let bLen = bytes.len
if bytes.highByte().signbitSet():
if bLen > 0 and bytes.highByte(byteorder, blen-1).signbitSet():
# res -= 1 shl (8 * bLen) <- the same as following line
res = res or lowestN_set0_and_rest_setP1[NimInt](totByteLen, bLen)

#debugecho "byte: ", res.toBin(64)

if not signed and res < 0:
Expand Down
3 changes: 3 additions & 0 deletions tests/tint_bytes_cvt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ test "int.{from,to}_bytes":
check (-2).to_bytes(2, "little", signed=true) == b"\xfe\xff"

checkpoint "int.from_bytes"
check NimInt.from_bytes(b"", "big", signed=true) == 0
check NimInt.from_bytes(b"", "big", signed=true) == 0
check NimInt.from_bytes(b"\x01\x00", "little") == 1
check NimInt.from_bytes(b"\x00\x01", "big") == 1
check NimInt.from_bytes(b"\xfe\xff", "little", signed=true) == -2
check NimInt.from_bytes(b"\xfe\xff", "big", signed=true) == -257
check NimInt.from_bytes(b"\x00\xff", "big", signed=true) == 255

0 comments on commit 1006e2c

Please sign in to comment.