Skip to content

Commit 4452adb

Browse files
committed
Treat encodings in exception correctly
1 parent 109cbfc commit 4452adb

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

lib/irb.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,15 @@ def eval_input
590590
end
591591
end
592592

593-
def convert_invalid_byte_sequence(str)
594-
str = str.force_encoding(Encoding::ASCII_8BIT)
595-
conv = Encoding::Converter.new(Encoding::ASCII_8BIT, Encoding::UTF_8)
593+
def convert_invalid_byte_sequence(str, enc)
594+
str.force_encoding(enc)
595+
str.scrub { |c|
596+
c.bytes.map{ |b| "\\x#{b.to_s(16).upcase}" }.join
597+
}
598+
end
599+
600+
def encode_with_invalid_byte_sequence(str, enc)
601+
conv = Encoding::Converter.new(str.encoding, enc)
596602
dst = String.new
597603
begin
598604
ret = conv.primitive_convert(str, dst)
@@ -640,7 +646,8 @@ def handle_exception(exc)
640646
message = exc.full_message(order: :top)
641647
order = :top
642648
end
643-
message = convert_invalid_byte_sequence(message)
649+
message = convert_invalid_byte_sequence(message, exc.message.encoding)
650+
message = encode_with_invalid_byte_sequence(message, IRB.conf[:LC_MESSAGES].encoding) if message.encoding != IRB.conf[:LC_MESSAGES].encoding
644651
message = message.gsub(/((?:^\t.+$\n)+)/) { |m|
645652
case order
646653
when :top

test/irb/test_raise_no_backtrace_exception.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,30 @@ def test_raise_exception_with_invalid_byte_sequence
2121
raise StandardError, "A\\xf3B"
2222
IRB
2323
end
24+
25+
def test_raise_exception_with_different_encoding_containing_invalid_byte_sequence
26+
skip if RUBY_ENGINE == 'truffleruby'
27+
backup_home = ENV["HOME"]
28+
Dir.mktmpdir("test_irb_raise_no_backtrace_exception_#{$$}") do |tmpdir|
29+
ENV["HOME"] = tmpdir
30+
31+
bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : []
32+
File.open('euc.rb', 'w') do |f|
33+
f.write(<<~EOF)
34+
# encoding: euc-jp
35+
36+
def raise_euc_with_invalid_byte_sequence
37+
raise "\xA4\xA2\\xFF"
38+
end
39+
EOF
40+
end
41+
assert_in_out_err(bundle_exec + %w[-rirb -W0 -e ENV[%(LC_ALL)]=%(ja_JP.UTF-8) -e ENV[%(LANG)]=%(ja_JP.UTF-8) -e IRB.start(__FILE__) -- -f --], <<~IRB, /`raise_euc_with_invalid_byte_sequence': あ\\xFF \(RuntimeError\)/, [])
42+
require_relative 'euc'
43+
raise_euc_with_invalid_byte_sequence
44+
IRB
45+
end
46+
ensure
47+
ENV["HOME"] = backup_home
48+
end
2449
end
2550
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy