Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add null pointer checks to avoid segfaults #72

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions src/compression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ end
# -------

function TranscodingStreams.initialize(codec::ZstdCompressor)
if codec.cstream.ptr == C_NULL
error("codec use after free")
end
code = initialize!(codec.cstream, codec.level)
if iserror(code)
zstderror(codec.cstream, code)
Expand All @@ -102,6 +105,9 @@ function TranscodingStreams.finalize(codec::ZstdCompressor)
end

function TranscodingStreams.startproc(codec::ZstdCompressor, mode::Symbol, error::Error)
if codec.cstream.ptr == C_NULL
error("codec use after free")
end
code = reset!(codec.cstream, 0 #=unknown source size=#)
if iserror(code)
error[] = ErrorException("zstd error")
Expand All @@ -111,6 +117,9 @@ function TranscodingStreams.startproc(codec::ZstdCompressor, mode::Symbol, error
end

function TranscodingStreams.process(codec::ZstdCompressor, input::Memory, output::Memory, error::Error)
if codec.cstream.ptr == C_NULL
error("codec use after free")
end
cstream = codec.cstream
ibuffer_starting_pos = UInt(0)
if codec.endOp == LibZstd.ZSTD_e_end &&
Expand Down
9 changes: 9 additions & 0 deletions src/decompression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ end
# -------

function TranscodingStreams.initialize(codec::ZstdDecompressor)
if codec.dstream.ptr == C_NULL
error("codec use after free")
end
code = initialize!(codec.dstream)
if iserror(code)
zstderror(codec.dstream, code)
Expand All @@ -57,6 +60,9 @@ function TranscodingStreams.finalize(codec::ZstdDecompressor)
end

function TranscodingStreams.startproc(codec::ZstdDecompressor, mode::Symbol, error::Error)
if codec.dstream.ptr == C_NULL
error("codec use after free")
end
code = reset!(codec.dstream)
if iserror(code)
error[] = ErrorException("zstd error")
Expand All @@ -66,6 +72,9 @@ function TranscodingStreams.startproc(codec::ZstdDecompressor, mode::Symbol, err
end

function TranscodingStreams.process(codec::ZstdDecompressor, input::Memory, output::Memory, error::Error)
if codec.dstream.ptr == C_NULL
error("codec use after free")
end
dstream = codec.dstream
dstream.ibuffer.src = input.ptr
dstream.ibuffer.size = input.size
Expand Down
35 changes: 35 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,41 @@ include("utils.jl")
@test CodecZstd.find_decompressed_size(v) == CodecZstd.ZSTD_CONTENTSIZE_ERROR
end

@testset "use after free doesn't segfault" begin
@testset "$(Codec)" for Codec in (ZstdCompressor, ZstdDecompressor)
codec = Codec()
TranscodingStreams.initialize(codec)
TranscodingStreams.finalize(codec)
data = [0x00,0x01]
GC.@preserve data let m = TranscodingStreams.Memory(pointer(data), length(data))
try
TranscodingStreams.expectedsize(codec, m)
catch
end
try
TranscodingStreams.minoutsize(codec, m)
catch
end
try
TranscodingStreams.initialize(codec)
catch
end
try
TranscodingStreams.startproc(codec, :read, TranscodingStreams.Error())
catch
end
try
TranscodingStreams.process(codec, m, m, TranscodingStreams.Error())
catch
end
try
TranscodingStreams.finalize(codec)
catch
end
end
end
end

include("compress_endOp.jl")
include("static_only_tests.jl")
end
Loading