Skip to content

Commit 578dfae

Browse files
committed
Handle nil backtrace_locations in generate_snippet
1 parent d945ffd commit 578dfae

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

lib/error_highlight/core_ext.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module CoreExt
55
private def generate_snippet
66
if ArgumentError === self && message =~ /\A(?:wrong number of arguments|missing keyword[s]?|unknown keyword[s]?|no keywords accepted)\b/
77
locs = self.backtrace_locations
8-
return "" if locs.size < 2
8+
return "" if locs.nil? || locs.size < 2
99
callee_loc, caller_loc = locs
1010
callee_spot = ErrorHighlight.spot(self, backtrace_location: callee_loc, point_type: :name)
1111
caller_spot = ErrorHighlight.spot(self, backtrace_location: caller_loc, point_type: :name)

test/test_error_highlight.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,30 @@ def test_detailed_message_does_not_raise_when_argument_error_is_rewrapped
18151815
end
18161816
end
18171817

1818+
def test_detailed_message_does_not_raise_when_backtrace_locations_is_nil
1819+
# This reproduces a real-world crash: when an exception crosses a process
1820+
# boundary via Marshal, `backtrace` survives as strings but
1821+
# `backtrace_locations` becomes nil. A keyword ArgumentError then crashed
1822+
# CoreExt#generate_snippet with `undefined method 'size' for nil`.
1823+
begin
1824+
def_with_required_keyword
1825+
rescue ArgumentError => original
1826+
exc = Marshal.load(Marshal.dump(original))
1827+
end
1828+
1829+
assert_nil exc.backtrace_locations
1830+
1831+
msg = nil
1832+
assert_nothing_raised do
1833+
msg = exc.detailed_message(highlight: false)
1834+
end
1835+
assert_match("missing keyword", msg)
1836+
1837+
assert_nothing_raised do
1838+
exc.full_message(highlight: false)
1839+
end
1840+
end
1841+
18181842
private
18191843

18201844
def find_node_by_id(node, node_id)

0 commit comments

Comments
 (0)