-
Notifications
You must be signed in to change notification settings - Fork 542
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
chore: Enable gosec G103 #10474
base: main
Are you sure you want to change the base?
chore: Enable gosec G103 #10474
Conversation
|
||
import "unsafe" | ||
|
||
func YoloBuf(s string) []byte { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decided against exporting this, as it still passes on certain usage requirements to its callers and yet the linter doesn't know to look for call sites. Exporting something that directly wraps a linter check felt against the spirit of the lint rule.
underlying := unsafe.Pointer(unsafe.SliceData(postings)) //nolint:gosec | ||
return unsafe.Slice((*byte)(underlying), len(postings)*bytesPerPosting) //nolint:gosec |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modernized and avoiding deprecated types. This has an identical allocation profile to the original:
└──>go test ./pkg/storegateway/indexcache/... -bench BenchmarkCanonicalPostingsKey -run ^$ -benchmem
Before change:
BenchmarkCanonicalPostingsKey/10_postings-16 5908748 200.3 ns/op 96 B/op 2 allocs/op
BenchmarkCanonicalPostingsKey/100_postings-16 1346832 886.8 ns/op 96 B/op 2 allocs/op
BenchmarkCanonicalPostingsKey/1000_postings-16 163398 7304 ns/op 96 B/op 2 allocs/op
BenchmarkCanonicalPostingsKey/10000_postings-16 16560 72348 ns/op 96 B/op 2 allocs/op
BenchmarkCanonicalPostingsKey/100000_postings-16 1670 716915 ns/op 96 B/op 2 allocs/op
BenchmarkCanonicalPostingsKey/1000000_postings-16 163 7155922 ns/op 96 B/op 2 allocs/op
After change:
BenchmarkCanonicalPostingsKey/10_postings-16 6104720 193.4 ns/op 96 B/op 2 allocs/op
BenchmarkCanonicalPostingsKey/100_postings-16 1358037 882.1 ns/op 96 B/op 2 allocs/op
BenchmarkCanonicalPostingsKey/1000_postings-16 163988 7292 ns/op 96 B/op 2 allocs/op
BenchmarkCanonicalPostingsKey/10000_postings-16 16810 71467 ns/op 96 B/op 2 allocs/op
BenchmarkCanonicalPostingsKey/100000_postings-16 1678 713610 ns/op 96 B/op 2 allocs/op
BenchmarkCanonicalPostingsKey/1000000_postings-16 166 7138759 ns/op 96 B/op 2 allocs/op
What this PR does
Enables gosec G103, which checks for usages of unsafe.
Audited existing usages. In some cases, modernized memory-unsafe code to the newer go 1.20/1.22 mechanisms.
Which issue(s) this PR fixes or relates to
n/a
Checklist
n/a
CHANGELOG.md
updated - the order of entries should be[CHANGE]
,[FEATURE]
,[ENHANCEMENT]
,[BUGFIX]
.about-versioning.md
updated with experimental features.