Skip to content

Commit

Permalink
[GR-59866] Improve validation of a Time.new single String argument
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4416
  • Loading branch information
andrykonchin committed Dec 4, 2024
2 parents 1fbf3cf + 661effe commit 9f8855d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
26 changes: 26 additions & 0 deletions spec/ruby/core/time/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,12 @@ def obj.to_int; 3; end
Time.new("2020-12-25")
}.should raise_error(ArgumentError, /no time information|can't parse:/)
end

it "raises ArgumentError if day is missing" do
-> {
Time.new("2020-12")
}.should raise_error(ArgumentError, /no time information|can't parse:/)
end
end

it "raises ArgumentError if subsecond is missing after dot" do
Expand Down Expand Up @@ -679,6 +685,26 @@ def obj.to_int; 3; end
Time.new("2021-11-31 00:00:59 +09:00 abc")
}.should raise_error(ArgumentError, /can't parse.+ abc/)
end

ruby_version_is "3.2.3" do
it "raises ArgumentError when there are leading space characters" do
-> { Time.new(" 2020-12-02 00:00:00") }.should raise_error(ArgumentError, /can't parse/)
-> { Time.new("\t2020-12-02 00:00:00") }.should raise_error(ArgumentError, /can't parse/)
-> { Time.new("\n2020-12-02 00:00:00") }.should raise_error(ArgumentError, /can't parse/)
-> { Time.new("\v2020-12-02 00:00:00") }.should raise_error(ArgumentError, /can't parse/)
-> { Time.new("\f2020-12-02 00:00:00") }.should raise_error(ArgumentError, /can't parse/)
-> { Time.new("\r2020-12-02 00:00:00") }.should raise_error(ArgumentError, /can't parse/)
end

it "raises ArgumentError when there are trailing whitespaces" do
-> { Time.new("2020-12-02 00:00:00 ") }.should raise_error(ArgumentError, /can't parse/)
-> { Time.new("2020-12-02 00:00:00\t") }.should raise_error(ArgumentError, /can't parse/)
-> { Time.new("2020-12-02 00:00:00\n") }.should raise_error(ArgumentError, /can't parse/)
-> { Time.new("2020-12-02 00:00:00\v") }.should raise_error(ArgumentError, /can't parse/)
-> { Time.new("2020-12-02 00:00:00\f") }.should raise_error(ArgumentError, /can't parse/)
-> { Time.new("2020-12-02 00:00:00\r") }.should raise_error(ArgumentError, /can't parse/)
end
end
end
end
end
2 changes: 1 addition & 1 deletion src/main/ruby/truffleruby/core/truffle/time_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def self.new_from_string(time_class, str, **options)
: (?<min> \d{2})
: (?<sec> \d{2})
(?:\. (?<usec> \d+) )?
\s* (?<offset>\S+)?
(?:\s* (?<offset>\S+))?
)?\z/x =~ str
return self.compose(time_class, self.utc_offset_for_compose(offset || options[:in]), year, month, mday, hour, min, sec, usec)
end
Expand Down

0 comments on commit 9f8855d

Please sign in to comment.