fix(NSC): check error from rand.Int before using result in shuffle#2020
fix(NSC): check error from rand.Int before using result in shuffle#2020Aprazor wants to merge 1 commit intocloudnativelabs:masterfrom
Conversation
rand.Int returns (nil, err) on failure, but the return value was dereferenced on the next line before the error check. This causes a nil pointer panic if rand.Int ever fails. Move the error check before the dereference and continue the loop on error.
|
Hi @Aprazor! Thanks for your interest in kube-router and for looking into some of these data race, contention, and Hopefully we'll have a chance to review them in the next week or two. In the meantime, it would be super helpful if Here's a suggestion for how you could reorganize these into 3 PRs: PR 1: This would combine #2037, #2036, #2023, #2021, and #2020 — all of which are bug fixes in PR 2: This would just be #2038 as-is. It's the only change in the routing controller ( PR 3: This would combine #2039 and #2022 — the sanitization of iptables comment strings in the network policy controller One other thing — please make sure the consolidated PRs include unit tests for the new code. Most of the current PRs don't add any test coverage. Our Developer's Guide covers the testing expectations, and since you Thanks again for the contributions — looking forward to reviewing once they're consolidated! |
What type of PR is this?
bug
What this PR does / why we need it:
In
shuffle()(pkg/controllers/proxy/network_services_controller.go), the return value ofrand.Int()is dereferenced on the line before the error check. Ifrand.Intreturns an error, it returns(nil, err), andrandBitInt.Int64()panics with a nil pointer dereference.The fix moves the error check before the dereference and uses
continueto skip the swap on error.Which issue(s) this PR is related to:
None found.
Was AI used during the creation of this PR?
What, if any, amount of integration testing was done with this change in a Kubernetes environment?
Unit tests pass. No integration testing — the fix is a simple reordering of existing lines.
Does this PR introduce a breaking change?
Anything else the reviewer should know that wasn't already covered?
The bug is latent —
crypto/rand.Readerrarely fails, but when it does (e.g., entropy exhaustion), this would crash the controller.