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

linters: add makezero and fix a related bug #1832

Merged
merged 1 commit into from
Dec 5, 2023

Conversation

mtardy
Copy link
Member

@mtardy mtardy commented Dec 4, 2023

Makezero is a linter to ensure that slices are not initialized with non-zero length. Since in Go, we most of the time use append on the slice, initializing it with non-zero length can tend to create a slice full of zero that will clobber the beginning of the slice.

In the bug fixed here, we created a list with 300+ zeros and then append after them, only to skip the zero in the calling method. Usually we want to prealloc the slice with a capacity of len but a length of 0, i.e. replace make([]int, len(nums)) with make([]int, 0, len(nums)). See more info here https://github.com/ashanbrown/makezero.

@mtardy mtardy added area/ci Related to CI release-note/misc This PR makes changes that have no direct user impact. labels Dec 4, 2023
@mtardy mtardy requested a review from a team as a code owner December 4, 2023 11:33
@mtardy mtardy requested a review from kevsecurity December 4, 2023 11:33
Makezero is a linter to ensure that slices are not initialized with
non-zero length. Since in Go, we most of the time use append on the
slice, initializing it with non-zero length can tend to create a slice
full of zero that will clobber the beginning of the slice.

In the bug fixed here, we created a list with 300+ zeros and then append
after them, only to skip the zero in the calling method. Usually we want
to prealloc the slice with a capacity of len but a length of 0, i.e.
replace make([]int, len(nums)) with make([]int, 0, len(nums)).  See more
info here https://github.com/ashanbrown/makezero.

Signed-off-by: Mahe Tardy <[email protected]>
@mtardy mtardy force-pushed the pr/mtardy/add-makezero-linter branch from c762faa to 3dd23df Compare December 5, 2023 09:53
Copy link

netlify bot commented Dec 5, 2023

Deploy Preview for tetragon ready!

Name Link
🔨 Latest commit 3dd23df
🔍 Latest deploy log https://app.netlify.com/sites/tetragon/deploys/656ef3183517b300084cae84
😎 Deploy Preview https://deploy-preview-1832--tetragon.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@mtardy mtardy requested a review from kevsecurity December 5, 2023 09:53
Copy link
Contributor

@kevsecurity kevsecurity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Like the variable renaming too!

@mtardy mtardy merged commit c7b1295 into main Dec 5, 2023
33 checks passed
@mtardy mtardy deleted the pr/mtardy/add-makezero-linter branch December 5, 2023 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ci Related to CI release-note/misc This PR makes changes that have no direct user impact.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants