don't disable checks in ptrops

This commit is contained in:
Dustin Brody 2022-03-16 17:02:32 +00:00
parent 67b43e2dba
commit bb73567168
No known key found for this signature in database
GPG Key ID: 3D7A11A0156519DC
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 = count * sizeof(T)
# We turn off checking here - too large counts is UB cast[ptr T](offset(cast[pointer](p), 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