Project

General

Profile

Actions

Bug #18805

closed

IO::Buffer is inconsistent when returning a string from an empty buffer

Added by procmarco (Marco Concetto Rudilosso) over 2 years ago. Updated 10 months ago.

Status:
Closed
Target version:
-
ruby -v:
Backport:
[ruby-core:108697]

Description

I’ve been using IO::Buffer and I found it to be slightly inconsistent when it comes to returning empty string for empty buffers.
for example, a slice of an allocated buffer with size = 0, returns "" with get_string, as an example:

buffer = IO::Buffer.new(5)
empty_buffer = buffer.slice(0,0)
puts empty_buffer.size # this prints 0
empty_buffer.get_string # this returns ""

but, if you create a buffer with IO::Buffer.new(0) then it stops working

empty_buffer = IO::Buffer.new(0)
puts empty_buffer.size # this prints 0
empty_buffer.get_string # this raises The buffer is not allocated! (IO::Buffer::AllocationError)

Is this working as intended? It would be good I think to have a consistent experience where the base case (buffer with size 0) always returns an empty string.
I have a prototype of a possible patch I could send upstream to fix it, which would check the size of the buffer and if 0 it would always return an empty string.


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #19542: Operations on zero-sized IO::Buffer are raisingClosedioquatix (Samuel Williams)Actions

Updated by jeremyevans0 (Jeremy Evans) about 1 year ago

  • Assignee set to ioquatix (Samuel Williams)

@ioquatix (Samuel Williams) Is this expected behavior or a bug?

Updated by ioquatix (Samuel Williams) 11 months ago

This is currently working as intended but we could consider changing the behaviour. I understand the use case.

Actions #3

Updated by ioquatix (Samuel Williams) 10 months ago

  • Related to Bug #19542: Operations on zero-sized IO::Buffer are raising added

Updated by ioquatix (Samuel Williams) 10 months ago

As a 2nd issue was created about the similar issue, I think it's worth improving.

See https://github.com/ruby/ruby/pull/9532 for the proposed changes.

Actions #6

Updated by ioquatix (Samuel Williams) 10 months ago

  • Backport deleted (2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0