Skip to content

Commit 4df056f

Browse files
committed
fix: clear code-range when re-using a string buffer (jruby#9035)
1 parent 024123c commit 4df056f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

core/src/main/java/org/jruby/util/io/EncodingUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ public static RubyString setStrBuf(Ruby runtime, final IRubyObject obj, final in
13331333
str = obj.convertToString();
13341334
int clen = str.size();
13351335
if (clen >= len) {
1336-
str.modify();
1336+
str.modifyAndClearCodeRange();
13371337
return str;
13381338
}
13391339
str.modifyExpand(len);

test/jruby/test_io.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,27 @@ def test_file_read_with_nil_buffer
173173
assert_equal " ", f.read(1, nil)
174174
end
175175

176+
def test_file_read_into_buffer_code_range
177+
buf = String.new
178+
buf.concat "¿Cómo estás? Ça va bien?"
179+
assert_false buf.ascii_only? # base-line: scan and set the code-range for the buffer
180+
181+
require 'tempfile'
182+
tmpfile = Tempfile.new('file')
183+
tmpfile.open
184+
tmpfile.write("ascii only")
185+
tmpfile.flush
186+
tmpfile.close
187+
188+
File.open(tmpfile.path, encoding: 'binary') do |file|
189+
file.read(10, buf) # read less then buf.size
190+
end
191+
tmpfile.delete
192+
193+
assert_true buf.ascii_only? # reading into the buffer should have cleared the code-range
194+
assert_equal 'ascii only', buf
195+
end
196+
176197
def test_open
177198
ensure_files @file
178199

0 commit comments

Comments
 (0)