Skip to content

Commit

Permalink
Detect truncated frames to avoid infinite loop (#47)
Browse files Browse the repository at this point in the history
* detect truncated frames

* add check that code > 0

* fix tests for 1.6
  • Loading branch information
nhz2 authored May 19, 2024
1 parent 4c799fe commit 4b8aecd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/decompression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ function TranscodingStreams.process(codec::ZstdDecompressor, input::Memory, outp
error[] = ErrorException("zstd error")
return Δin, Δout, :error
else
return Δin, Δout, code == 0 ? :end : :ok
if code == 0
return Δin, Δout, :end
elseif input.size == 0 && code > 0
error[] = ErrorException("zstd frame truncated. Expected at least $(code) more bytes")
return Δin, Δout, :error
else
return Δin, Δout, :ok
end
end
end

Expand Down
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ using Random
using TranscodingStreams
using Test

Random.seed!(1234)

@testset "Zstd Codec" begin
codec = ZstdCompressor()
@test codec isa ZstdCompressor
Expand All @@ -20,6 +22,20 @@ using Test
@test read(ZstdDecompressorStream(IOBuffer(data))) == b"foo"
@test read(ZstdDecompressorStream(IOBuffer(vcat(data, data)))) == b"foofoo"

@testset "Truncated frames" begin
# issue #24
@test_throws ErrorException transcode(ZstdDecompressor, UInt8[])
for trial in 1:1000
local uncompressed_data = rand(UInt8, rand(0:100))
local compressed_data = transcode(ZstdCompressor, uncompressed_data)
local L = length(compressed_data)
for n in 0:L-1
@test_throws ErrorException transcode(ZstdDecompressor, compressed_data[1:n])
end
@test transcode(ZstdDecompressor, compressed_data) == uncompressed_data
end
end

@test ZstdCompressorStream <: TranscodingStreams.TranscodingStream
@test ZstdDecompressorStream <: TranscodingStreams.TranscodingStream

Expand Down

0 comments on commit 4b8aecd

Please sign in to comment.