ruby-changes:64750
From: aycabta <ko1@a...>
Date: Tue, 5 Jan 2021 18:13:49 +0900 (JST)
Subject: [ruby-changes:64750] e72a6ed45f (master): [ruby/irb] Escape invalid byte sequence in Exception
https://git.ruby-lang.org/ruby.git/commit/?id=e72a6ed45f From e72a6ed45f6ba844f15523b9f1250c22601c0011 Mon Sep 17 00:00:00 2001 From: aycabta <aycabta@g...> Date: Mon, 28 Dec 2020 10:45:17 +0900 Subject: [ruby/irb] Escape invalid byte sequence in Exception This fixes ruby/irb#141. https://github.com/ruby/irb/commit/0815317d42 diff --git a/lib/irb.rb b/lib/irb.rb index 4eef8be..1fb1781 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -578,6 +578,29 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb.rb#L578 end end + def convert_invalid_byte_sequence(str) + str = str.force_encoding(Encoding::ASCII_8BIT) + conv = Encoding::Converter.new(Encoding::ASCII_8BIT, Encoding::UTF_8) + dst = String.new + begin + ret = conv.primitive_convert(str, dst) + case ret + when :invalid_byte_sequence + conf.insert_output(conf.primitive_errinfo[3].dump[1..-2]) + redo + when :undefined_conversion + c = conv.primitive_errinfo[3].dup.force_encoding(conv.primitive_errinfo[1]) + conv.insert_output(c.dump[1..-2]) + redo + when :incomplete_input + conv.insert_output(conv.primitive_errinfo[3].dump[1..-2]) + when :finished + end + break + end while nil + dst + end + def handle_exception(exc) if exc.backtrace && exc.backtrace[0] =~ /\/irb(2)?(\/.*|-.*|\.rb)?:/ && exc.class.to_s !~ /^IRB/ && !(SyntaxError === exc) && !(EncodingError === exc) @@ -621,7 +644,8 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb.rb#L644 end puts messages.reverse end - m = exc.to_s.split(/\n/) + converted_exc_s = convert_invalid_byte_sequence(exc.to_s.dup) + m = converted_exc_s.split(/\n/) print "#{attr[1]}#{exc.class} (#{attr[4]}#{m.shift}#{attr[0, 1]})#{attr[]}\n" puts m.map {|s| "#{attr[1]}#{s}#{attr[]}\n"} if attr == ATTR_PLAIN diff --git a/test/irb/test_raise_no_backtrace_exception.rb b/test/irb/test_raise_no_backtrace_exception.rb index 699990f..9babc29 100644 --- a/test/irb/test_raise_no_backtrace_exception.rb +++ b/test/irb/test_raise_no_backtrace_exception.rb @@ -13,5 +13,13 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_raise_no_backtrace_exception.rb#L13 raise e IRB end + + def test_raise_exception_with_invalid_byte_sequence + skip if RUBY_ENGINE == 'truffleruby' + bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : [] + assert_in_out_err(bundle_exec + %w[-rirb -W0 -e IRB.start(__FILE__) -- -f --], <<~IRB, /StandardError \(A\\xF3B\)/, []) + raise StandardError, "A\\xf3B" + IRB + end end end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/