Prevent irb from crashing when exception with nil backtrace is raised #434

Closed
wants to merge 2 commits into
from

Conversation

4 participants
Member

makimoto commented Oct 30, 2013

This pull req fixes irb's unexpected crash.

When an exception whose backtrace is nil is raised, irb crashes and exits.
irb requires #backtrace with Exception but some exceptions have not their backtraces.
An example of such exceptions I found is ActionView::Template::Error.
With such exception, irb crashes with NoMethodError as the following:

> irb
irb(main):001:0> require 'action_view'
=> true
irb(main):002:0> e = ActionView::Template::Error.new(:foo, Exception.new)
=> #<ActionView::Template::Error: Exception>
irb(main):003:0> raise e
ActionView::Template::Error: Exception
/Users/makimoto/.rbenv/versions/trunk/lib/ruby/2.1.0/irb.rb:502:in `block (2 levels) in eval_input': undefined method `[]' for nil:NilClass (NoMethodError)
    from /Users/makimoto/.rbenv/versions/trunk/lib/ruby/2.1.0/irb.rb:624:in `signal_status'
    from /Users/makimoto/.rbenv/versions/trunk/lib/ruby/2.1.0/irb.rb:489:in `block in eval_input'
    from /Users/makimoto/.rbenv/versions/trunk/lib/ruby/2.1.0/irb/ruby-lex.rb:247:in `block (2 levels) in each_top_level_statement'
    from /Users/makimoto/.rbenv/versions/trunk/lib/ruby/2.1.0/irb/ruby-lex.rb:233:in `loop'
    from /Users/makimoto/.rbenv/versions/trunk/lib/ruby/2.1.0/irb/ruby-lex.rb:233:in `block in each_top_level_statement'
    from /Users/makimoto/.rbenv/versions/trunk/lib/ruby/2.1.0/irb/ruby-lex.rb:232:in `catch'
    from /Users/makimoto/.rbenv/versions/trunk/lib/ruby/2.1.0/irb/ruby-lex.rb:232:in `each_top_level_statement'
    from /Users/makimoto/.rbenv/versions/trunk/lib/ruby/2.1.0/irb.rb:488:in `eval_input'
    from /Users/makimoto/.rbenv/versions/trunk/lib/ruby/2.1.0/irb.rb:397:in `block in start'
    from /Users/makimoto/.rbenv/versions/trunk/lib/ruby/2.1.0/irb.rb:396:in `catch'
    from /Users/makimoto/.rbenv/versions/trunk/lib/ruby/2.1.0/irb.rb:396:in `start'
    from /Users/makimoto/.rbenv/versions/trunk/bin/irb:11:in `<main>'
> 

With my patch, irb just shows ordinary exception messages without crash.

> irb
irb(main):001:0> require 'action_view'
=> true
irb(main):002:0>  e = ActionView::Template::Error.new(:foo, Exception.new)
=> #<ActionView::Template::Error: Exception>
irb(main):003:0> raise e
ActionView::Template::Error: Exception

irb(main):004:0>

(Of course, such exceptions are bad-mannered. However, they can occur actually. So it should be fixed.)

Member

sorah commented Oct 30, 2013

Patch looks good to me.

Member

zzak commented Oct 30, 2013

Good find! Now try to write a test for it :trollface:

Member

makimoto commented Oct 30, 2013

@zzak okay, how about it? 2327a2914881dc8651b8589deeeedbb8df7c8a3e

Member

nobu commented Oct 30, 2013

Why does that exception class make its backtrace nil?

Member

makimoto commented Oct 30, 2013

@nobu

Why does that exception class make its backtrace nil?

You mean "that exception" is my example ActionView::Template::Error?
In that case, it works as a wrapper of other exceptions and overrides default Exception#backtrace with backtrace of others' exceptions.

Member

nobu commented Oct 30, 2013

Then it seems a bug of ActionView::Template::Error, which overrides #backtrace but not #set_backtrace.

As for the patch, please file a new ticket on https://bugs.ruby-lang.org and ask keiju-san.

Member

makimoto commented Oct 30, 2013

Okay, done. http://bugs.ruby-lang.org/issues/9063
And I'm try fixing ActionView::Template::Error...

@hsbt hsbt closed this in f741fd2 Aug 13, 2014

mmasaki pushed a commit to mmasaki/ruby that referenced this pull request Aug 27, 2014

* lib/irb.rb: Prevent irb from crashing when exception with
  nil backtrace is raised.
  [fix GH-434][ruby-core:58078][Bug #9063]
* test/irb/test_raise_no_backtrace_exception.rb: ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47164 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

@makimoto makimoto deleted the makimoto:irb-when-backtrace-nil branch Sep 20, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment