Potential fix for code scanning alert no. 3: Incorrect conversion between integer types#14
Open
Potential fix for code scanning alert no. 3: Incorrect conversion between integer types#14
Conversation
…ween integer types Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Potential fix for https://github.com/storbase/redis-operator/security/code-scanning/3
In general, to fix this kind of issue you either (a) parse the integer directly at the target bit size, or (b) add explicit range checks before converting from a larger to a smaller integer type. In Go, using
strconv.ParseInt/ParseUintwith the correct bit size is preferred tostrconv.Atoiwhen you ultimately need a smaller type.In this codebase,
parseIntFieldis the common helper that currently returnsintviastrconv.Atoi. The cleanest fix is to change it to returnint32and to parse usingstrconv.ParseInt(value, 10, 32). That way, the parsing step itself enforces the 32‑bit range, and all current call sites that immediately cast toint32(like incluster.go) can simply use the returnedint32directly, eliminating the unsafe narrowing cast. This does not meaningfully change behavior: if Redis ever reports a number that does not fit in a 32‑bit signed integer,ParseIntwill fail and we already propagate an error.Concretely:
internal/infra/redis/admin.go, changeparseIntField’s signature fromfunc parseIntField(...)(int, error)to returnint32, replacestrconv.Atoibystrconv.ParseInt(value, 10, 32), and cast the parsedint64toint32on return.internal/infra/redis/cluster.go, update the code buildingClusterObservationto remove the redundantint32(...)casts and use theint32values directly. This addresses the specific flagged sink (int32(slotsAssigned)) and the similar casts forclusterSizeandknownNodes.No new imports are needed:
strconvis already imported where used, and we do not needmathbecause we rely onParseInt’s built-in range checking.Suggested fixes powered by Copilot Autofix. Review carefully before merging.