prevent leakage of ptrops checks pragma disabling

This commit is contained in:
Dustin Brody 2022-03-10 09:19:14 +00:00
parent b87fd80b0f
commit fecc3cc5cb
No known key found for this signature in database
GPG Key ID: 3D7A11A0156519DC
1 changed files with 11 additions and 6 deletions

View File

@ -1,5 +1,5 @@
# stew # stew
# Copyright 2018-2019 Status Research & Development GmbH # Copyright 2018-2022 Status Research & Development GmbH
# Licensed under either of # Licensed under either of
# #
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
@ -36,9 +36,12 @@ template offset*[T](p: ptr T, count: int): ptr T =
# better optimizations. # better optimizations.
# We turn off checking here - too large counts is UB # We turn off checking here - too large counts is UB
{.checks: off.} {.push checks: off.}
let bytes = count * sizeof(T) let
cast[ptr T](offset(cast[pointer](p), bytes)) 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
@ -50,5 +53,7 @@ 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
{.checks: off.} {.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