mirror of
https://github.com/coredns/coredns.git
synced 2025-10-28 08:44:17 -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{}
|
blocks := [3][]*ast.ImportSpec{}
|
||||||
prevpos := 0
|
prevpos := 0
|
||||||
bl := 0
|
bl := 0
|
||||||
|
reportedTooManyBlocks := false
|
||||||
for _, im := range f.Imports {
|
for _, im := range f.Imports {
|
||||||
line := fs.Position(im.Path.Pos()).Line
|
line := fs.Position(im.Path.Pos()).Line
|
||||||
if line-prevpos > 1 && prevpos > 0 {
|
if line-prevpos > 1 && prevpos > 0 {
|
||||||
bl++
|
bl++
|
||||||
}
|
}
|
||||||
if bl > 2 {
|
if bl > 2 {
|
||||||
|
if !reportedTooManyBlocks {
|
||||||
absPath, _ := filepath.Abs(path)
|
absPath, _ := filepath.Abs(path)
|
||||||
w.Errors = append(w.Errors, fmt.Errorf("more than %d import blocks in %q", bl, absPath))
|
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)
|
blocks[bl] = append(blocks[bl], im)
|
||||||
prevpos = line
|
prevpos = line
|
||||||
|
|||||||
Reference in New Issue
Block a user