Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ uuid = "43287f4e-b6f4-7ad1-bb20-aadabca52c3d"
authors = ["Lilith Orion Hafner <[email protected]> and contributors"]
version = "1.3.0"

[deps]
CheckedSizeProduct = "1b2cc9da-92e3-4b71-9788-30fc110eefda"

[compat]
CheckedSizeProduct = "1"
julia = "1"

[extras]
Expand Down
18 changes: 7 additions & 11 deletions src/PtrArrays.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module PtrArrays

using CheckedSizeProduct

export malloc, free, PtrArray

"""
Expand Down Expand Up @@ -32,19 +34,13 @@ end
# Because Core.checked_dims is buggy 😢
checked_dims(elsize::Int; message) = elsize
function checked_dims(elsize::Int, d0::Int, d::Int...; message)
overflow = false
neg = (d0+1) < 1
zero = false # of d0==0 we won't have overflow since we go left to right
len = d0
for di in d
len, o = Base.mul_with_overflow(len, di)
zero |= di === 0
overflow |= o
neg |= (di+1) < 1
throw_argument_error = let message = message
() -> throw(ArgumentError("invalid $message dimensions"))
end
len = checked_size_product((d0, d...))
(len isa Int) || throw_argument_error()
len, o = Base.mul_with_overflow(len, elsize)
err = o | neg | overflow & !zero
err && throw(ArgumentError("invalid $message dimensions"))
o && throw_argument_error()
len
end

Expand Down
Loading