From b0d0648d4c58e03ad4e5936182ab637873c04679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Sun, 27 Dec 2020 01:12:54 +0100 Subject: [PATCH 1/2] Improve handling edge cases for Time::Location#load --- spec/std/time/location_spec.cr | 18 +++++++++++++++--- src/time/location/loader.cr | 7 +++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/spec/std/time/location_spec.cr b/spec/std/time/location_spec.cr index b08f0bc7a6ad..39d9a4e2298b 100644 --- a/spec/std/time/location_spec.cr +++ b/spec/std/time/location_spec.cr @@ -34,11 +34,23 @@ class Time::Location end it "invalid timezone identifier" do - expect_raises(InvalidLocationNameError, "Foobar/Baz") do - Location.load("Foobar/Baz") + with_zoneinfo(datapath("zoneinfo")) do + expect_raises(InvalidLocationNameError, "Foobar/Baz") do + Location.load("Foobar/Baz") + end end - Location.load?("Foobar/Baz", Crystal::System::Time.zone_sources).should be_nil + Location.load?("Foobar/Baz", [datapath("zoneinfo")]).should be_nil + end + + it "name is folder" do + Location.load?("Foo", [datapath("zoneinfo")]).should be_nil + end + + it "invalid zone file" do + expect_raises(Time::Location::InvalidTZDataError) do + Location.load?("Foo/invalid", [datapath("zoneinfo")]) + end end it "treats UTC as special case" do diff --git a/src/time/location/loader.cr b/src/time/location/loader.cr index 29c7c52add8b..d25789ce3305 100644 --- a/src/time/location/loader.cr +++ b/src/time/location/loader.cr @@ -63,11 +63,12 @@ class Time::Location def self.find_zoneinfo_file(name : String, sources : Enumerable(String)) sources.each do |source| if source.ends_with?(".zip") - return source if File.exists?(source) + path = source else path = File.join(source, name) - return source if File.exists?(path) end + + return source if File.exists?(path) && File.file?(path) && File.readable?(path) end end @@ -145,6 +146,8 @@ class Time::Location end new(location_name, zones, transitions) + rescue exc : IO::Error + raise InvalidTZDataError.new(cause: exc) end private def self.read_int32(io : IO) From 2bf1a8de10f86441b1f7f2fa7dcc1eb6d4d521f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Sun, 27 Dec 2020 01:48:48 +0100 Subject: [PATCH 2/2] fixup! Improve handling edge cases for Time::Location#load --- spec/std/data/zoneinfo/Foo/invalid | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 spec/std/data/zoneinfo/Foo/invalid diff --git a/spec/std/data/zoneinfo/Foo/invalid b/spec/std/data/zoneinfo/Foo/invalid new file mode 100644 index 000000000000..e69de29bb2d1