Skip to content

Commit e485ef3

Browse files
committedJun 12, 2018
[fix] potential string sharing as each_line returns shared buffer
... later modification to StringIO#string might affect the line resolves GH-5203
1 parent 6b7d821 commit e485ef3

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed
 

Diff for: ‎core/src/main/java/org/jruby/ext/stringio/StringIO.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ private RubyString strioSubstr(Ruby runtime, int pos, int len) {
508508
if (len < 0) len = 0;
509509

510510
if (len == 0) return RubyString.newEmptyString(runtime);
511+
string.setByteListShared(); // we only share the byte[] buffer but its easier this way
511512
return RubyString.newStringShared(runtime, stringBytes, stringByteList.getBegin() + pos, len, enc);
512513
}
513514
}
@@ -657,7 +658,7 @@ private IRubyObject getline(ThreadContext context, final IRubyObject rs, int lim
657658
if (limit > 0 && s + limit < e) {
658659
e = ptr.enc.rightAdjustCharHead(stringBytes, s, s + limit, e);
659660
}
660-
if (rs.isNil()) {
661+
if (rs == context.nil) {
661662
if (chomp) {
662663
w = chompNewlineWidth(stringBytes, s, e);
663664
}

Diff for: ‎test/jruby/test_string.rb

+11
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ def test_count
9696
assert_equal(2, "abc\u{3042 3044 3046}".count("^\u3042", "^\u3044", "^\u3046", "^c"))
9797
end
9898

99+
# GH-5203
100+
def test_string_buffer_sharing_in_stringio; require 'stringio'
101+
zero = 0
102+
strio = StringIO.new "123\nabcdefghijklmnopqrstuvxyz\n#{zero}\n"
103+
ary = []; strio.each_line { |line| ary << line }
104+
strio.rewind
105+
strio.write('456')
106+
assert_equal ["123\n", "abcdefghijklmnopqrstuvxyz\n", "0\n"], ary
107+
assert_equal "456\nabcdefghijklmnopqrstuvxyz\n0\n", strio.string
108+
end
109+
99110
private
100111

101112
def do_sub buf, e1, e2, e3

0 commit comments

Comments
 (0)