Skip to content

Commit 6057b23

Browse files
authored
executor: fix query panic on INFORMATION_SCHEMA.CLUSTER_SLOW_QUERY with some slow query files (#54325) (#54710)
close #54324
1 parent 45e9157 commit 6057b23

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

pkg/executor/slow_query.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -1147,13 +1147,8 @@ func readLastLines(ctx context.Context, file *os.File, endCursor int64) ([]strin
11471147
lines = append(chars, lines...) // nozero
11481148

11491149
// find first '\n' or '\r'
1150-
for i := 0; i < len(chars); i++ {
1151-
// reach the line end
1152-
// the first newline may be in the line end at the first round
1153-
if i >= len(lines)-1 {
1154-
break
1155-
}
1156-
if (chars[i] == 10 || chars[i] == 13) && chars[i+1] != 10 && chars[i+1] != 13 {
1150+
for i := 0; i < len(chars)-1; i++ {
1151+
if (chars[i] == '\n' || chars[i] == '\r') && chars[i+1] != '\n' && chars[i+1] != '\r' {
11571152
firstNonNewlinePos = i + 1
11581153
break
11591154
}

pkg/executor/slow_query_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -768,3 +768,27 @@ func removeFiles(fileNames []string) {
768768
os.Remove(fileName)
769769
}
770770
}
771+
772+
func TestIssue54324(t *testing.T) {
773+
f, err := os.CreateTemp("", "test-tidb-slow-query-issue54324")
774+
require.NoError(t, err)
775+
defer os.Remove(f.Name()) // clean up
776+
777+
w := bufio.NewWriter(f)
778+
for i := 0; i < 8191; i++ {
779+
w.WriteByte('x')
780+
}
781+
w.WriteByte('\n')
782+
for i := 0; i < 4096; i++ {
783+
w.WriteByte('a')
784+
}
785+
require.NoError(t, w.Flush())
786+
787+
stat, err := f.Stat()
788+
require.NoError(t, err)
789+
endCursor := stat.Size()
790+
lines, readBytes, err := readLastLines(context.Background(), f, endCursor)
791+
require.NoError(t, err)
792+
require.Len(t, lines, 2)
793+
require.Equal(t, readBytes, 8192+4096)
794+
}

0 commit comments

Comments
 (0)