From a38b2b4c9b7b07cb0f4adc2288a8e8ceaf93ea53 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Sat, 29 Jun 2024 01:06:32 +0800 Subject: [PATCH] executor: fix query panic on INFORMATION_SCHEMA.CLUSTER_SLOW_QUERY with some slow query files --- pkg/executor/slow_query.go | 9 ++------- pkg/executor/slow_query_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/pkg/executor/slow_query.go b/pkg/executor/slow_query.go index 2dd5ecb1b0dbf..f7bb535f70102 100644 --- a/pkg/executor/slow_query.go +++ b/pkg/executor/slow_query.go @@ -1094,13 +1094,8 @@ func readLastLines(ctx context.Context, file *os.File, endCursor int64) ([]strin lines = append(chars, lines...) // nozero // find first '\n' or '\r' - for i := 0; i < len(chars); i++ { - // reach the line end - // the first newline may be in the line end at the first round - if i >= len(lines)-1 { - break - } - if (chars[i] == 10 || chars[i] == 13) && chars[i+1] != 10 && chars[i+1] != 13 { + for i := 0; i < len(chars)-1; i++ { + if (chars[i] == '\n' || chars[i] == '\r') && chars[i+1] != '\n' && chars[i+1] != '\r' { firstNonNewlinePos = i + 1 break } diff --git a/pkg/executor/slow_query_test.go b/pkg/executor/slow_query_test.go index 8d6a1d19f17ec..7347068a7fdb7 100644 --- a/pkg/executor/slow_query_test.go +++ b/pkg/executor/slow_query_test.go @@ -734,3 +734,27 @@ func removeFiles(fileNames []string) { os.Remove(fileName) } } + +func TestIssue54324(t *testing.T) { + f, err := os.CreateTemp("", "test-tidb-slow-query-issue54324") + require.NoError(t, err) + defer os.Remove(f.Name()) // clean up + + w := bufio.NewWriter(f) + for i := 0; i < 8191; i++ { + w.WriteByte('x') + } + w.WriteByte('\n') + for i := 0; i < 4096; i++ { + w.WriteByte('a') + } + require.NoError(t, w.Flush()) + + stat, err := f.Stat() + require.NoError(t, err) + endCursor := stat.Size() + lines, readBytes, err := readLastLines(context.Background(), f, endCursor) + require.NoError(t, err) + require.Len(t, lines, 2) + require.Equal(t, readBytes, 8192+4096) +}