Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vet source code using NilAway #73

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ vet: ## Vet the source code
@go run github.com/tetafro/godot/cmd/godot@v1.4.15 .
@go run github.com/tomarrell/wrapcheck/v2/cmd/wrapcheck@v2.8.1 .
@go run golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@2f9d82f .
@go run go.uber.org/nilaway/cmd/nilaway@a267567 .
@go run honnef.co/go/tools/cmd/staticcheck@v0.4.6 .
@go run mvdan.cc/unparam@3ee2d22 .

Expand Down
6 changes: 5 additions & 1 deletion analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,19 @@
var r = regexp.MustCompile(`\$\{\{.*?\}\}`)

func analyzeManifest(manifest *Manifest) []violation {
if manifest.Runs.Using == "composite" {
if manifest != nil && manifest.Runs.Using == "composite" {
return analyzeSteps(manifest.Runs.Steps)
} else {
return make([]violation, 0)

Check failure on line 44 in analyze.go

View workflow job for this annotation

GitHub Actions / Mutation test

invalid argument: index -1 (constant of type int) must not be negative
}
}

func analyzeWorkflow(workflow *Workflow) []violation {
violations := make([]violation, 0)

Check failure on line 49 in analyze.go

View workflow job for this annotation

GitHub Actions / Mutation test

invalid argument: index -1 (constant of type int) must not be negative
if workflow == nil {
return violations
}

for id, job := range workflow.Jobs {
job := job
violations = append(violations, analyzeJob(id, &job)...)
Expand All @@ -61,7 +65,7 @@
name = id
}

violations := make([]violation, 0)

Check failure on line 68 in analyze.go

View workflow job for this annotation

GitHub Actions / Mutation test

invalid argument: index -1 (constant of type int) must not be negative
for _, v := range analyzeSteps(job.Steps) {
v.jobId = name
violations = append(violations, v)
Expand All @@ -71,7 +75,7 @@
}

func analyzeSteps(steps []JobStep) []violation {
violations := make([]violation, 0)

Check failure on line 78 in analyze.go

View workflow job for this annotation

GitHub Actions / Mutation test

invalid argument: index -1 (constant of type int) must not be negative
for i, step := range steps {
step := step
violations = append(violations, analyzeStep(i, &step)...)
Expand All @@ -86,7 +90,7 @@
name = fmt.Sprintf("#%d", id)
}

violations := make([]violation, 0)

Check failure on line 93 in analyze.go

View workflow job for this annotation

GitHub Actions / Mutation test

invalid argument: index -1 (constant of type int) must not be negative
script, kind := extractScript(step)
for _, v := range analyzeScript(script) {
v.kind = kind
Expand All @@ -98,7 +102,7 @@
}

func analyzeScript(script string) []violation {
violations := make([]violation, 0)

Check failure on line 105 in analyze.go

View workflow job for this annotation

GitHub Actions / Mutation test

invalid argument: index -1 (constant of type int) must not be negative
if matches := r.FindAll([]byte(script), len(script)); matches != nil {
for _, problem := range matches {
violations = append(violations, violation{
Expand Down
20 changes: 20 additions & 0 deletions analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func TestAnalyzeManifest(t *testing.T) {
manifest: Manifest{
Runs: ManifestRuns{
Using: "node16",
Steps: []JobStep{
{
Name: "Example unsafe",
Run: "echo ${{ inputs.value }}",
},
},
},
},
want: 0,
Expand Down Expand Up @@ -123,6 +129,13 @@ func TestAnalyzeManifest(t *testing.T) {
}
})
}

t.Run("nil pointer", func(t *testing.T) {
violations := analyzeManifest(nil)
if got, want := len(violations), 0; got != want {
t.Fatalf("Unexpected number of violations (got %d, want %d)", got, want)
}
})
}

func TestAnalyzeWorkflow(t *testing.T) {
Expand Down Expand Up @@ -244,6 +257,13 @@ func TestAnalyzeWorkflow(t *testing.T) {
}
})
}

t.Run("nil pointer", func(t *testing.T) {
violations := analyzeWorkflow(nil)
if got, want := len(violations), 0; got != want {
t.Fatalf("Unexpected number of violations (got %d, want %d)", got, want)
}
})
}

func TestAnalyzeJob(t *testing.T) {
Expand Down
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@

for file, fileViolations := range targetViolations {
if len(fileViolations) > 0 {
if _, ok := violations[target]; !ok {
violations[target] = make(map[string][]violation)
targetViolations, ok := violations[target]
if !ok {
targetViolations = make(map[string][]violation)
violations[target] = targetViolations
}

violations[target][file] = fileViolations
targetViolations[file] = fileViolations
}
}
} else {
Expand Down Expand Up @@ -253,7 +255,7 @@
}

func printJson(rawViolations map[string]map[string][]violation) {
violations := make([]jsonViolation, 0)

Check failure on line 258 in main.go

View workflow job for this annotation

GitHub Actions / Mutation test

invalid argument: index -1 (constant of type int) must not be negative
for target, targetViolations := range rawViolations {
for file, fileViolations := range targetViolations {
for _, fileViolation := range fileViolations {
Expand Down Expand Up @@ -310,8 +312,8 @@
}

func getVariableNameForExpression(expression string) (name string) {
parts := strings.Split(expression, ".")
name = strings.TrimRight(parts[len(parts)-1], "}")
name = expression[strings.LastIndex(expression, ".")+1:]
name = strings.TrimRight(name, "}")
name = strings.TrimSpace(name)
return strings.ToUpper(name)
}
Expand Down