Skip to content

Commit

Permalink
Check overflow in bzalloc (#38)
Browse files Browse the repository at this point in the history
* check overflow in bzalloc

* add assert that typemax(Csize_t) ≥ typemax(Cint)

* update CI

* fix julia compat

* Add missing blank line
  • Loading branch information
nhz2 authored Dec 17, 2024
1 parent 49d7cc3 commit 5ce971b
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/libbzip2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ mutable struct BZStream
opaque::Ptr{Cvoid}
end

bzalloc(::Ptr{Cvoid}, m::Cint, n::Cint) = ccall(:jl_malloc, Ptr{Cvoid}, (Cint,), m*n)
@assert typemax(Csize_t) typemax(Cint)

function bzalloc(::Ptr{Cvoid}, m::Cint, n::Cint)::Ptr{Cvoid}
s, f = Base.Checked.mul_with_overflow(m, n)
if f || signbit(s)
C_NULL
else
ccall(:jl_malloc, Ptr{Cvoid}, (Csize_t,), s%Csize_t)
end
end
bzfree(::Ptr{Cvoid}, p::Ptr{Cvoid}) = ccall(:jl_free, Cvoid, (Ptr{Cvoid},), p)

function BZStream()
Expand Down

0 comments on commit 5ce971b

Please sign in to comment.