Skip to content

Commit

Permalink
🐛 Fix loop aliasing errors causing linter to fail. (ossf#3414)
Browse files Browse the repository at this point in the history
Signed-off-by: Spencer Schrock <[email protected]>
Signed-off-by: Allen Shearin <[email protected]>
  • Loading branch information
spencerschrock authored and ashearin committed Nov 13, 2023
1 parent 87429c6 commit a1c42bf
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
14 changes: 12 additions & 2 deletions checks/evaluation/permissions/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ func TokenPermissions(name string, c *checker.CheckRequest, r *checker.TokenPerm
"GitHub workflow tokens follow principle of least privilege")
}

// avoid memory aliasing by returning a new copy.
func newUint(u uint) *uint {
return &u
}

// avoid memory aliasing by returning a new copy.
func newStr(s string) *string {
return &s
}

func applyScorePolicy(results *checker.TokenPermissionsData, c *checker.CheckRequest) (int, error) {
// See list https://github.blog/changelog/2021-04-20-github-actions-control-permissions-for-github_token/.
// Note: there are legitimate reasons to use some of the permissions like checks, deployments, etc.
Expand All @@ -83,10 +93,10 @@ func applyScorePolicy(results *checker.TokenPermissionsData, c *checker.CheckReq
loc = &finding.Location{
Type: r.File.Type,
Path: r.File.Path,
LineStart: &r.File.Offset,
LineStart: newUint(r.File.Offset),
}
if r.File.Snippet != "" {
loc.Snippet = &r.File.Snippet
loc.Snippet = newStr(r.File.Snippet)
}
}

Expand Down
4 changes: 2 additions & 2 deletions clients/git/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func createTestRepo(t *testing.T) (path string) {
return dir
}

//nolint:testparallel
//nolint:paralleltest
func TestInitRepo(t *testing.T) {
tests := []struct { //nolint:govet
name string
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestListCommits(t *testing.T) {
}
}

//nolint:testparallel
//nolint:paralleltest
func TestSearch(t *testing.T) {
testCases := []struct {
name string
Expand Down
4 changes: 0 additions & 4 deletions clients/gitlabrepo/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ func (s suffixStubTripper) RoundTrip(r *http.Request) (*http.Response, error) {
}, nil
}

func strptr(s string) *string {
return &s
}

func associationptr(r clients.RepoAssociation) *clients.RepoAssociation {
return &r
}
Expand Down
7 changes: 6 additions & 1 deletion clients/gitlabrepo/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ func (handler *workflowsHandler) listSuccessfulWorkflowRuns(filename string) ([]
return workflowsRunsFrom(jobs, filename), nil
}

// avoid memory aliasing by returning a new copy.
func strptr(s string) *string {
return &s
}

func workflowsRunsFrom(data []*gitlab.Job, filename string) []clients.WorkflowRun {
var workflowRuns []clients.WorkflowRun
for _, job := range data {
// Find a better way to do this.
for _, artifact := range job.Artifacts {
if strings.EqualFold(artifact.Filename, filename) {
workflowRuns = append(workflowRuns, clients.WorkflowRun{
HeadSHA: &job.Pipeline.Sha,
HeadSHA: strptr(job.Pipeline.Sha),
URL: job.WebURL,
})
continue
Expand Down
6 changes: 3 additions & 3 deletions pkg/json_raw_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (r *jsonScorecardRawResult) addTokenPermissionsRawResults(tp *checker.Token
Offset: t.File.Offset,
}
if t.File.Snippet != "" {
p.File.Snippet = &t.File.Snippet
p.File.Snippet = asPointer(t.File.Snippet)
}
}

Expand Down Expand Up @@ -361,7 +361,7 @@ func (r *jsonScorecardRawResult) addPackagingRawResults(pk *checker.PackagingDat
}

if p.File.Snippet != "" {
jpk.File.Snippet = &p.File.Snippet
jpk.File.Snippet = asPointer(p.File.Snippet)
}

for _, run := range p.Runs {
Expand Down Expand Up @@ -419,7 +419,7 @@ func (r *jsonScorecardRawResult) addDangerousWorkflowRawResults(df *checker.Dang
Type: string(e.Type),
}
if e.File.Snippet != "" {
v.File.Snippet = &e.File.Snippet
v.File.Snippet = asPointer(e.File.Snippet)
}
if e.Job != nil {
v.Job = &jsonWorkflowJob{
Expand Down

0 comments on commit a1c42bf

Please sign in to comment.