Psych.dump raises Encoding::CompatibilityError for UTF-16/UTF-32 strings
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 55/100
Direzione di ricerca
Esamina Psych::Visitors::YAMLTree#visit_String intorno alle righe 303, 313 e 316 in lib/psych/visitors/yaml_tree.rb. Inizia con la copertura della codifica UTF-16 in test/psych/test_encoding.rb, inclusi test_transcode_utf16le e test_transcode_utf16be, quindi verifica che le stringhe UTF-16/32 vengano sottoposte a dump senza un errore di codifica e facciano il round-trip come le stringhe UTF-8 equivalenti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
Psych.dump raises Encoding::CompatibilityError for any string in a non-ASCII-compatible encoding (UTF-16LE/BE, UTF-32LE/BE). An empty string is enough to trigger it.
This is asymmetric with the load side: Psych.load explicitly supports UTF-16 input and has tests for it (test_transcode_utf16le / test_transcode_utf16be in test/psych/test_encoding.rb), but dumping a UTF-16 string crashes.
require "psych"
Psych.load("--- こんにちは!".encode("UTF-16LE")) # => "こんにちは!" (works)
Psych.dump("".encode("UTF-16LE")) # => raises
Encoding::CompatibilityError: incompatible encoding regexp match (US-ASCII regexp with UTF-16LE string)
lib/psych/visitors/yaml_tree.rb:303:in 'String#match?'
lib/psych/visitors/yaml_tree.rb:303:in 'Psych::Visitors::YAMLTree#visit_String'
Affects every dump entry point: Psych.dump, YAML.dump, and to_yaml on a String, or on any Hash/Array/nested structure containing one — including when the string is used as a hash key.
Cause
visit_String matches the string against US-ASCII regexp literals:
yaml_tree.rb:303—o.match?(/\n(?!\Z)/)yaml_tree.rb:313—o.match?(/^[^[:word:]][^"]*$/)yaml_tree.rb:316—/\A0[0-7]*[89]/.match?(o)
A string whose encoding is not ASCII-compatible cannot be matched against an ASCII regexp at all, so the first of these raises regardless of the string's content.
The guard above them only covers ASCII_8BIT:
def binary? string
string.encoding == Encoding::ASCII_8BIT && !string.ascii_only?
end
so UTF-16/32 falls straight through.
Which encodings are affected
The crashing set maps exactly onto Encoding#ascii_compatible?:
| encoding | ascii_compatible? |
Psych.dump |
|---|---|---|
| UTF-8, ISO-8859-1, EUC-JP, Windows-1252, Shift_JIS, ASCII-8BIT | true |
works |
| UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE | false |
raises |
Every other non-UTF-8 encoding transcodes and round-trips correctly, which suggests the UTF-16/32 family is simply an unhandled case rather than a deliberate restriction.
Expected
A UTF-16/32 string should dump like the equivalent UTF-8 string does — it should not be tagged !binary, and it should round-trip.
Environment
Reproduced on psych 5.4.0 (Ruby 4.0.4) and on master 79be592f1dbe3c6eb36d4e1015b8379207158a75 (5.5.0), same file and line on both.
I have a patch and will open a PR referencing this issue.
- Lingua principale
- Ruby
- Stelle
- 597
- Fork
- 223
- Merge medio
- 11h 23m
- PR unite (30g)
- 3
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di ruby/psych
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Documentation
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 68/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 52/100
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
TheOdinProject/curriculum#31417 · 2 commenti ·
-
Allow faraday-http-cache 3.x Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
glossarist/glossarist-ruby#238 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
palladius/rails8-app-on-gcp#145 ·