Merge pull request #108 from status-im/sqz

don't disable checks in ptrops
This commit is contained in:
tersec 2022-03-16 20:24:54 +00:00 committed by GitHub
commit 419903c9a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 14 deletions

View File

@ -1,5 +1,5 @@
# byteutils # byteutils
# Copyright (c) 2018 Status Research & Development GmbH # Copyright (c) 2018-2022 Status Research & Development GmbH
# Licensed and distributed under either of # Licensed and distributed under either of
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). # * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). # * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
@ -29,7 +29,7 @@ proc readHexChar*(c: char): byte
of 'a'..'f': result = byte(ord(c) - ord('a') + 10) of 'a'..'f': result = byte(ord(c) - ord('a') + 10)
of 'A'..'F': result = byte(ord(c) - ord('A') + 10) of 'A'..'F': result = byte(ord(c) - ord('A') + 10)
else: else:
raise newException(ValueError, $c & " is not a hexademical character") raise newException(ValueError, $c & " is not a hexadecimal character")
template skip0xPrefix(hexStr: string): int = template skip0xPrefix(hexStr: string): int =
## Returns the index of the first meaningful char in `hexStr` by skipping ## Returns the index of the first meaningful char in `hexStr` by skipping

View File

@ -34,14 +34,8 @@ template offset*[T](p: ptr T, count: int): ptr T =
# Actual behavior is wrapping, but this may be revised in the future to enable # Actual behavior is wrapping, but this may be revised in the future to enable
# better optimizations. # better optimizations.
let bytes = cast[uint](count) * uint(sizeof(T))
# We turn off checking here - too large counts is UB cast[ptr T](offset(cast[pointer](p), cast[int](bytes)))
{.push checks: off.}
let
bytes = count * sizeof(T)
res = cast[ptr T](offset(cast[pointer](p), bytes))
{.pop.}
res
template distance*(a, b: pointer): int = template distance*(a, b: pointer): int =
## Number of bytes between a and b - undefined behavior when difference ## Number of bytes between a and b - undefined behavior when difference
@ -53,7 +47,4 @@ template distance*(a, b: pointer): int =
template distance*[T](a, b: ptr T): int = template distance*[T](a, b: ptr T): int =
# Number of elements between a and b - undefined behavior when difference # Number of elements between a and b - undefined behavior when difference
# exceeds what can be represented in an int # exceeds what can be represented in an int
{.push checks: off.} distance(cast[pointer](a), cast[pointer](b)) div sizeof(T)
let res = distance(cast[pointer](a), cast[pointer](b)) div sizeof(T)
{.pop.}
res