mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user