diff options
author | Jean byroot Boussier <[email protected]> | 2023-10-05 09:43:59 +0200 |
---|---|---|
committer | git <[email protected]> | 2023-10-05 07:44:08 +0000 |
commit | f087f2c74c99ec5fed04896d3dc91ff76c2b16b8 (patch) | |
tree | a66f6c9ab91ac35c6e36df5e474b4a9750daf1e3 /ext/stringio | |
parent | 9d58f9382893a71d8badad605879c0120915fbee (diff) |
[ruby/stringio] StringIO#pread: handle 0 length like IO#pread
(https://github.com/ruby/stringio/pull/67)
Fix: https://github.com/ruby/stringio/issues/66
If length is 0, IO#pread don't even try to read the IO, it simply return
the buffer untouched if there is one or a new empty buffer otherwise.
It also doesn't validate the offset when length is 0.
cc @jdelStrother @kou
https://github.com/ruby/stringio/commit/37e9279337
Co-authored-by: Jean Boussier <[email protected]>
Diffstat (limited to 'ext/stringio')
-rw-r--r-- | ext/stringio/stringio.c | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 1feb9231db..d5753a97a0 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -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)); } |