Skip to content

Commit ee642e8

Browse files
kouclaude
andcommitted
Check frozen state after argument conversions in StringIO#reopen
`reopen` checked whether the receiver is frozen only before converting the arguments. If a conversion (`to_str` of the string or the mode, or `to_strio`) froze the receiver, `reopen` modified the frozen receiver. Check it again after the conversions in `strio_init()` and `strio_copy()`. `strio_init()` also keeps the parsed mode in a local variable until the check, so that the flags of the frozen receiver aren't changed. `strio_init()` and `strio_copy()` are also used by `initialize` and `initialize_copy`. So calling them directly on a frozen `StringIO` now raises `FrozenError` instead of modifying it. `dup` and `clone` aren't affected because the copy isn't frozen yet when `initialize_copy` is called. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 66110f7 commit ee642e8

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ private void strioInit(ThreadContext context, int argc, IRubyObject arg0, IRubyO
372372

373373
// switch to per-use oflags if it is ever used in the future
374374
EncodingUtils.extractModeEncoding(context, ioEncodable, vmodeAndVpermP, maybeOptions, OFLAGS_UNUSED, fmode);
375-
ptr.flags = fmode[0];
376375
vmode = EncodingUtils.vmode(vmodeAndVpermP);
377376

378377
// clear shared vmodeVperm
@@ -383,6 +382,9 @@ private void strioInit(ThreadContext context, int argc, IRubyObject arg0, IRubyO
383382
} else if (argc == 0) {
384383
string = RubyString.newEmptyString(runtime, runtime.getDefaultExternalEncoding());
385384
}
385+
// The conversions above may freeze this stream.
386+
checkFrozen();
387+
ptr.flags = fmode[0];
386388

387389
if (!string.isNil() && string.isFrozen()) {
388390
if ((ptr.flags & OpenFile.WRITABLE) != 0) {
@@ -422,6 +424,8 @@ public IRubyObject initialize_copy(ThreadContext context, IRubyObject other) {
422424

423425
if (this == otherIO) return this;
424426

427+
checkFrozen();
428+
425429
ptr = otherIO.getPtrForRead();
426430
flags = (byte) (flags & ~STRIO_READWRITE | otherIO.flags & STRIO_READWRITE);
427431

‎ext/stringio/stringio.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,17 +400,20 @@ static VALUE
400400
strio_init(int argc, VALUE *argv, struct StringIO *ptr, VALUE self)
401401
{
402402
VALUE string, vmode, opt;
403-
int oflags;
403+
int oflags, fmode;
404404
rb_io_enc_t convconfig;
405405

406406
argc = rb_scan_args(argc, argv, "02:", &string, &vmode, &opt);
407-
rb_io_extract_modeenc(&vmode, 0, opt, &oflags, &ptr->flags, &convconfig);
407+
rb_io_extract_modeenc(&vmode, 0, opt, &oflags, &fmode, &convconfig);
408408
if (!NIL_P(string)) {
409409
StringValue(string);
410410
}
411411
else if (!argc) {
412412
string = rb_enc_str_new("", 0, rb_default_external_encoding());
413413
}
414+
/* The conversions above may freeze this stream. */
415+
rb_check_frozen(self);
416+
ptr->flags = fmode;
414417

415418
if (!NIL_P(string) && readonly_string_p(string)) {
416419
if (ptr->flags & FMODE_WRITABLE) {
@@ -793,6 +796,7 @@ strio_copy(VALUE copy, VALUE orig)
793796

794797
orig = rb_convert_type(orig, T_DATA, "StringIO", "to_strio");
795798
if (copy == orig) return copy;
799+
rb_check_frozen(copy);
796800
ptr = StringIOForRead(orig);
797801
old_ptr = check_strio(copy);
798802
if (old_ptr) {

‎test/stringio/test_stringio.rb‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,37 @@ def test_set_string_via_dup_with_gc
12171217
end;
12181218
end
12191219

1220+
def test_reopen_freeze_during_argument_conversion
1221+
f = StringIO.new("hello")
1222+
c = Object.new
1223+
c.define_singleton_method(:to_str) {f.freeze; "new"}
1224+
assert_raise(FrozenError) {f.reopen(c, "r")}
1225+
assert_equal(["hello", false], [f.string, f.closed_write?])
1226+
end
1227+
1228+
def test_initialize_frozen
1229+
f = StringIO.new("hello")
1230+
f.freeze
1231+
assert_raise(FrozenError) {f.send(:initialize, "new")}
1232+
assert_equal("hello", f.string)
1233+
end
1234+
1235+
def test_reopen_freeze_during_to_strio
1236+
f = StringIO.new("hello")
1237+
g = StringIO.new("world")
1238+
c = Object.new
1239+
c.define_singleton_method(:to_strio) {f.freeze; g}
1240+
assert_raise(FrozenError) {f.reopen(c)}
1241+
assert_equal("hello", f.string)
1242+
end
1243+
1244+
def test_initialize_copy_frozen
1245+
f = StringIO.new("hello")
1246+
f.freeze
1247+
assert_raise(FrozenError) {f.send(:initialize_copy, StringIO.new("world"))}
1248+
assert_equal("hello", f.string)
1249+
end
1250+
12201251
private
12211252

12221253
def assert_string(content, encoding, str, mesg = nil)

0 commit comments

Comments
 (0)