Skip to content

Handle non-UTF-8 encodings and invalid byte sequences in colorize_code - #1283

Open
lyn1dev wants to merge 3 commits into
ruby:masterfrom
lyn1dev:fix-colorize-non-utf8-encoding
Open

lyn1dev wants to merge 3 commits into
ruby:masterfrom
lyn1dev:fix-colorize-non-utf8-encoding

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 25, 2026 •

Copy link
Copy Markdown

Fixes #976

Summary

IRB::Color.colorize_code and IRB::WorkSpace#code_around_binding could raise ArgumentError (invalid byte sequence in UTF-8) or Encoding::CompatibilityError when colorizing files or strings containing non-UTF-8 encodings, magic encoding comments (e.g. # coding: sjis), or invalid byte sequences (such as non-UTF-8 data after __END__):

  1. Source encoding resolution: Prism.parse_lex(code, scopes: ...) previously omitted encoding: code.encoding (defaulting to UTF-8 for non-UTF-8 strings without magic comments) and did not update code or location slices to the parsed source encoding when a magic comment changed it (including under Prism::Serialize / FFI backends such as TruffleRuby). As a result, tokens parsed as Windows-31J were concatenated with code.lines still tagged as UTF-8 (e.g. lines following __END__), or raised Encoding::CompatibilityError / ArgumentError during token matching and slicing.
  2. Invalid byte sequences: Reline::Unicode.escape_for_print executes regexes against its argument, raising ArgumentError: invalid byte sequence when unparsed data (such as arbitrary bytes after __END__ or syntax error fragments) is not valid in the active encoding. Similarly, error_tokens.reject! and ColorizeVisitor#visit_call_node could run regexes against slices with invalid encodings.

Changes

  • Pass encoding: orig_encoding to Prism.parse_lex, resolve the parsed token/source encoding across both C-extension and Prism::Serialize backends, align code, location slices, and the output buffer with that encoding, and re-encode the colorized result back to the caller's orig_encoding with replacement fallback.
  • Scrub invalid byte sequences in IRB::Color.escape_for_print before passing text to Reline::Unicode.escape_for_print, and guard regex checks on potentially invalid token/message slices.
  • Ensure colorized, fmt, and file encodings are compatible in IRB::WorkSpace#code_around_binding before formatting lines (e.g. when SCRIPT_LINES__ or source files use non-UTF-8 encodings alongside a UTF-8 file path).

Testing

  • Added unit tests in test/irb/test_color.rb and test/irb/test_workspace.rb covering all reproduction cases from IRB fails to colorize code other than utf-8 #976, non-UTF-8 inputs without magic comments, ASCII-8BIT / invalid UTF-8 fragments, non-ASCII-compatible encodings, and SCRIPT_LINES__.
  • The new tests fail without the fix and pass with it:
    • bundle exec ruby -Ilib -Itest test/irb/test_color.rb (8 tests, 906 assertions, 0 failures, 0 errors)
    • bundle exec ruby -Ilib -Itest test/irb/test_workspace.rb (6 tests, 12 assertions, 0 failures, 0 errors)
    • bundle exec rubocop lib/irb/color.rb lib/irb/workspace.rb test/irb/test_color.rb test/irb/test_workspace.rb (0 offenses)

AI was used to write this code.

This branch has not been deployed

No deployments
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.

IRB fails to colorize code other than utf-8

1 participant