]> Cypherpunks repositories - gostls13.git/commitdiff
time: reject tzdata with no zones
authorJosh Bleecher Snyder <josharian@gmail.com>
Fri, 28 Dec 2018 00:23:29 +0000 (14:23 -1000)
committerJosh Bleecher Snyder <josharian@gmail.com>
Wed, 27 Feb 2019 18:08:03 +0000 (18:08 +0000)
Fixes #29437

Change-Id: Ice0a03a543e564d66651bfdfce5cd32ebaa35926
Reviewed-on: https://go-review.googlesource.com/c/155746
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/time/zoneinfo_read.go
src/time/zoneinfo_test.go

index d54632fb49052f2288df9b3cc7f32fa6b5c57839..1e559a62cc291cec68e560b98eabe0275ec85083 100644 (file)
@@ -216,7 +216,13 @@ func LoadLocationFromTZData(name string, data []byte) (*Location, error) {
        // Now we can build up a useful data structure.
        // First the zone information.
        //      utcoff[4] isdst[1] nameindex[1]
-       zone := make([]zone, n[NZone])
+       nzone := n[NZone]
+       if nzone == 0 {
+               // Reject tzdata files with no zones. There's nothing useful in them.
+               // This also avoids a panic later when we add and then use a fake transition (golang.org/issue/29437).
+               return nil, badData
+       }
+       zone := make([]zone, nzone)
        for i := range zone {
                var ok bool
                var n uint32
index cd0731768e717c2c8fd2239cb37f460476d2c346..a7ef10c6bc32087ed4246ca3ac6a20d4a6804ff8 100644 (file)
@@ -173,3 +173,12 @@ func TestEarlyLocation(t *testing.T) {
                t.Errorf("Zone offset == %d, want %d", tzOffset, want)
        }
 }
+
+func TestMalformedTZData(t *testing.T) {
+       // The goal here is just that malformed tzdata results in an error, not a panic.
+       issue29437 := "TZif\x00000000000000000\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0000"
+       _, err := time.LoadLocationFromTZData("abc", []byte(issue29437))
+       if err == nil {
+               t.Error("expected error, got none")
+       }
+}