Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add upper limit on initial buffer size in MessagePack::Unpacker
Currently, the initial buffer size is specified in rb_ary_new2() or rb_hash_new_capa(). If a huge size is specified, a large amount of memory is allocated and system memory might be depleted. We want to unpack the data received over the network. However the service may stop due to large amount of memory allocation with crafted data. So this patch add upper limit on initial buffer size. If the buffer runs out, Ruby API will be reallocated automatically. ## Test code ```ruby require "msgpack" puts "msgpack version: #{MessagePack::VERSION}" unpacker = MessagePack::Unpacker.new unpacker.feed_each("\xDF\x20\x00\x00\x00") {} puts "Memory Usage: #{`ps -o rss= -p #{Process.pid}`.strip} KB" ``` ## Before Before it apply this patch, it allocates 8 GB memory on my environment. ``` $ ruby -v test.rb ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [x86_64-linux] msgpack version: 1.7.2 Memory Usage: 8403320 KB ``` ## After ``` ruby -v test.rb ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [x86_64-linux] msgpack version: 1.7.2 Memory Usage: 14480 KB ```
- Loading branch information