Skip to content

Commit 6821e4b

Browse files
authored
compressable: fix RuntimeError with large zstd compressed data (#5006)
**Which issue(s) this PR fixes**: Fixes #5005 **What this PR does / why we need it**: This PR will fix RuntimeError with large zstd compressed data **Docs Changes**: Not needed because this feature has not been released yet. **Release Note**: Should be described with #4657 Signed-off-by: Shizuo Fujita <[email protected]>
1 parent c5cf521 commit 6821e4b

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/fluent/plugin/compressable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ def string_decompress_gzip(compressed_data)
7979

8080
def string_decompress_zstd(compressed_data)
8181
io = StringIO.new(compressed_data)
82+
reader = Zstd::StreamReader.new(io)
8283
out = ''
8384
loop do
84-
reader = Zstd::StreamReader.new(io)
8585
# Zstd::StreamReader needs to specify the size of the buffer
8686
out << reader.read(1024)
8787
# Zstd::StreamReader doesn't provide unused data, so we have to manually adjust the position
@@ -117,8 +117,8 @@ def io_decompress_gzip(input, output)
117117
end
118118

119119
def io_decompress_zstd(input, output)
120+
reader = Zstd::StreamReader.new(input)
120121
loop do
121-
reader = Zstd::StreamReader.new(input)
122122
# Zstd::StreamReader needs to specify the size of the buffer
123123
v = reader.read(1024)
124124
output.write(v)

test/plugin/test_compressable.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,29 @@ def compress_assert_equal(expected, actual)
8383
assert_equal '', decompress('')
8484
assert_equal '', decompress('', output_io: StringIO.new)
8585
end
86+
87+
test 'decompress large zstd compressed data' do
88+
src1 = SecureRandom.random_bytes(1024)
89+
src2 = SecureRandom.random_bytes(1024)
90+
src3 = SecureRandom.random_bytes(1024)
91+
92+
zstd_compressed_data = compress(src1, type: :zstd) + compress(src2, type: :zstd) + compress(src3, type: :zstd)
93+
assert_equal src1 + src2 + src3, decompress(zstd_compressed_data, type: :zstd)
94+
end
95+
96+
test 'decompress large zstd compressed data with input_io and output_io' do
97+
src1 = SecureRandom.random_bytes(1024)
98+
src2 = SecureRandom.random_bytes(1024)
99+
src3 = SecureRandom.random_bytes(1024)
100+
101+
zstd_compressed_data = compress(src1, type: :zstd) + compress(src2, type: :zstd) + compress(src3, type: :zstd)
102+
103+
input_io = StringIO.new(zstd_compressed_data)
104+
output_io = StringIO.new
105+
output_io.set_encoding(src1.encoding)
106+
107+
decompress(input_io: input_io, output_io: output_io, type: :zstd)
108+
assert_equal src1 + src2 + src3, output_io.string
109+
end
86110
end
87111
end

0 commit comments

Comments
 (0)