mirror of
https://github.com/ruby/ruby.git
synced 2025-09-22 03:53:58 +02:00
[ruby/zlib] Synchronize access to zstream to prevent segfault in multithreaded use
I'm not sure whether this handles all multithreaded use cases,
but this handles the example that crashes almost immediately
and does 10,000,000 total deflates using 100 separate threads.
To prevent the tests from taking forever, the committed test
for this uses only 10,000 deflates across 10 separate threads,
which still causes a segfault in the previous implementation
almost immediately.
Fixes [Bug #17803]
4b1023b3f2
This commit is contained in:
parent
218c3b2548
commit
b3d62a77d9
2 changed files with 93 additions and 1 deletions
|
@ -4,6 +4,7 @@ require 'test/unit'
|
|||
require 'stringio'
|
||||
require 'tempfile'
|
||||
require 'tmpdir'
|
||||
require 'securerandom'
|
||||
|
||||
begin
|
||||
require 'zlib'
|
||||
|
@ -503,6 +504,66 @@ if defined? Zlib
|
|||
assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }
|
||||
z.close
|
||||
end
|
||||
|
||||
def test_multithread_deflate
|
||||
zd = Zlib::Deflate.new
|
||||
|
||||
s = "x" * 10000
|
||||
(0...10).map do |x|
|
||||
Thread.new do
|
||||
1000.times { zd.deflate(s) }
|
||||
end
|
||||
end.each do |th|
|
||||
th.join
|
||||
end
|
||||
ensure
|
||||
zd&.finish
|
||||
zd&.close
|
||||
end
|
||||
|
||||
def test_multithread_inflate
|
||||
zi = Zlib::Inflate.new
|
||||
|
||||
s = Zlib.deflate("x" * 10000)
|
||||
(0...10).map do |x|
|
||||
Thread.new do
|
||||
1000.times { zi.inflate(s) }
|
||||
end
|
||||
end.each do |th|
|
||||
th.join
|
||||
end
|
||||
ensure
|
||||
zi&.finish
|
||||
zi&.close
|
||||
end
|
||||
|
||||
def test_recursive_deflate
|
||||
zd = Zlib::Deflate.new
|
||||
|
||||
s = SecureRandom.random_bytes(1024**2)
|
||||
assert_raise(Zlib::BufError) do
|
||||
zd.deflate(s) do
|
||||
zd.deflate(s)
|
||||
end
|
||||
end
|
||||
ensure
|
||||
zd&.finish
|
||||
zd&.close
|
||||
end
|
||||
|
||||
def test_recursive_inflate
|
||||
zi = Zlib::Inflate.new
|
||||
|
||||
s = Zlib.deflate(SecureRandom.random_bytes(1024**2))
|
||||
|
||||
assert_raise(Zlib::DataError) do
|
||||
zi.inflate(s) do
|
||||
zi.inflate(s)
|
||||
end
|
||||
end
|
||||
ensure
|
||||
zi&.close
|
||||
end
|
||||
end
|
||||
|
||||
class TestZlibGzipFile < Test::Unit::TestCase
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue