Skip to content

Commit b9cc8e5

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 c69332d commit b9cc8e5

3 files changed

Lines changed: 42 additions & 2 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
@@ -371,14 +371,16 @@ private void strioInit(ThreadContext context, int argc, IRubyObject arg0, IRubyO
371371

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

377376
if (!string.isNil()) {
378377
string = string.convertToString();
379378
} else if (argc == 0) {
380379
string = RubyString.newEmptyString(runtime, runtime.getDefaultExternalEncoding());
381380
}
381+
// The conversions above may freeze this stream.
382+
checkFrozen();
383+
ptr.flags = fmode[0];
382384

383385
if (!string.isNil() && string.isFrozen()) {
384386
if ((ptr.flags & OpenFile.WRITABLE) != 0) {
@@ -418,6 +420,8 @@ public IRubyObject initialize_copy(ThreadContext context, IRubyObject other) {
418420

419421
if (this == otherIO) return this;
420422

423+
checkFrozen();
424+
421425
ptr = otherIO.getPtrForRead();
422426
flags = (byte) (flags & ~STRIO_READWRITE | otherIO.flags & STRIO_READWRITE);
423427

‎ext/stringio/stringio.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,16 +401,20 @@ strio_init(int argc, VALUE *argv, struct StringIO *ptr, VALUE self)
401401
{
402402
VALUE string, vmode, opt;
403403
int oflags;
404+
rb_io_mode_t fmode;
404405
rb_io_enc_t convconfig;
405406

406407
argc = rb_scan_args(argc, argv, "02:", &string, &vmode, &opt);
407-
rb_io_extract_modeenc(&vmode, 0, opt, &oflags, &ptr->flags, &convconfig);
408+
rb_io_extract_modeenc(&vmode, 0, opt, &oflags, &fmode, &convconfig);
408409
if (!NIL_P(string)) {
409410
StringValue(string);
410411
}
411412
else if (!argc) {
412413
string = rb_enc_str_new("", 0, rb_default_external_encoding());
413414
}
415+
/* The conversions above may freeze this stream. */
416+
rb_check_frozen(self);
417+
ptr->flags = fmode;
414418

415419
if (!NIL_P(string) && readonly_string_p(string)) {
416420
if (ptr->flags & FMODE_WRITABLE) {
@@ -793,6 +797,7 @@ strio_copy(VALUE copy, VALUE orig)
793797

794798
orig = rb_convert_type(orig, T_DATA, "StringIO", "to_strio");
795799
if (copy == orig) return copy;
800+
rb_check_frozen(copy);
796801
ptr = StringIOForRead(orig);
797802
old_ptr = check_strio(copy);
798803
if (old_ptr) {

‎test/stringio/test_stringio.rb‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,37 @@ def test_new_during_mode_conversion
12551255
assert_predicate(StringIO.new("".dup, mode), :closed_write?)
12561256
end
12571257

1258+
def test_reopen_freeze_during_argument_conversion
1259+
f = StringIO.new("hello")
1260+
c = Object.new
1261+
c.define_singleton_method(:to_str) {f.freeze; "new"}
1262+
assert_raise(FrozenError) {f.reopen(c, "r")}
1263+
assert_equal(["hello", false], [f.string, f.closed_write?])
1264+
end
1265+
1266+
def test_initialize_frozen
1267+
f = StringIO.new("hello")
1268+
f.freeze
1269+
assert_raise(FrozenError) {f.send(:initialize, "new")}
1270+
assert_equal("hello", f.string)
1271+
end
1272+
1273+
def test_reopen_freeze_during_to_strio
1274+
f = StringIO.new("hello")
1275+
g = StringIO.new("world")
1276+
c = Object.new
1277+
c.define_singleton_method(:to_strio) {f.freeze; g}
1278+
assert_raise(FrozenError) {f.reopen(c)}
1279+
assert_equal("hello", f.string)
1280+
end
1281+
1282+
def test_initialize_copy_frozen
1283+
f = StringIO.new("hello")
1284+
f.freeze
1285+
assert_raise(FrozenError) {f.send(:initialize_copy, StringIO.new("world"))}
1286+
assert_equal("hello", f.string)
1287+
end
1288+
12581289
private
12591290

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

0 commit comments

Comments
 (0)