From 1046eae7814930fb326371930715122d647457a2 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 23 Nov 2017 07:10:56 +0000 Subject: io.c: read BOM only for reading * io.c (io_strip_bom): just abandon detecting UTF encoding by BOM unless opened for reading. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60884 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- io.c | 3 +++ test/ruby/test_io_m17n.rb | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/io.c b/io.c index 6a9a273b41..ac2f246b51 100644 --- a/io.c +++ b/io.c @@ -5970,7 +5970,10 @@ static int io_strip_bom(VALUE io) { VALUE b1, b2, b3, b4; + rb_io_t *fptr; + GetOpenFile(io, fptr); + if (!(fptr->mode & FMODE_READABLE)) return 0; if (NIL_P(b1 = rb_io_getbyte(io))) return 0; switch (b1) { case INT2FIX(0xEF): diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb index ccb124a46b..8ba34845fa 100644 --- a/test/ruby/test_io_m17n.rb +++ b/test/ruby/test_io_m17n.rb @@ -2168,6 +2168,20 @@ EOT assert_nil(enc) end + def test_bom_non_reading + with_tmpdir { + enc = nil + assert_nothing_raised(IOError) { + open("test", "w:bom|utf-8") {|f| + enc = f.external_encoding + f.print("abc") + } + } + assert_equal(Encoding::UTF_8, enc) + assert_equal("abc", File.binread("test")) + } + end + def test_cbuf with_tmpdir { fn = "tst" -- cgit v1.2.3