Skip to content

Commit c501b15

Browse files
authored
Use a thread local variable for recursion checking (#106)
Checking the stack callstack is extremely expensive. Use a thread local variable for detecting recursion and raising an exception.
1 parent 11abf22 commit c501b15

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

lib/paperclip/interpolations.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,14 @@ def filename(attachment, style_name)
5454
# Returns the interpolated URL. Will raise an error if the url itself
5555
# contains ":url" to prevent infinite recursion. This interpolation
5656
# is used in the default :path to ease default specifications.
57-
RIGHT_HERE = "#{__FILE__.gsub(%r{\A\./}, '')}:#{__LINE__ + 3}"
5857
def url(attachment, style_name)
59-
raise Errors::InfiniteInterpolationError if caller.any? { |b| b.index(RIGHT_HERE) }
58+
if Thread.current.thread_variable_get(:kt_paperclip_no_recursion)
59+
raise Errors::InfiniteInterpolationError
60+
end
61+
Thread.current.thread_variable_set(:kt_paperclip_no_recursion, true)
6062
attachment.url(style_name, timestamp: false, escape: false)
63+
ensure
64+
Thread.current.thread_variable_set(:kt_paperclip_no_recursion, false)
6165
end
6266

6367
# Returns the timestamp as defined by the <attachment>_updated_at field

0 commit comments

Comments
 (0)