aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2020-06-06 11:34:20 +0200
committerDmitry Vyukov <dvyukov@google.com>2020-06-07 10:41:01 +0200
commit3f04838a10346e60b9dd81f0adb47c2518486d28 (patch)
tree76348cbffe82c8126b2f01ad5e1b6d757c1d3007
parentcdf1aa4dc338ddd37917942e7d6b992f4e079a00 (diff)
.golangci.yml: make goconst checker more strict
-rw-r--r--.golangci.yml7
-rw-r--r--pkg/ast/ast.go2
-rw-r--r--pkg/compiler/check.go8
-rw-r--r--pkg/csource/options.go16
-rw-r--r--prog/rand.go8
-rw-r--r--tools/syz-check/check.go13
6 files changed, 33 insertions, 21 deletions
diff --git a/.golangci.yml b/.golangci.yml
index 010e10490..7bee35ebf 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -76,8 +76,8 @@ linters-settings:
dupl:
threshold: 60
goconst:
- min-len: 7
- min-occurrences: 4
+ min-len: 3
+ min-occurrences: 3
nestif:
# TODO: consider reducing this value.
min-complexity: 12
@@ -109,3 +109,6 @@ issues:
- path: (dashboard/app/.*_test\.go)
linters:
- dupl
+ - path: (prog/.*_test\.go)
+ linters:
+ - goconst
diff --git a/pkg/ast/ast.go b/pkg/ast/ast.go
index 60b984134..991d27ffc 100644
--- a/pkg/ast/ast.go
+++ b/pkg/ast/ast.go
@@ -216,7 +216,7 @@ type Type struct {
}
func (n *Type) Info() (Pos, string, string) {
- return n.Pos, "type", n.Ident
+ return n.Pos, "type-opt", n.Ident
}
type Field struct {
diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go
index 2e4dd3d1b..317ce5ae5 100644
--- a/pkg/compiler/check.go
+++ b/pkg/compiler/check.go
@@ -184,6 +184,8 @@ func (comp *compiler) checkFieldGroup(fields []*ast.Field, what, ctx string) {
}
}
+const argBase = "BASE"
+
func (comp *compiler) checkTypedefs() {
for _, decl := range comp.desc.Nodes {
switch n := decl.(type) {
@@ -200,7 +202,7 @@ func (comp *compiler) checkTypedefs() {
// For templates we only do basic checks of arguments.
names := make(map[string]bool)
for i, arg := range n.Args {
- if arg.Name == "BASE" && i != len(n.Args)-1 {
+ if arg.Name == argBase && i != len(n.Args)-1 {
comp.error(arg.Pos, "type argument BASE must be the last argument")
}
if names[arg.Name] {
@@ -857,7 +859,7 @@ func (comp *compiler) replaceTypedef(ctx *checkCtx, t *ast.Type, flags checkFlag
}
typedef := comp.typedefs[typedefName]
// Handling optional BASE argument.
- if len(typedef.Args) > 0 && typedef.Args[len(typedef.Args)-1].Name == "BASE" {
+ if len(typedef.Args) > 0 && typedef.Args[len(typedef.Args)-1].Name == argBase {
if flags&checkIsArg != 0 && len(t.Args) == len(typedef.Args)-1 {
t.Args = append(t.Args, &ast.Type{
Pos: t.Pos,
@@ -889,7 +891,7 @@ func (comp *compiler) replaceTypedef(ctx *checkCtx, t *ast.Type, flags checkFlag
if nargs == 0 {
comp.error(t.Pos, "type %v is not a template", typedefName)
} else {
- if flags&checkIsArg != 0 && typedef.Args[len(typedef.Args)-1].Name == "BASE" {
+ if flags&checkIsArg != 0 && typedef.Args[len(typedef.Args)-1].Name == argBase {
nargs--
}
comp.error(t.Pos, "template %v needs %v arguments instead of %v",
diff --git a/pkg/csource/options.go b/pkg/csource/options.go
index 01389240c..b8fb804fb 100644
--- a/pkg/csource/options.go
+++ b/pkg/csource/options.go
@@ -260,21 +260,25 @@ func defaultFeatures(value bool) Features {
}
func ParseFeaturesFlags(enable string, disable string, defaultValue bool) (Features, error) {
- if enable == "none" && disable == "none" {
+ const (
+ none = "none"
+ all = "all"
+ )
+ if enable == none && disable == none {
return defaultFeatures(defaultValue), nil
}
- if enable != "none" && disable != "none" {
+ if enable != none && disable != none {
return nil, fmt.Errorf("can't use -enable and -disable flags at the same time")
}
- if enable == "all" || disable == "" {
+ if enable == all || disable == "" {
return defaultFeatures(true), nil
}
- if disable == "all" || enable == "" {
+ if disable == all || enable == "" {
return defaultFeatures(false), nil
}
var items []string
var features Features
- if enable != "none" {
+ if enable != none {
items = strings.Split(enable, ",")
features = defaultFeatures(false)
} else {
@@ -286,7 +290,7 @@ func ParseFeaturesFlags(enable string, disable string, defaultValue bool) (Featu
return nil, fmt.Errorf("unknown feature specified: %s", item)
}
feature := features[item]
- feature.Enabled = (enable != "none")
+ feature.Enabled = enable != none
features[item] = feature
}
return features, nil
diff --git a/prog/rand.go b/prog/rand.go
index c7c2c9bed..3112fc50a 100644
--- a/prog/rand.go
+++ b/prog/rand.go
@@ -435,8 +435,7 @@ func (r *randGen) createResource(s *state, res *ResourceType, dir Dir) (arg Arg,
func (r *randGen) generateText(kind TextKind) []byte {
switch kind {
case TextTarget:
- if r.target.Arch == "amd64" || r.target.Arch == "386" {
- cfg := createTargetIfuzzConfig(r.target)
+ if cfg := createTargetIfuzzConfig(r.target); cfg != nil {
return ifuzz.Generate(cfg, r.Rand)
}
fallthrough
@@ -456,8 +455,7 @@ func (r *randGen) generateText(kind TextKind) []byte {
func (r *randGen) mutateText(kind TextKind, text []byte) []byte {
switch kind {
case TextTarget:
- if r.target.Arch == "amd64" || r.target.Arch == "386" {
- cfg := createTargetIfuzzConfig(r.target)
+ if cfg := createTargetIfuzzConfig(r.target); cfg != nil {
return ifuzz.Mutate(cfg, r.Rand, text)
}
fallthrough
@@ -489,7 +487,7 @@ func createTargetIfuzzConfig(target *Target) *ifuzz.Config {
case "386":
cfg.Mode = ifuzz.ModeProt32
default:
- panic("unknown text kind")
+ return nil
}
return cfg
}
diff --git a/tools/syz-check/check.go b/tools/syz-check/check.go
index 3c12ba3da..a94090a6a 100644
--- a/tools/syz-check/check.go
+++ b/tools/syz-check/check.go
@@ -541,9 +541,14 @@ func isNetlinkPolicy(fields []prog.Field) bool {
return haveAttr
}
+const (
+ nlattrT = "nlattr_t"
+ nlattrTT = "nlattr_tt"
+)
+
func isNlattr(typ prog.Type) bool {
name := typ.TemplateName()
- return name == "nlattr_t" || name == "nlattr_tt"
+ return name == nlattrT || name == nlattrTT
}
func checkNetlinkPolicy(structMap map[string]prog.Type, typ prog.Type, fields []prog.Field,
@@ -586,7 +591,7 @@ func checkNetlinkPolicy(structMap map[string]prog.Type, typ prog.Type, fields []
func checkNetlinkAttr(typ *prog.StructType, policy nlaPolicy) string {
payload := typ.Fields[2].Type
- if typ.TemplateName() == "nlattr_tt" {
+ if typ.TemplateName() == nlattrTT {
payload = typ.Fields[4].Type
}
if warn := checkAttrType(typ, payload, policy); warn != "" {
@@ -657,11 +662,11 @@ func checkAttrType(typ *prog.StructType, payload prog.Type, policy nlaPolicy) st
return "expect string"
}
case NLA_NESTED:
- if typ.TemplateName() != "nlattr_tt" || typ.Fields[3].Type.(*prog.ConstType).Val != 1 {
+ if typ.TemplateName() != nlattrTT || typ.Fields[3].Type.(*prog.ConstType).Val != 1 {
return "should be nlnest"
}
case NLA_BITFIELD32:
- if typ.TemplateName() != "nlattr_t" || payload.TemplateName() != "nla_bitfield32" {
+ if typ.TemplateName() != nlattrT || payload.TemplateName() != "nla_bitfield32" {
return "should be nlattr[nla_bitfield32]"
}
case NLA_NESTED_ARRAY, NLA_REJECT: