Skip to content

Commit 13572d8

Browse files
committed
Suppress crashing when EncodingError has occurred without lineno
1 parent 2b96a8d commit 13572d8

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

lib/irb.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,8 @@ def eval_input
554554

555555
def handle_exception(exc)
556556
if exc.backtrace && exc.backtrace[0] =~ /\/irb(2)?(\/.*|-.*|\.rb)?:/ && exc.class.to_s !~ /^IRB/ &&
557-
!(SyntaxError === exc)
557+
!(SyntaxError === exc) && !(EncodingError === exc)
558+
# The backtrace of invalid encoding hash (ex. {"\xAE": 1}) raises EncodingError without lineno.
558559
irb_bug = true
559560
else
560561
irb_bug = false

lib/irb/ruby-lex.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ def check_code_block(code)
211211
else
212212
RubyVM::InstructionSequence.compile(code)
213213
end
214+
rescue EncodingError
215+
# This is for a hash with invalid encoding symbol, {"\xAE": 1}
214216
rescue SyntaxError => e
215217
case e.message
216218
when /unterminated (?:string|regexp) meets end of file/

test/irb/test_context.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ def test_evaluate_with_exception
6363
assert_not_match(/rescue _\.class/, e.message)
6464
end
6565

66+
def test_evaluate_with_encoding_error_without_lineno
67+
assert_raise_with_message(EncodingError, /invalid symbol/) {
68+
@context.evaluate(%q[{"\xAE": 1}], 1)
69+
# The backtrace of this invalid encoding hash doesn't contain lineno.
70+
}
71+
end
72+
6673
def test_evaluate_with_onigmo_warning
6774
assert_warning("(irb):1: warning: character class has duplicated range: /[aa]/\n") do
6875
@context.evaluate('/[aa]/', 1)

0 commit comments

Comments
 (0)