Skip to content

Commit efe8351

Browse files
committed
Check arguments in pread
1 parent 4611e6c commit efe8351

3 files changed

Lines changed: 28 additions & 19 deletions

File tree

ext/java/org/jruby/ext/stringio/StringIO.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,14 +1213,17 @@ private RubyString preadCommon(ThreadContext context, int argc, IRubyObject arg0
12131213
}
12141214
case 2:
12151215
len = RubyNumeric.fix2int(arg0);
1216-
offset = RubyNumeric.fix2int(arg1);
12171216
if (!arg0.isNil()) {
1218-
len = RubyNumeric.fix2int(arg0);
1219-
12201217
if (len < 0) {
12211218
throw runtime.newArgumentError("negative length " + len + " given");
12221219
}
12231220
}
1221+
1222+
offset = RubyNumeric.fix2int(arg1);
1223+
if (offset < 0) {
1224+
throw runtime.newErrnoEINVALError("pread: Invalid offset argument");
1225+
}
1226+
12241227
break;
12251228
default:
12261229
throw runtime.newArgumentError(argc, 0, 2);
@@ -1235,10 +1238,6 @@ private RubyString preadCommon(ThreadContext context, int argc, IRubyObject arg0
12351238
return (RubyString) str;
12361239
}
12371240

1238-
if (offset < 0) {
1239-
throw runtime.newErrnoEINVALError("pread: Invalid offset argument");
1240-
}
1241-
12421241
if (isOutside(offset)) {
12431242
throw context.runtime.newEOFError();
12441243
}

ext/stringio/stringio.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ strio_substr(struct StringIO *ptr, long pos, long len, rb_encoding *enc)
161161
return enc_subseq(str, pos, len, enc);
162162
}
163163

164+
static VALUE
165+
strio_readbuf(struct StringIO *ptr, VALUE str)
166+
{
167+
if (!NIL_P(str)) {
168+
StringValue(str);
169+
rb_str_modify(str);
170+
}
171+
return str;
172+
}
173+
164174
#define StringIO(obj) get_strio(obj)
165175
#define StringIOForRead(obj) get_strio_for_read(obj)
166176

@@ -1684,11 +1694,7 @@ strio_read(int argc, VALUE *argv, VALUE self)
16841694

16851695
switch (argc) {
16861696
case 2:
1687-
str = argv[1];
1688-
if (!NIL_P(str)) {
1689-
StringValue(str);
1690-
rb_str_modify(str);
1691-
}
1697+
str = strio_readbuf(ptr, argv[1]);
16921698
/* fall through */
16931699
case 1:
16941700
if (!NIL_P(argv[0])) {
@@ -1753,6 +1759,8 @@ static VALUE
17531759
strio_pread(int argc, VALUE *argv, VALUE self)
17541760
{
17551761
VALUE rb_len, rb_offset, rb_buf;
1762+
struct StringIO *ptr = readable(self);
1763+
17561764
rb_scan_args(argc, argv, "21", &rb_len, &rb_offset, &rb_buf);
17571765
long len = NUM2LONG(rb_len);
17581766
long offset = NUM2LONG(rb_offset);
@@ -1761,19 +1769,19 @@ strio_pread(int argc, VALUE *argv, VALUE self)
17611769
rb_raise(rb_eArgError, "negative string size (or size too big): %" PRIsVALUE, rb_len);
17621770
}
17631771

1772+
if (offset < 0) {
1773+
rb_syserr_fail_str(EINVAL, rb_sprintf("pread: Invalid offset argument: %" PRIsVALUE, rb_offset));
1774+
}
1775+
1776+
rb_buf = strio_readbuf(ptr, rb_buf);
1777+
17641778
if (len == 0) {
17651779
if (NIL_P(rb_buf)) {
17661780
return rb_str_new("", 0);
17671781
}
17681782
return rb_buf;
17691783
}
17701784

1771-
if (offset < 0) {
1772-
rb_syserr_fail_str(EINVAL, rb_sprintf("pread: Invalid offset argument: %" PRIsVALUE, rb_offset));
1773-
}
1774-
1775-
struct StringIO *ptr = readable(self);
1776-
17771785
if (outside_p(ptr, offset)) {
17781786
rb_eof_error();
17791787
}

test/stringio/test_stringio.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,9 +831,11 @@ def test_pread
831831
assert_raise(EOFError) { f.pread(1, 5) }
832832
assert_raise(ArgumentError) { f.pread(-1, 0) }
833833
assert_raise(Errno::EINVAL) { f.pread(3, -1) }
834+
assert_raise(Errno::EINVAL) { f.pread(0, -1) }
835+
assert_raise(IOError) { StringIO.new(nil, "w").pread(3, 0) }
836+
assert_raise(TypeError) { f.pread(3, 0, []) }
834837

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

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

0 commit comments

Comments
 (0)