Skip to content

Commit

Permalink
Avoid infinite loops when calling inspect
Browse files Browse the repository at this point in the history
Following my earlier changes, some slightly buggy code in one of my
applications triggered this. The problem went away once the code was
corrected but it is obviously worth avoiding this to start with.
  • Loading branch information
chewi committed Nov 20, 2017
1 parent 8f63744 commit 040384e
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/her/model/introspection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@ module Introspection
# @user = User.find(1)
# p @user # => #<User(/users/1) id=1 name="Tobias Fünke">
def inspect
first = Thread.current[:her_inspect_objects].nil?
Thread.current[:her_inspect_objects] = [] if first

resource_path = begin
request_path
rescue Her::Errors::PathError => e
joined = e.missing_parameters.map { |m| "`#{m}`" }.join(", ")
"<unknown path, missing #{joined}>"
end

"#<#{self.class}(#{resource_path}) #{attributes.keys.map { |k| "#{k}=#{attribute_for_inspect(send(k))}" }.join(" ")}>"
result = "#<#{self.class}(#{resource_path}) "

if Thread.current[:her_inspect_objects].include?(self)
result << '...>'
else
Thread.current[:her_inspect_objects] << self
result << attributes.keys.map { |k| "#{k}=#{attribute_for_inspect(send(k))}" }.join(' ') + '>'
end
ensure
Thread.current[:her_inspect_objects] = nil if first
end

private
Expand Down

0 comments on commit 040384e

Please sign in to comment.