From 2c22fff57100f9b44f5d0215dbe0a77b06d2dc15 Mon Sep 17 00:00:00 2001 From: bb7133 Date: Tue, 23 Aug 2022 16:30:20 +0800 Subject: [PATCH 1/2] sessionctx/variable: fix range check for @@timestamp (#37249) close pingcap/tidb#31585 --- sessionctx/variable/sysvar.go | 8 +++++++- sessionctx/variable/sysvar_test.go | 18 ++++++++++++++++++ sessionctx/variable/tidb_vars.go | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index b8c4e2d596ac7..f9458c9a54de5 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -77,7 +77,7 @@ var defaultSysVars = []*SysVar{ {Scope: ScopeNone, Name: TiDBAllowFunctionForExpressionIndex, ReadOnly: true, Value: collectAllowFuncName4ExpressionIndex()}, /* The system variables below have SESSION scope */ - {Scope: ScopeSession, Name: Timestamp, Value: DefTimestamp, MinValue: 0, MaxValue: 2147483647, Type: TypeFloat, GetSession: func(s *SessionVars) (string, error) { + {Scope: ScopeSession, Name: Timestamp, Value: DefTimestamp, MinValue: 0, MaxValue: math.MaxInt32, Type: TypeFloat, GetSession: func(s *SessionVars) (string, error) { if timestamp, ok := s.systems[Timestamp]; ok && timestamp != DefTimestamp { return timestamp, nil } @@ -86,6 +86,12 @@ var defaultSysVars = []*SysVar{ }, GetStateValue: func(s *SessionVars) (string, bool, error) { timestamp, ok := s.systems[Timestamp] return timestamp, ok && timestamp != DefTimestamp, nil + }, Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) { + val := tidbOptFloat64(originalValue, DefTimestampFloat) + if val > math.MaxInt32 { + return originalValue, ErrWrongValueForVar.GenWithStackByArgs(Timestamp, originalValue) + } + return normalizedValue, nil }}, {Scope: ScopeSession, Name: WarningCount, Value: "0", ReadOnly: true, GetSession: func(s *SessionVars) (string, error) { return strconv.Itoa(s.SysWarningCount), nil diff --git a/sessionctx/variable/sysvar_test.go b/sessionctx/variable/sysvar_test.go index 5acba5474af22..58259cfe8ce36 100644 --- a/sessionctx/variable/sysvar_test.go +++ b/sessionctx/variable/sysvar_test.go @@ -820,6 +820,24 @@ func TestTimestamp(t *testing.T) { require.NoError(t, err) require.NotEqual(t, "", val) require.NotEqual(t, "10", val) + + // Test validating a value that less than the minimum one. + sv := GetSysVar(Timestamp) + _, err = sv.Validate(vars, "-5", ScopeSession) + require.NoError(t, err) + warn := vars.StmtCtx.GetWarnings()[0].Err + require.Equal(t, "[variable:1292]Truncated incorrect timestamp value: '-5'", warn.Error()) + + // Test validating values that larger than the maximum one. + _, err = sv.Validate(vars, "3147483698", ScopeSession) + require.Equal(t, "[variable:1231]Variable 'timestamp' can't be set to the value of '3147483698'", err.Error()) + + _, err = sv.Validate(vars, "2147483648", ScopeSession) + require.Equal(t, "[variable:1231]Variable 'timestamp' can't be set to the value of '2147483648'", err.Error()) + + // Test validating the maximum value. + _, err = sv.Validate(vars, "2147483647", ScopeSession) + require.NoError(t, err) } func TestIdentity(t *testing.T) { diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index b0b7ba037ee2d..e5985c30959ec 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -958,6 +958,7 @@ const ( DefTiDBRegardNULLAsPoint = true DefEnablePlacementCheck = true DefTimestamp = "0" + DefTimestampFloat = 0.0 DefTiDBEnableStmtSummary = true DefTiDBStmtSummaryInternalQuery = false DefTiDBStmtSummaryRefreshInterval = 1800 From 11dbd0b881671db0110e90dec3e0ebeceaad5fc1 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 23 Aug 2022 17:40:20 +0800 Subject: [PATCH 2/2] *: update bazel config (#37313) --- bindinfo/BUILD.bazel | 1 + types/json/BUILD.bazel | 1 + 2 files changed, 2 insertions(+) diff --git a/bindinfo/BUILD.bazel b/bindinfo/BUILD.bazel index f1af14fd5319f..a95b8f4fd0421 100644 --- a/bindinfo/BUILD.bazel +++ b/bindinfo/BUILD.bazel @@ -57,6 +57,7 @@ go_test( ], embed = [":bindinfo"], flaky = True, + shard_count = 50, deps = [ "//config", "//domain", diff --git a/types/json/BUILD.bazel b/types/json/BUILD.bazel index 8bc9cf5f61e0d..12321a21f60fd 100644 --- a/types/json/BUILD.bazel +++ b/types/json/BUILD.bazel @@ -12,6 +12,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//errno", + "//parser/mysql", "//parser/terror", "//util/dbterror", "//util/hack",