Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,13 @@ strio_pread(int argc, VALUE *argv, VALUE self)
rb_raise(rb_eArgError, "negative string size (or size too big): %" PRIsVALUE, rb_len);
}

if (len == 0) {
if (NIL_P(rb_buf)) {
return rb_str_new("", 0);
}
return rb_buf;
}

if (offset < 0) {
rb_syserr_fail_str(EINVAL, rb_sprintf("pread: Invalid offset argument: %" PRIsVALUE, rb_offset));
}
Expand Down
7 changes: 7 additions & 0 deletions test/stringio/test_stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,13 @@ def test_pread
assert_raise(EOFError) { f.pread(1, 5) }
assert_raise(ArgumentError) { f.pread(-1, 0) }
assert_raise(Errno::EINVAL) { f.pread(3, -1) }

assert_equal "".b, StringIO.new("").pread(0, 0)
assert_equal "".b, StringIO.new("").pread(0, -10)

buf = "stale".b
assert_equal "stale".b, StringIO.new("").pread(0, 0, buf)
assert_equal "stale".b, buf
end

def test_size
Expand Down