[Assigned] REXML's encoding is broken if reading UTF-16 XML and Encondig.default_internal is set

Issue #10418 has been reported by Usaku NAKAMURA.


Bug #10418: REXML’s encoding is broken if reading UTF-16 XML and
Encondig.default_internal is set

  • Author: Usaku NAKAMURA
  • Status: Assigned
  • Priority: Normal
  • Assignee: Kouhei S.
  • Category: lib
  • Target version: current: 2.2.0
  • ruby -v: -
  • Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN

Encoding.default_internalがセットされている状態でREXMLにUTF-16なIOを食わせると、REXML::Document#encodingがUTF-16ではなくEncoding.default_internalになります。
以下パッチ。

Index: lib/rexml/source.rb
===================================================================
--- lib/rexml/source.rb  (revision 48095)
+++ lib/rexml/source.rb  (working copy)
@@ -285,7 +285,7 @@
       case @encoding
       when "UTF-16BE", "UTF-16LE"
         @source.binmode
-        @source.set_encoding(@encoding)
+        @source.set_encoding(@encoding, @encoding)
       end
       @line_break = encode(">")
       @pending_buffer, @buffer = @buffer, ""
Index: test/rexml/test_encoding.rb
===================================================================
--- test/rexml/test_encoding.rb  (revision 48095)
+++ test/rexml/test_encoding.rb  (working copy)
@@ -91,8 +91,18 @@
       utf16 = File.open(fixture_path("ticket_110_utf16.xml")) do |f|
         REXML::Document.new(f)
       end
-      assert_equal(utf16.encoding, "UTF-16")
+      assert_equal("UTF-16", utf16.encoding)
       assert( utf16[0].kind_of?(REXML::XMLDecl))
     end
+
+    def test_default_internal_with_utf16
+      orig_internal = ::Encoding.default_internal
+      ::Encoding.default_internal = 'utf-8'
+      begin
+        test_ticket_110
+      ensure
+        ::Encoding.default_internal = orig_internal
+      end
+    end
   end
 end