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

chore: Enable gosec G103 #10474

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

chore: Enable gosec G103 #10474

wants to merge 5 commits into from

Conversation

alexweav
Copy link
Contributor

@alexweav alexweav commented Jan 17, 2025

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

  • Tests updated.
  • Documentation added.
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX].
  • about-versioning.md updated with experimental features.

@alexweav alexweav marked this pull request as ready for review January 17, 2025 18:12
@alexweav alexweav requested a review from a team as a code owner January 17, 2025 18:12

import "unsafe"

func YoloBuf(s string) []byte {
Copy link
Contributor Author

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.

Comment on lines +159 to +160
underlying := unsafe.Pointer(unsafe.SliceData(postings)) //nolint:gosec
return unsafe.Slice((*byte)(underlying), len(postings)*bytesPerPosting) //nolint:gosec
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant