You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ParseException could not get message when xml with invalid characters #29
I get the following backtrace message when i load xml:
incompatible character encodings: UTF-8 and ASCII-8BIT
/usr/local/rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/rexml-3.2.2/lib/rexml/parseexception.rb:32:in `to_s'
the xml encoding is UTF-8 and with invalid characters, but parseexception to_s use ASCII-8BIT encoding, so here to_s will raise an exception with encoding fail, user will not get the actual error information in xml
require 'rexml/document'
include REXML
require 'json'
# $confing_path is the full path of the xml file which I want to load
xml_file = Document.new(File::open($confing_path))
the xml file is utf-8 encoding, I know the xml contains invalid characters, after I load the xml file, ruby raise <REXML::ParseException: #<ArgumentError: invalid byte sequence in UTF-8> excepiton and I cann't get the exact error info by exception message, if I temporary change the ParseException to_s method line 32 to utf-8 like this:
err << @source.buffer[0..80].force_encoding("UTF-8"), now I get the exact error information in xml file:
... Exception parsing
Line: 5
Position: 270
Last 80 unconsumed characters:
^M
The input is invalid XML and rightly triggers a ParseException, but then reading the exception's attributes raises another error: incompatible character encodings: UTF-8 and ASCII-8BIT (Encoding::CompatibilityError).
It looks like this is a bug in the ParseException code. Line 21 initializes an empty string, which defaults to UTF8 encoding. Then line 32 forces a string's encoding to ASCII-8BIT and tries to append it to the UTF8 string, which triggers the encoding mismatch:
Activity
kou commentedon Apr 25, 2020
Could you show a Ruby script and XML that reproduce this problem?
kewudu commentedon Apr 30, 2020
My XML file contains invalid encoding, part of XML file is:
my rub script is simple:
the xml file is utf-8 encoding, I know the xml contains invalid characters, after I load the xml file, ruby raise <REXML::ParseException: #<ArgumentError: invalid byte sequence in UTF-8> excepiton and I cann't get the exact error info by exception message, if I temporary change the ParseException to_s method line 32 to utf-8 like this:
err << @source.buffer[0..80].force_encoding("UTF-8"), now I get the exact error information in xml file:
...
Exception parsing
Line: 5
Position: 270
Last 80 unconsumed characters:
^M
iangreenleaf commentedon Feb 15, 2022
Here's a very simple reproduction of this bug (the base64 stuff is just there to make sure the special characters in the string come through):
The input is invalid XML and rightly triggers a
ParseException, but then reading the exception's attributes raises another error: incompatible character encodings: UTF-8 and ASCII-8BIT (Encoding::CompatibilityError).It looks like this is a bug in the
ParseExceptioncode. Line 21 initializes an empty string, which defaults to UTF8 encoding. Then line 32 forces a string's encoding to ASCII-8BIT and tries to append it to the UTF8 string, which triggers the encoding mismatch:Fixed a problem in which ParseException error messages could not be r…
Fix a problem that parse exception message can't be generated for inv…