From cab4d09ead9ad6914e0dc61f6dd3b02ea5c1cdff Mon Sep 17 00:00:00 2001 From: bb7133 Date: Sun, 21 Aug 2022 17:26:10 +0800 Subject: [PATCH] sessionctx/variable: fix range check for @@timestamp --- 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 8979518a8d6a5..5abfc54ac26b8 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 0467dbba9f620..28a0315fb93f6 100644 --- a/sessionctx/variable/sysvar_test.go +++ b/sessionctx/variable/sysvar_test.go @@ -821,6 +821,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 e1bae81b229d5..2520b669e22e1 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