Skip to content
This repository has been archived by the owner on Nov 11, 2017. It is now read-only.

Commit

Permalink
Prevent recursive data structures
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Mar 16, 2011
1 parent 4917739 commit 9f153b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/hoptoad_notifier/notice.rb
Expand Up @@ -226,14 +226,16 @@ def clean_unserializable_data_from(attribute)
# Removes non-serializable data. Allowed data types are strings, arrays,
# and hashes. All other types are converted to strings.
# TODO: move this onto Hash
def clean_unserializable_data(data)
def clean_unserializable_data(data, stack = [])
return "[possible infinite recursion halted]" if stack.any?{|item| item == data.object_id }

if data.respond_to?(:to_hash)
data.to_hash.inject({}) do |result, (key, value)|
result.merge(key => clean_unserializable_data(value))
result.merge(key => clean_unserializable_data(value, stack + [data.object_id]))
end
elsif data.respond_to?(:to_ary)
data.collect do |value|
clean_unserializable_data(value)
clean_unserializable_data(value, stack + [data.object_id])
end
else
data.to_s
Expand Down
10 changes: 10 additions & 0 deletions test/recursion_test.rb
@@ -0,0 +1,10 @@
require File.dirname(__FILE__) + '/helper'

class RecursionTest < Test::Unit::TestCase
should "not allow infinite recursion" do
hash = {:a => :a}
hash[:hash] = hash
notice = HoptoadNotifier::Notice.new(:parameters => hash)
assert_equal "[possible infinite recursion halted]", notice.parameters[:hash]
end
end

0 comments on commit 9f153b3

Please sign in to comment.