From ffc97a7d04d6ae48ac2c9ae6aa64b132f0617a49 Mon Sep 17 00:00:00 2001 From: WangXiangUSTC Date: Sat, 30 Mar 2019 12:47:45 +0800 Subject: [PATCH] sessionctx: add time zone check (#9822) --- 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 425bb9381c576..b4d6ddef0d70a 100644 --- a/sessionctx/variable/varsutil.go +++ b/sessionctx/variable/varsutil.go @@ -489,9 +489,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 7dd8f5249b278..07e22b47421af 100644 --- a/sessionctx/variable/varsutil_test.go +++ b/sessionctx/variable/varsutil_test.go @@ -143,16 +143,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 {