Fix crash in IO.external_encoding/IO.internal_encoding

[Bug #22410]

If the IO is not initialized, IO.external_encoding/IO.internal_encoding
can crash because it doesn't check for it.
This commit is contained in:
Peter Zhu committed 2026-10-07 23:31:40 +09:00
1 parent 9ce0df6719
commit 67f4ea55a3
2 files changed
+4 -2

No files matched your search

+2 -2
View File
@@ -13770,7 +13770,7 @@ rb_io_s_copy_stream(int argc, VALUE *argv, VALUE io)
static VALUE
rb_io_external_encoding(VALUE io)
{
rb_io_t *fptr = RFILE(rb_io_taint_check(io))->fptr;
rb_io_t *fptr = rb_io_get_fptr(rb_io_taint_check(io));
if (fptr->encs.enc2) {
return rb_enc_from_encoding(fptr->encs.enc2);
@@ -13798,7 +13798,7 @@ rb_io_external_encoding(VALUE io)
static VALUE
rb_io_internal_encoding(VALUE io)
{
rb_io_t *fptr = RFILE(rb_io_taint_check(io))->fptr;
rb_io_t *fptr = rb_io_get_fptr(rb_io_taint_check(io));
if (!fptr->encs.enc2) return Qnil;
return rb_enc_from_encoding(io_read_encoding(fptr));
+2
View File
@@ -3306,6 +3306,8 @@ class TestIO < Test::Unit::TestCase
def test_uninitialized
assert_raise(IOError) { IO.allocate.print "" }
assert_raise(IOError) { IO.allocate.external_encoding }
assert_raise(IOError) { IO.allocate.internal_encoding }
end
def test_nofollow