Skip to content

Commit

Permalink
Throw ArgumentError if unsafe_SecretBuffer!() is passed NULL
Browse files Browse the repository at this point in the history
Previously, if given a NULL `Cstring` we would blithely call `strlen()`
on it, which resulted in a segfault.  It is better if we throw an
exception instead.
  • Loading branch information
staticfloat committed Jun 10, 2022
1 parent ada860f commit f79bca4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 7 additions & 1 deletion base/secretbuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ function SecretBuffer!(d::Vector{UInt8})
s
end

unsafe_SecretBuffer!(s::Cstring) = unsafe_SecretBuffer!(convert(Ptr{UInt8}, s), Int(ccall(:strlen, Csize_t, (Cstring,), s)))
function unsafe_SecretBuffer!(s::Cstring)
if s == C_NULL
throw(ArgumentError("cannot convert NULL to SecretBuffer"))
end
len = Int(ccall(:strlen, Csize_t, (Cstring,), s))
unsafe_SecretBuffer!(convert(Ptr{UInt8}, s), len)
end
function unsafe_SecretBuffer!(p::Ptr{UInt8}, len=1)
s = SecretBuffer(sizehint=len)
for i in 1:len
Expand Down
4 changes: 4 additions & 0 deletions test/secretbuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,8 @@ using Test
@test hash(sb1, UInt(5)) === hash(sb2, UInt(5))
shred!(sb1); shred!(sb2)
end
@testset "NULL initialization" begin
null_ptr = Cstring(C_NULL)
@test_throws ArgumentError Base.unsafe_SecretBuffer!(null_ptr)
end
end

0 comments on commit f79bca4

Please sign in to comment.