Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ext/java/org/jruby/ext/stringio/StringIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,16 @@ private void strioInit(ThreadContext context, int argc, IRubyObject arg0, IRubyO

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

if (!string.isNil()) {
string = string.convertToString();
} else if (argc == 0) {
string = RubyString.newEmptyString(runtime, runtime.getDefaultExternalEncoding());
}
// The conversions above may freeze this stream.
checkFrozen();
ptr.flags = fmode[0];

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

if (this == otherIO) return this;

checkFrozen();

ptr = otherIO.getPtrForRead();
flags = (byte) (flags & ~STRIO_READWRITE | otherIO.flags & STRIO_READWRITE);

Expand Down
7 changes: 6 additions & 1 deletion ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,16 +401,20 @@ strio_init(int argc, VALUE *argv, struct StringIO *ptr, VALUE self)
{
VALUE string, vmode, opt;
int oflags;
rb_io_mode_t fmode;
rb_io_enc_t convconfig;

argc = rb_scan_args(argc, argv, "02:", &string, &vmode, &opt);
rb_io_extract_modeenc(&vmode, 0, opt, &oflags, &ptr->flags, &convconfig);
rb_io_extract_modeenc(&vmode, 0, opt, &oflags, &fmode, &convconfig);
if (!NIL_P(string)) {
StringValue(string);
}
else if (!argc) {
string = rb_enc_str_new("", 0, rb_default_external_encoding());
}
/* The conversions above may freeze this stream. */
rb_check_frozen(self);
ptr->flags = fmode;

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

orig = rb_convert_type(orig, T_DATA, "StringIO", "to_strio");
if (copy == orig) return copy;
rb_check_frozen(copy);
ptr = StringIOForRead(orig);
old_ptr = check_strio(copy);
if (old_ptr) {
Expand Down
31 changes: 31 additions & 0 deletions test/stringio/test_stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,37 @@ def test_new_during_mode_conversion
assert_predicate(StringIO.new("".dup, mode), :closed_write?)
end

def test_reopen_freeze_during_argument_conversion
f = StringIO.new("hello")
c = Object.new
c.define_singleton_method(:to_str) {f.freeze; "new"}
assert_raise(FrozenError) {f.reopen(c, "r")}
assert_equal(["hello", false], [f.string, f.closed_write?])
end

def test_initialize_frozen
f = StringIO.new("hello")
f.freeze
assert_raise(FrozenError) {f.send(:initialize, "new")}
assert_equal("hello", f.string)
end

def test_reopen_freeze_during_to_strio
f = StringIO.new("hello")
g = StringIO.new("world")
c = Object.new
c.define_singleton_method(:to_strio) {f.freeze; g}
assert_raise(FrozenError) {f.reopen(c)}
assert_equal("hello", f.string)
end

def test_initialize_copy_frozen
f = StringIO.new("hello")
f.freeze
assert_raise(FrozenError) {f.send(:initialize_copy, StringIO.new("world"))}
assert_equal("hello", f.string)
end

private

def assert_string(content, encoding, str, mesg = nil)
Expand Down
Loading