From ebc990461ca29a9288d1c2e008d635cf60068a56 Mon Sep 17 00:00:00 2001 From: WangXiangUSTC Date: Tue, 2 Apr 2019 20:18:10 +0800 Subject: [PATCH] sessionctx: add time zone check (#9822) (#10000) --- sessionctx/variable/varsutil.go | 11 +++++++++++ sessionctx/variable/varsutil_test.go | 18 ++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/sessionctx/variable/varsutil.go b/sessionctx/variable/varsutil.go index 2f863bead4286..b2f02034b8617 100644 --- a/sessionctx/variable/varsutil.go +++ b/sessionctx/variable/varsutil.go @@ -416,9 +416,20 @@ func parseTimeZone(s string) (*time.Location, error) { } // The value can be given as a string indicating an offset from UTC, such as '+10:00' or '-6:00'. + // The time zone's value should in [-12:59,+14:00]. if strings.HasPrefix(s, "+") || strings.HasPrefix(s, "-") { d, err := types.ParseDuration(nil, s[1:], 0) if err == nil { + if s[0] == '-' { + if d.Duration > 12*time.Hour+59*time.Minute { + return nil, ErrUnknownTimeZone.GenWithStackByArgs(s) + } + } else { + if d.Duration > 14*time.Hour { + return nil, ErrUnknownTimeZone.GenWithStackByArgs(s) + } + } + ofst := int(d.Duration / time.Second) if s[0] == '-' { ofst = -ofst diff --git a/sessionctx/variable/varsutil_test.go b/sessionctx/variable/varsutil_test.go index a3708b782e969..3befc3769490e 100644 --- a/sessionctx/variable/varsutil_test.go +++ b/sessionctx/variable/varsutil_test.go @@ -124,16 +124,26 @@ func (s *testVarsutilSuite) TestVarsutil(c *C) { expect string compareValue bool diff time.Duration + err error }{ - {"Europe/Helsinki", "Europe/Helsinki", true, -2 * time.Hour}, - {"US/Eastern", "US/Eastern", true, 5 * time.Hour}, + {"Europe/Helsinki", "Europe/Helsinki", true, -2 * time.Hour, nil}, + {"US/Eastern", "US/Eastern", true, 5 * time.Hour, nil}, //TODO: Check it out and reopen this case. //{"SYSTEM", "Local", false, 0}, - {"+10:00", "", true, -10 * time.Hour}, - {"-6:00", "", true, 6 * time.Hour}, + {"+10:00", "", true, -10 * time.Hour, nil}, + {"-6:00", "", true, 6 * time.Hour, nil}, + {"+14:00", "", true, -14 * time.Hour, nil}, + {"-12:59", "", true, 12*time.Hour + 59*time.Minute, nil}, + {"+14:01", "", false, -14 * time.Hour, ErrUnknownTimeZone.GenWithStackByArgs("+14:01")}, + {"-13:00", "", false, 13 * time.Hour, ErrUnknownTimeZone.GenWithStackByArgs("-13:00")}, } for _, tt := range tests { err = SetSessionSystemVar(v, TimeZone, types.NewStringDatum(tt.input)) + if tt.err != nil { + c.Assert(err, NotNil) + continue + } + c.Assert(err, IsNil) c.Assert(v.TimeZone.String(), Equals, tt.expect) if tt.compareValue {