From 7a4d09a41c43fe97fbee64fd01225c5ea775f58e Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 2 May 2025 23:23:58 -0700 Subject: [PATCH 1/2] Never change the underlying string encoding StringIO should be a view of given source string and set_encoding shouldn't change source encoding. Similar change for binmode and set_encoding_by_bom. --- ext/stringio/stringio.c | 9 --------- test/stringio/test_stringio.rb | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 89c1ff7..ebb18bc 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -278,9 +278,6 @@ set_encoding_by_bom(struct StringIO *ptr) if (idx) { extenc = rb_enc_from_index(idx); ptr->pos = bomlen; - if (ptr->flags & FMODE_WRITABLE) { - rb_enc_associate_index(ptr->string, idx); - } } ptr->enc = extenc; return extenc; @@ -696,9 +693,6 @@ strio_binmode(VALUE self) rb_encoding *enc = rb_ascii8bit_encoding(); ptr->enc = enc; - if (WRITABLE(self)) { - rb_enc_associate(ptr->string, enc); - } return self; } @@ -1859,9 +1853,6 @@ strio_set_encoding(int argc, VALUE *argv, VALUE self) } } ptr->enc = enc; - if (!NIL_P(ptr->string) && WRITABLE(self)) { - rb_enc_associate(ptr->string, enc); - } return self; } diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 570b3d7..5f4efab 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -304,7 +304,7 @@ def test_set_encoding f = StringIO.new() f.set_encoding("ISO-8859-16:ISO-8859-1") assert_equal(Encoding::ISO_8859_16, f.external_encoding) - assert_equal(Encoding::ISO_8859_16, f.string.encoding) + assert_equal(Encoding::UTF_8, f.string.encoding) assert_nil(f.internal_encoding) end From 247bdd049bea9706822f64095644c7fefcad3fca Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 2 May 2025 23:30:59 -0700 Subject: [PATCH 2/2] Update Java version to not set encoding on underlying string --- ext/java/org/jruby/ext/stringio/StringIO.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/ext/java/org/jruby/ext/stringio/StringIO.java b/ext/java/org/jruby/ext/stringio/StringIO.java index 98d997a..bc58db5 100644 --- a/ext/java/org/jruby/ext/stringio/StringIO.java +++ b/ext/java/org/jruby/ext/stringio/StringIO.java @@ -421,7 +421,6 @@ public IRubyObject initialize_copy(ThreadContext context, IRubyObject other) { public IRubyObject binmode(ThreadContext context) { StringIOData ptr = this.getPtr(); ptr.enc = EncodingUtils.ascii8bitEncoding(context.runtime); - if (writable()) ptr.string.setEncoding(ptr.enc); return this; } @@ -1809,10 +1808,6 @@ public IRubyObject set_encoding(ThreadContext context, IRubyObject ext_enc) { // in read-only mode, StringIO#set_encoding no longer sets the encoding RubyString string = ptr.string; - if (string != null && writable() && string.getEncoding() != enc) { - string.modify(); - string.setEncoding(enc); - } } finally { if (locked) unlock(ptr); } @@ -1847,9 +1842,6 @@ public IRubyObject set_encoding_by_bom(ThreadContext context) { private Encoding setEncodingByBOM(ThreadContext context, StringIOData ptr) { Encoding enc = detectBOM(context, ptr.string, (ctx, enc2, bomlen) -> { ptr.pos = bomlen; - if (writable()) { - ptr.string.setEncoding(enc2); - } return enc2; }); ptr.enc = enc;