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 <ville@vesilehto.fi>
This commit is contained in:
Ville Vesilehto
2025-09-14 01:14:59 +03:00
committed by GitHub
parent c149567dbe
commit a72a14d88b

View File

@@ -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