[前][次][番号順一覧][スレッド一覧]

ruby-changes:60509

From: aycabta <ko1@a...>
Date: Thu, 26 Mar 2020 17:44:34 +0900 (JST)
Subject: [ruby-changes:60509] 22477128cd (master): [ruby/irb] Suppress crashing when EncodingError has occurred without lineno

https://git.ruby-lang.org/ruby.git/commit/?id=22477128cd

From 22477128cd77e0d0dce7e78bc12f9cc8cccc1cb4 Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Thu, 26 Mar 2020 16:43:16 +0900
Subject: [ruby/irb] Suppress crashing when EncodingError has occurred without
 lineno

https://github.com/ruby/irb/commit/13572d8cdc

diff --git a/lib/irb.rb b/lib/irb.rb
index e73174e..ee6979c 100644
--- a/lib/irb.rb
+++ b/lib/irb.rb
@@ -554,7 +554,8 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb.rb#L554
 
     def handle_exception(exc)
       if exc.backtrace && exc.backtrace[0] =~ /\/irb(2)?(\/.*|-.*|\.rb)?:/ && exc.class.to_s !~ /^IRB/ &&
-         !(SyntaxError === exc)
+         !(SyntaxError === exc) && !(EncodingError === exc)
+        # The backtrace of invalid encoding hash (ex. {"\xAE": 1}) raises EncodingError without lineno.
         irb_bug = true
       else
         irb_bug = false
diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb
index 251db9e..d5630c8 100644
--- a/lib/irb/ruby-lex.rb
+++ b/lib/irb/ruby-lex.rb
@@ -211,6 +211,8 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L211
       else
         RubyVM::InstructionSequence.compile(code)
       end
+    rescue EncodingError
+      # This is for a hash with invalid encoding symbol, {"\xAE": 1}
     rescue SyntaxError => e
       case e.message
       when /unterminated (?:string|regexp) meets end of file/
diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb
index 8a6521b..d03cc30 100644
--- a/test/irb/test_context.rb
+++ b/test/irb/test_context.rb
@@ -63,6 +63,13 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_context.rb#L63
       assert_not_match(/rescue _\.class/, e.message)
     end
 
+    def test_evaluate_with_encoding_error_without_lineno
+      assert_raise_with_message(EncodingError, /invalid symbol/) {
+        @context.evaluate(%q[{"\xAE": 1}], 1)
+        # The backtrace of this invalid encoding hash doesn't contain lineno.
+      }
+    end
+
     def test_evaluate_with_onigmo_warning
       assert_warning("(irb):1: warning: character class has duplicated range: /[aa]/\n") do
         @context.evaluate('/[aa]/', 1)
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]