diff options
author | Charles Oliver Nutter <headius@headius.com> | 2017-09-07 16:25:54 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-07 16:25:54 -0500 |
commit | b572261900893df0352d3ad71c17dfa81ab17ccc (patch) | |
tree | 3bcf8b863240a2dfb2023242fe2a35d71ba4add2 /ext | |
parent | 4039a811248de7b0b9ae9e4a97854f00ba985255 (diff) | |
parent | 2bbb245edaf80179f4673bf4bf75978a10654c64 (diff) | |
download | psych-b572261900893df0352d3ad71c17dfa81ab17ccc.zip |
Merge pull request #328 from enebo/desensitize_io_encoding_on_load
Parse.load with an IO/File which has a non-YAML allowed encoding will…
Diffstat (limited to 'ext')
-rw-r--r-- | ext/java/PsychParser.java | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/ext/java/PsychParser.java b/ext/java/PsychParser.java index b3e747e..f5b6faf 100644 --- a/ext/java/PsychParser.java +++ b/ext/java/PsychParser.java @@ -33,6 +33,8 @@ import java.nio.charset.Charset; import java.util.Map; import org.jcodings.Encoding; +import org.jcodings.specific.UTF16BEEncoding; +import org.jcodings.specific.UTF16LEEncoding; import org.jcodings.specific.UTF8Encoding; import org.jcodings.unicode.UnicodeEncoding; import org.jruby.Ruby; @@ -162,6 +164,11 @@ public class PsychParser extends RubyObject { if (yaml instanceof RubyIO) { Encoding enc = ((RubyIO) yaml).getReadEncoding(); charset = enc.getCharset(); + + // libyaml treats non-utf encodings as utf-8 and hopes for the best. + if (!(enc instanceof UTF8Encoding) && !(enc instanceof UTF16LEEncoding) && !(enc instanceof UTF16BEEncoding)) { + charset = UTF8Encoding.INSTANCE.getCharset(); + } } if (charset == null) { // If we can't get it from the IO or it doesn't have a charset, fall back on UTF-8 |