1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix #29632 - nil #path leads to NoMethodError in LoadError#is_missing?

See #29632 for details. In short, it's possible to enter `LoadError#is_missing?` when `LoadError#path` returns `nil`, leading to `path.sub` throwing an none-to-helpful `NoMethodError`.

This tiniest of patch inserts `#to_s` before the `sub` call to make sure it succeeds. Affected surface area should be just as tiny since something has already gone wrong to get us into `#is_missing?` and the current behavior when `#path` returns `nil` seems clearly not intended.

[Gannon McGibbon + Neil Souza]
This commit is contained in:
Neil Souza 2017-07-26 19:13:13 +08:00 committed by Gannon McGibbon
parent 98c1432583
commit bfddb67197
2 changed files with 8 additions and 1 deletions

View file

@ -4,6 +4,6 @@ class LoadError
# Returns true if the given path name (except perhaps for the ".rb"
# extension) is the missing file which caused the exception to be raised.
def is_missing?(location)
location.sub(/\.rb$/, "".freeze) == path.sub(/\.rb$/, "".freeze)
location.sub(/\.rb$/, "".freeze) == path.to_s.sub(/\.rb$/, "".freeze)
end
end

View file

@ -7,13 +7,20 @@ class TestLoadError < ActiveSupport::TestCase
def test_with_require
assert_raise(LoadError) { require "no_this_file_don't_exist" }
end
def test_with_load
assert_raise(LoadError) { load "nor_does_this_one" }
end
def test_path
begin load "nor/this/one.rb"
rescue LoadError => e
assert_equal "nor/this/one.rb", e.path
end
end
def test_is_missing_with_nil_path
error = LoadError.new(nil)
assert_nothing_raised { error.is_missing?("anything") }
end
end