Skip to content

Commit

Permalink
Suppress crashing when EncodingError has occurred without lineno
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed Mar 26, 2020
1 parent 2b96a8d commit 13572d8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,8 @@ def eval_input

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
Expand Down
2 changes: 2 additions & 0 deletions lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ def check_code_block(code)
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/
Expand Down
7 changes: 7 additions & 0 deletions test/irb/test_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ def test_evaluate_with_exception
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)
Expand Down

0 comments on commit 13572d8

Please sign in to comment.