Permalink
Browse files

Prevent recursive data structures

  • Loading branch information...
1 parent 4917739 commit 9f153b33e6f0e44d73535d297f61f4db96a8436c @jyurek jyurek committed Mar 16, 2011
Showing with 15 additions and 3 deletions.
  1. +5 −3 lib/hoptoad_notifier/notice.rb
  2. +10 −0 test/recursion_test.rb
View
8 lib/hoptoad_notifier/notice.rb
@@ -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
View
10 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.