Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Make JRuby's RubyTime argument checking be same as MRI #1515

Merged
merged 1 commit into from

2 participants

@ratnikov

This should fix #1501

@ratnikov

It seems to me that the travis failure is just flake (although I couldn't figure out whether https://travis-ci.org/jruby/jruby/jobs/19282757 is failing because of a test failure or just issues with running it)

@enebo enebo merged commit e0c4430 into jruby:jruby-1_7
@enebo enebo added this to the JRuby 1.7.11 milestone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 20, 2014
  1. @ratnikov
This page is out of date. Refresh to see the latest.
View
20 core/src/main/java/org/jruby/RubyTime.java
@@ -1211,9 +1211,6 @@ protected static RubyTime s_mload(IRubyObject recv, RubyTime time, IRubyObject f
}
}
- private static final int[] time_min = {1, 0, 0, 0, Integer.MIN_VALUE};
- private static final int[] time_max = {31, 23, 59, 60, Integer.MAX_VALUE};
-
private static final int ARG_SIZE = 7;
private static RubyTime createTime(IRubyObject recv, IRubyObject[] args, boolean gmt) {
@@ -1301,14 +1298,21 @@ private static RubyTime createTime(IRubyObject recv, IRubyObject[] args, boolean
}
}
- long value = RubyNumeric.num2long(args[i + 2]);
- if (time_min[i] > value || value > time_max[i]) {
- throw runtime.newArgumentError("argument out of range.");
- }
- int_args[i] = (int) value;
+ int_args[i] = RubyNumeric.num2int(args[i + 2]);
}
}
+ // Validate the times
+ // Complying with MRI behavior makes it a little bit complicated. Logic copied from:
+ // https://github.com/ruby/ruby/blob/trunk/time.c#L2609
+ if ( (int_args[0] < 1 || int_args[0] > 31)
+ || (int_args[1] < 0 || int_args[1] > 24)
+ || (int_args[1] == 24 && (int_args[2] > 0 || int_args[3] > 0))
+ || (int_args[2] < 0 || int_args[2] > 59)
+ || (int_args[3] < 0 || int_args[3] > 60)) {
+ throw runtime.newArgumentError("argument out of range.");
+ }
+
if (!runtime.is1_9()) {
if (0 <= year && year < 39) {
year += 2000;
View
19 spec/ruby/core/time/shared/gm.rb
@@ -20,4 +20,23 @@
time.inspect.should == "2000-01-01 20:15:01 UTC"
end
end
+
+ it "allows leap seconds like MRI does" do
+ # From MRI code: https://github.com/ruby/ruby/blob/trunk/time.c#L2609
+ next_day = Time.send(@method, 2000, 1, 2)
+
+ Time.send(@method, 2000, 1, 1, 24).should == next_day
+ Time.send(@method, 2000, 1, 1, 23, 59, 60).should == next_day
+ end
+
+ it "validates time like MRI does" do
+ [
+ [ 2000, 1, 1, 24, 1 ],
+ [ 2000, 1, 1, 23, 59, 61 ],
+ [ 2000, 1, 1, 25, 0, 0 ]
+ ].each do |bad_args|
+ proc { Time.send(@method, *bad_args) }.should raise_error(
+ ArgumentError, /out of range/)
+ end
+ end
end
Something went wrong with that request. Please try again.