tools/syz-linter: fix comments check

Turns out ast.Inspect does not visit most comments.
Walk file.Comments manually.

Update #1876
This commit is contained in:
Dmitry Vyukov 2020-07-10 18:50:44 +02:00
parent d4c58caef7
commit 78178cfb8c
7 changed files with 24 additions and 29 deletions

View File

@ -38,7 +38,7 @@ func RemoveAll(dir string) error {
func SystemMemorySize() uint64 { func SystemMemorySize() uint64 {
var info syscall.Sysinfo_t var info syscall.Sysinfo_t
syscall.Sysinfo(&info) syscall.Sysinfo(&info)
return uint64(info.Totalram) //nolint:unconvert return uint64(info.Totalram) // nolint:unconvert
} }
func removeImmutable(fname string) error { func removeImmutable(fname string) error {

View File

@ -277,9 +277,9 @@ var zirconOopses = append([]*oops{
// We should detect just "stopping other cpus" as some kernel crash rather then as "lost connection", // We should detect just "stopping other cpus" as some kernel crash rather then as "lost connection",
// but if we add oops for "stopping other cpus", then it will interfere with other formats, // but if we add oops for "stopping other cpus", then it will interfere with other formats,
// because "stopping other cpus" usually goes after "ZIRCON KERNEL PANIC", but sometimes before. Mess. // because "stopping other cpus" usually goes after "ZIRCON KERNEL PANIC", but sometimes before. Mess.
//{ // {
// []byte("stopping other cpus"), // []byte("stopping other cpus"),
//}, // },
{ {
[]byte("welcome to Zircon"), []byte("welcome to Zircon"),
[]oopsFormat{ []oopsFormat{

View File

@ -69,7 +69,7 @@ func TestHintsCheckConstArg(t *testing.T) {
// i16 w = (i16) el // i16 w = (i16) el
// if (w == 0x88) {...} // if (w == 0x88) {...}
// i16 other = 0xfffe // i16 other = 0xfffe
// if (w == other) // if (w == other)
// }; test8(i8(0x1234)); // }; test8(i8(0x1234));
0x34: compSet(0x88, 0x1122, 0xfffffffffffffffe, 0xffffffffffffff0a), 0x34: compSet(0x88, 0x1122, 0xfffffffffffffffe, 0xffffffffffffff0a),
// This following args should be iggnored. // This following args should be iggnored.

View File

@ -280,16 +280,6 @@ mutate4(&(0x7f0000000000)="11223344", 0x4)
`, ` `, `
mutate4(&(0x7f0000000000)="113344", 0x3) mutate4(&(0x7f0000000000)="113344", 0x3)
`}, `},
// Mutate data (insert byte and update size).
// TODO: this is not working, because Mutate constantly tends
// update addresses and insert mmap's.
/*
{`
mutate4(&(0x7f0000000000)="1122", 0x2)
`, `
mutate4(&(0x7f0000000000)="112200", 0x3)
`},
*/
// Mutate data (change byte). // Mutate data (change byte).
{` {`
mutate4(&(0x7f0000000000)="1122", 0x2) mutate4(&(0x7f0000000000)="1122", 0x2)

View File

@ -19,7 +19,7 @@ func parseKernelObject(obj string) (map[string]*dwarf.StructType, error) {
var sections []*elf.Section var sections []*elf.Section
for _, sec := range file.Sections { for _, sec := range file.Sections {
// We don't need these for our purposes and dropping them speeds up parsing a lot. // We don't need these for our purposes and dropping them speeds up parsing a lot.
//nolint:misspell // nolint:misspell
if sec.Name == ".debug_line" || strings.HasPrefix(sec.Name, ".rela.") { if sec.Name == ".debug_line" || strings.HasPrefix(sec.Name, ".rela.") {
continue continue
} }

View File

@ -63,9 +63,12 @@ func run(p *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files { for _, file := range pass.Files {
ast.Inspect(file, func(n ast.Node) bool { ast.Inspect(file, func(n ast.Node) bool {
switch n := n.(type) { switch n := n.(type) {
case *ast.Comment: case *ast.File:
pass.checkMulitlineComments(n) for _, group := range n.Comments {
pass.checkCommentSpace(n) for _, comment := range group.List {
pass.checkComment(comment)
}
}
case *ast.BinaryExpr: case *ast.BinaryExpr:
pass.checkStringLenCompare(n) pass.checkStringLenCompare(n)
case *ast.FuncType: case *ast.FuncType:
@ -94,19 +97,17 @@ func (pass *Pass) typ(e ast.Expr) types.Type {
return pass.TypesInfo.Types[e].Type return pass.TypesInfo.Types[e].Type
} }
// checkMulitlineComments warns about C++-style multiline comments. // checkComment warns about C++-style multiline comments (we don't use them in the codebase)
// We don't use them in the codebase. // and about "//nospace", "// tabs and spaces" and similar.
func (pass *Pass) checkMulitlineComments(n *ast.Comment) { func (pass *Pass) checkComment(n *ast.Comment) {
if !strings.HasPrefix(n.Text, "/*") { if strings.HasPrefix(n.Text, "/*") {
pass.report(n, "Use C-style comments // instead of /* */")
return return
} }
pass.report(n, "Use C-style comments // instead of /* */") if allowedComments.MatchString(n.Text) {
} return
}
// checkCommentSpace warns about "//nospace", "// tabs and spaces" and similar. if strings.HasPrefix(n.Text, "//go:generate") {
func (pass *Pass) checkCommentSpace(n *ast.Comment) {
if !strings.HasPrefix(n.Text, "//") ||
allowedComments.MatchString(n.Text) {
return return
} }
pass.report(n, "Use either //<one-or-more-spaces>comment or //<one-or-more-tabs>comment format for comments") pass.report(n, "Use either //<one-or-more-spaces>comment or //<one-or-more-tabs>comment format for comments")

View File

@ -32,8 +32,12 @@ func returnString() string { return "foo" }
// Tab and spaces. // want "Use either //<one-or-more-spaces>comment or //<one-or-more-tabs>comment format for comments" // Tab and spaces. // want "Use either //<one-or-more-spaces>comment or //<one-or-more-tabs>comment format for comments"
// Space and tab. // want "Use either //<one-or-more-spaces>comment or //<one-or-more-tabs>comment format for comments" // Space and tab. // want "Use either //<one-or-more-spaces>comment or //<one-or-more-tabs>comment format for comments"
func checkCommentSpace() { func checkCommentSpace() {
// Comment without a dot at the end
checkCommentSpace()
} }
//No space. // want "Use either //<one-or-more-spaces>comment or //<one-or-more-tabs>comment format for comments"
func funcArgsGood(a, b int) (int, int) { func funcArgsGood(a, b int) (int, int) {
return 0, 0 return 0, 0
} }