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

fix: golangci-lint warnings about unsafe integer conversions #882

Conversation

CarlosNihelton
Copy link
Contributor

@CarlosNihelton CarlosNihelton commented Aug 26, 2024

Most of the warnings were not a real issue, but the one around the Win32 GetAdapterAddersses was harder to prove with the previous implementation so I refactored it a little bit to make it easy to reach the piece of mind that we're not doing nasting error-prone narrowing conversions.

This should unblock #877 .

UDENG-4234

gosec 115 has been promoted into golangci-lint v1.60.2.

That's about integer conversion. We're doing nasting conversions there:
uintptr -> int (we're 64-bit, but still bad for portable type-safe code)
uintptr -> uint32 (we relied on domain knowledge the compiler cannot
ensure).
This reimplements the low-level byte-counting to:
- Merge two functions into one, reducing the risk of mistakes
- Doing all the math with uint64 and
- Finally validate that the amount we'd allocate is still <= MaxUint32.

This way we don't surface the int conversions, but rather keep them
isolated into a single function.
Copy link

codecov bot commented Aug 26, 2024

Codecov Report

Attention: Patch coverage is 94.23077% with 3 lines in your changes missing coverage. Please review.

Project coverage is 88.87%. Comparing base (c7e3986) to head (644c657).
Report is 14 commits behind head on dependabot/go_modules/tools/github.com/golangci/golangci-lint-1.60.2.

Files Patch % Lines
windows-agent/internal/daemon/networking.go 86.36% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@                                           Coverage Diff                                            @@
##           dependabot/go_modules/tools/github.com/golangci/golangci-lint-1.60.2     #882      +/-   ##
========================================================================================================
+ Coverage                                                                 88.61%   88.87%   +0.25%     
========================================================================================================
  Files                                                                       105      105              
  Lines                                                                      6806     6821      +15     
========================================================================================================
+ Hits                                                                       6031     6062      +31     
+ Misses                                                                      600      584      -16     
  Partials                                                                    175      175              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

With one way that demonstrates we don't overflow uint32
It's too much close to the real system APIs :)
To prevent false warnings of narrow conversion from uintptr.
StoreApiError aliased to int64 instead of int
@CarlosNihelton CarlosNihelton force-pushed the fix-ptr-int-conv branch 3 times, most recently from e0efdbd to 174bee4 Compare August 26, 2024 20:13
There is some non-negligible error translation in there
mostly due the somewhat involving error handling imposed by
syscall/dll_windows.
This adds a modest set of test cases to cover that logic.
For that to be approachable I extracted the error reporting
out of the proc call method, this way we don't depend on calling a DLL
function to test this part.
Quite artificial setup, but I always enjoy separating "computing" from
"doing".
@CarlosNihelton CarlosNihelton marked this pull request as ready for review August 26, 2024 20:49
@CarlosNihelton CarlosNihelton requested a review from a team as a code owner August 26, 2024 20:49
Copy link
Collaborator

@didrocks didrocks left a comment

Choose a reason for hiding this comment

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

Looks great! I’m trusting you on the Win32 API returned values.

However, I have a couple of nitpicks and one question/remark on the growing size for the adapter address logic!

storeapi/go-wrapper/microsoftstore/store.go Outdated Show resolved Hide resolved
storeapi/go-wrapper/microsoftstore/store.go Show resolved Hide resolved
storeapi/go-wrapper/microsoftstore/store_test.go Outdated Show resolved Hide resolved
storeapi/go-wrapper/microsoftstore/store_test.go Outdated Show resolved Hide resolved
windows-agent/internal/daemon/networking.go Show resolved Hide resolved
@CarlosNihelton CarlosNihelton self-assigned this Aug 27, 2024
Copy link
Collaborator

@didrocks didrocks left a comment

Choose a reason for hiding this comment

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

Thanks for addressing those!

@CarlosNihelton CarlosNihelton merged commit 6afb8c3 into dependabot/go_modules/tools/github.com/golangci/golangci-lint-1.60.2 Aug 28, 2024
35 checks passed
@CarlosNihelton CarlosNihelton deleted the fix-ptr-int-conv branch August 28, 2024 13:39
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.

2 participants