From a72a14d88bbabc1a3c73d637629964d96f9cf42d Mon Sep 17 00:00:00 2001 From: Ville Vesilehto Date: Sun, 14 Sep 2025 01:14:59 +0300 Subject: [PATCH] test(presubmit): prevent panic in TestImportOrdering on split import (#7540) Fix a panic in presubmit test when import statements are split into >3 logical blocks (e.g., std, coredns, then third party in multiple blocks). The computed block index could exceed the fixed array bounds. Signed-off-by: Ville Vesilehto --- test/presubmit_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/presubmit_test.go b/test/presubmit_test.go index 3f8858926..02a5ac497 100644 --- a/test/presubmit_test.go +++ b/test/presubmit_test.go @@ -238,14 +238,20 @@ func (w *testImportOrderingWalker) walk(path string, info os.FileInfo, _ error) blocks := [3][]*ast.ImportSpec{} prevpos := 0 bl := 0 + reportedTooManyBlocks := false for _, im := range f.Imports { line := fs.Position(im.Path.Pos()).Line if line-prevpos > 1 && prevpos > 0 { bl++ } if bl > 2 { - absPath, _ := filepath.Abs(path) - w.Errors = append(w.Errors, fmt.Errorf("more than %d import blocks in %q", bl, absPath)) + if !reportedTooManyBlocks { + absPath, _ := filepath.Abs(path) + w.Errors = append(w.Errors, fmt.Errorf("more than %d import blocks in %q", bl, absPath)) + reportedTooManyBlocks = true + } + // Clamp to last valid block index to avoid out-of-bounds access + bl = 2 } blocks[bl] = append(blocks[bl], im) prevpos = line