Skip to content

Commit 76e231e

Browse files
pixeltrixrafaelfranca
authored andcommitted
Merge pull request rails#11474 from bogdan/time-with-zone-succ
Prevent server blow up when iterating over TimeWithZone Range
1 parent 21b000a commit 76e231e

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require 'active_support/core_ext/range/conversions'
22
require 'active_support/core_ext/range/include_range'
33
require 'active_support/core_ext/range/overlaps'
4+
require 'active_support/core_ext/range/each'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'active_support/core_ext/module/aliasing'
2+
require 'active_support/core_ext/object/acts_like'
3+
4+
class Range #:nodoc:
5+
6+
def each_with_time_with_zone(&block)
7+
ensure_iteration_allowed
8+
each_without_time_with_zone(&block)
9+
end
10+
alias_method_chain :each, :time_with_zone
11+
12+
def step_with_time_with_zone(n = 1, &block)
13+
ensure_iteration_allowed
14+
step_without_time_with_zone(n, &block)
15+
end
16+
alias_method_chain :step, :time_with_zone
17+
18+
private
19+
def ensure_iteration_allowed
20+
if first.acts_like?(:time)
21+
raise TypeError, "can't iterate from #{first.class}"
22+
end
23+
end
24+
end

activesupport/test/core_ext/range_ext_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,26 @@ def test_no_overlaps_on_time
9090
time_range_2 = Time.utc(2005, 12, 10, 17, 31)..Time.utc(2005, 12, 10, 18, 00)
9191
assert !time_range_1.overlaps?(time_range_2)
9292
end
93+
94+
def test_each_on_time_with_zone
95+
twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone['Eastern Time (US & Canada)'] , Time.utc(2006,11,28,10,30))
96+
assert_raises TypeError do
97+
((twz - 1.hour)..twz).each {}
98+
end
99+
end
100+
101+
def test_step_on_time_with_zone
102+
twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone['Eastern Time (US & Canada)'] , Time.utc(2006,11,28,10,30))
103+
assert_raises TypeError do
104+
((twz - 1.hour)..twz).step(1) {}
105+
end
106+
end
107+
108+
def test_include_on_time_with_zone
109+
twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone['Eastern Time (US & Canada)'] , Time.utc(2006,11,28,10,30))
110+
assert_raises TypeError do
111+
((twz - 1.hour)..twz).include?(twz)
112+
end
113+
end
114+
93115
end

0 commit comments

Comments
 (0)