-
Notifications
You must be signed in to change notification settings - Fork 794
fix(nvidia): guard int32 narrowing in GenerateResourceRequests #2719
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
Closed
HESleagacy
wants to merge
1
commit into
Project-HAMi:master
from
HESleagacy:fix/nvidia-int32-narrowing
+148
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
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.
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: Project-HAMi/HAMi
Length of output: 2096
🏁 Script executed:
Repository: Project-HAMi/HAMi
Length of output: 13482
🏁 Script executed:
Repository: Project-HAMi/HAMi
Length of output: 2197
🌐 Web query:
Kubernetes apimachinery v0.36.3 resource.Quantity AsInt64 source fractional negative quantity💡 Result:
In Kubernetes apimachinery v0.36.3, the resource.Quantity.AsInt64 method is designed to return a value as an int64 only if a "fast conversion" is possible [1][2]. Regarding the specific conditions mentioned: 1. Fractional Quantities: If a Quantity is stored as a decimal (which occurs for fractional values), AsInt64 will return 0 and false [1][2]. It does not attempt to truncate or round fractional values to an int64; instead, it indicates that the caller must use the internal decimal representation (inf.Dec) to handle the precision [1][3]. 2. Negative Quantities: AsInt64 handles negative values correctly if they are represented in the int64 form (i.e., not stored as a decimal) [4]. The underlying int64Amount type supports negative values, and AsInt64 will return the correct negative integer and true, provided the value is within the range of an int64 and does not require decimal precision [4]. Key points for developers: - Return value: AsInt64 returns (int64, bool) [1][2]. The boolean return value is critical; a false value indicates that the quantity is currently stored as a decimal (either due to fractional components or because it was promoted to the decimal path) and cannot be accurately represented as a simple int64 [3][2]. - Scaling: If you need to perform calculations on scaled values (e.g., converting a quantity to a specific unit like bytes or GiB), it is recommended to use AsScaledInt64 instead [3][4]. This method provides explicit control over scaling and rounding, which is safer for handling varied quantity formats [4]. - Recent Updates: As of v0.36.3, improvements have been made to the parsing path (e.g., handling values near math.MaxInt64) to ensure that more valid integer-based quantities successfully use the fast path, avoiding unnecessary decimal promotion [5].
Citations:
🏁 Script executed:
Repository: Project-HAMi/HAMi
Length of output: 2197
Reject negative fractional core quantities before
AsInt64.resource.Quantity.AsInt64()returnsok=falsefor-1m. The current code then keepsDefaultCoresand returns a non-empty request. Checkcore.Sign() < 0beforeAsInt64(). Add aresource.MustParse("-1m")case toTest_GenerateResourceRequests_OutOfRangeValues.🧰 Tools
🪛 ast-grep (0.45.1)
[warning] 601-601: Narrowing a non-constant integer to a smaller fixed-width type (int8/int16/int32, uint8/uint16/uint32) can silently overflow or wrap, yielding negative or truncated values that are dangerous in size, length, or index logic. Validate the source value is within the target type's range before converting (e.g. bounds-check, or use a checked helper), and avoid narrowing untrusted or len()/parsed values.
Context: int32(corenums)
Note: [CWE-190] Integer Overflow or Wraparound.
(integer-overflow-narrowing-conversion-go)
🤖 Prompt for AI Agents