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

IO::Buffer#resize: Free internal buffer if new size is zero #7569

Merged
merged 2 commits into from Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions io_buffer.c
Expand Up @@ -1422,6 +1422,11 @@ rb_io_buffer_resize(VALUE self, size_t size)
#endif

if (data->flags & RB_IO_BUFFER_INTERNAL) {
if (size == 0) {
io_buffer_free(data);
return;
}

void *base = realloc(data->base, size);

if (!base) {
Expand Down
18 changes: 18 additions & 0 deletions test/ruby/test_io_buffer.rb
Expand Up @@ -156,6 +156,24 @@ def test_resize_preserve
assert_equal message, buffer.get_string(0, message.bytesize)
end

def test_resize_zero_internal
buffer = IO::Buffer.new(1)

buffer.resize(0)
assert_equal 0, buffer.size

buffer.resize(1)
assert_equal 1, buffer.size
end

def test_resize_zero_external
buffer = IO::Buffer.for('1')

assert_raise IO::Buffer::AccessError do
buffer.resize(0)
end
end

def test_compare_same_size
buffer1 = IO::Buffer.new(1)
assert_equal buffer1, buffer1
Expand Down