diff --git a/infoschema/tables.go b/infoschema/tables.go index 656a99f52d838..8df6cab9cb84c 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -1729,8 +1729,8 @@ func FormatTiDBVersion(TiDBVersion string, isDefaultVersion bool) string { // The user hasn't set the config 'ServerVersion'. if isDefaultVersion { - nodeVersion = TiDBVersion[strings.LastIndex(TiDBVersion, "TiDB-")+len("TiDB-"):] - if nodeVersion[0] == 'v' { + nodeVersion = TiDBVersion[strings.Index(TiDBVersion, "TiDB-")+len("TiDB-"):] + if len(nodeVersion) > 0 && nodeVersion[0] == 'v' { nodeVersion = nodeVersion[1:] } nodeVersions := strings.Split(nodeVersion, "-") diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index 3650f2833a1a6..aee92b0afed70 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -676,8 +676,14 @@ func TestSelectHiddenColumn(t *testing.T) { func TestFormatVersion(t *testing.T) { // Test for defaultVersions. - defaultVersions := []string{"5.7.25-TiDB-None", "5.7.25-TiDB-8.0.18", "5.7.25-TiDB-8.0.18-beta.1", "5.7.25-TiDB-v4.0.0-beta-446-g5268094af"} - defaultRes := []string{"None", "8.0.18", "8.0.18-beta.1", "4.0.0-beta"} + defaultVersions := []string{ + "5.7.25-TiDB-None", + "5.7.25-TiDB-8.0.18", + "5.7.25-TiDB-8.0.18-beta.1", + "5.7.25-TiDB-v4.0.0-beta-446-g5268094af", + "5.7.25-TiDB-", + "5.7.25-TiDB-v4.0.0-TiDB-446"} + defaultRes := []string{"None", "8.0.18", "8.0.18-beta.1", "4.0.0-beta", "", "4.0.0-TiDB"} for i, v := range defaultVersions { version := infoschema.FormatTiDBVersion(v, true) require.Equal(t, defaultRes[i], version)