Skip to content

Commit 4ed1d6b

Browse files
committed
Cannot read into the underlying string
Fix https://hackerone.com/reports/3716987
1 parent 1393e2b commit 4ed1d6b

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ private IRubyObject readCommon(ThreadContext context, int argc, IRubyObject arg0
11101110
final RubyString string;
11111111
switch (argc) {
11121112
case 2:
1113-
str = readBuf(arg1);
1113+
str = readBuf(context, ptr, arg1);
11141114
case 1:
11151115
if (!arg0.isNil()) {
11161116
len = RubyNumeric.fix2int(arg0);
@@ -1202,7 +1202,7 @@ private RubyString preadCommon(ThreadContext context, int argc, IRubyObject arg0
12021202

12031203
switch (argc) {
12041204
case 3:
1205-
str = readBuf(arg2);
1205+
str = readBuf(context, ptr, arg2);
12061206
case 2:
12071207
len = RubyNumeric.fix2int(arg0);
12081208
if (!arg0.isNil()) {
@@ -1694,12 +1694,16 @@ private static IRubyObject substrString(RubyString ch, IRubyObject str, Ruby run
16941694
return str;
16951695
}
16961696

1697-
private static IRubyObject readBuf(IRubyObject str) {
1698-
if (!str.isNil()) {
1699-
str = str.convertToString();
1700-
modifyString((RubyString) str);
1697+
private static IRubyObject readBuf(ThreadContext context, StringIOData ptr, IRubyObject arg) {
1698+
if (!arg.isNil()) {
1699+
arg = arg.convertToString();
1700+
RubyString str = (RubyString) arg;
1701+
modifyString(str);
1702+
if (str == ptr.string) {
1703+
throw context.runtime.newArgumentError("cannot read into the underlying string");
1704+
}
17011705
}
1702-
return str;
1706+
return arg;
17031707
}
17041708

17051709
// MRI: strio_write

ext/stringio/stringio.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ strio_readbuf(struct StringIO *ptr, VALUE str)
167167
if (!NIL_P(str)) {
168168
StringValue(str);
169169
rb_str_modify(str);
170+
if (str == ptr->string) {
171+
rb_raise(rb_eArgError, "cannot read into the underlying string");
172+
}
170173
}
171174
return str;
172175
}

test/stringio/test_stringio.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,8 @@ def test_read
765765
s = ""
766766
f.read(nil, s)
767767
assert_equal(Encoding::ASCII_8BIT, s.encoding, bug20418)
768+
769+
assert_raise(ArgumentError) {f.read(1, f.string)}
768770
end
769771

770772
def test_readpartial
@@ -830,6 +832,7 @@ def test_pread
830832

831833
assert_raise(EOFError) { f.pread(1, 5) }
832834
assert_raise(ArgumentError) { f.pread(-1, 0) }
835+
assert_raise(ArgumentError) { f.pread(0, 0, f.string) }
833836
assert_raise(Errno::EINVAL) { f.pread(3, -1) }
834837
assert_raise(Errno::EINVAL) { f.pread(0, -1) }
835838
assert_raise(IOError) { StringIO.new(nil, "w").pread(3, 0) }

0 commit comments

Comments
 (0)