Skip to content

JRuby: keep the whole character after an invalid escape - #1081

Merged
byroot merged 1 commit into
ruby:masterfrom
youdie006:jruby-invalid-escape-non-ascii
Sep 29, 2026
Merged

byroot merged 1 commit into
ruby:masterfrom
youdie006:jruby-invalid-escape-non-ascii

Conversation

@youdie006

Copy link
Copy Markdown
Contributor

With allow_invalid_escape: true, the JRuby decoder keeps the character after a stray backslash with append(character), which writes only the low byte of the code point (StringDecoder.java:158). The C extension keeps the character's bytes.

JSON.parse('["caf\\é"]', allow_invalid_escape: true)  # JRuby: ["caf\xE9"] (invalid UTF-8), CRuby: ["café"]
JSON.parse('["\\あ"]', allow_invalid_escape: true)    # JRuby: ["B"],                     CRuby: ["あ"]

Switched to writeUtf8Char, and added a case next to test_parse_invalid_escape, which only covers ASCII. On jruby:9.4-jdk21, bundle exec rake test JSON_COMPACT=1 fails the new test on master and passes with the change (591 tests); CRuby already passes it.

Written with AI assistance (Claude); I ran the tests above locally and reviewed the change.

With allow_invalid_escape, the JRuby decoder appended the code point read
after a stray backslash with append(int), which writes only its low byte.
"\\é" became "\xE9" and "\\あ" became "B", where the C extension
keeps the character. Use writeUtf8Char, as the other code point paths do.
@byroot
byroot merged commit edb3b20 into ruby:master Sep 29, 2026
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants