Image analyzer#337
Conversation
| // Prevent overflow and unreasonable retry counts. | ||
| retries = 10 | ||
| } | ||
| backoffLimit := int32(retries) |
Check failure
Code scanning / CodeQL
Incorrect conversion between integer types High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 1 month ago
General approach: Avoid converting from an unbounded, architecture-dependent int that comes from strconv.Atoi into a smaller fixed-width integer type without a clear, type-appropriate bound. Either (1) keep the value as int32 all the way from parsing to use, or (2) add explicit bounds based on the target type. In this code, backoffLimit is already logically constrained to [1, 10], so the safest, minimal-change fix is to ensure the configuration field used here is itself a small, fixed-width type (e.g., int32) and to avoid any further narrowing conversion in buildJobObject.
Best concrete fix with minimal behavior change:
- In
buildJobObject, instead of computingbackoffLimitasint32(retries)from anint, derive it directly as anint32value and keep all intermediate variables inint32. Because we can’t change types in theImageAnalysisConfigstruct here (its definition isn’t shown) and can only modify shown snippets, the simplest local change is:- Change the temporary
retriesvariable frominttoint32. - Cast the config value once, with an explicit bound check that also satisfies CodeQL’s requirement for constant bounds, before any further logic.
- Then operate purely on
int32values, and assign directly tobackoffLimitas anint32without an additional narrowing cast.
- Change the temporary
Concretely in internal/controller/image_job_manager.go inside buildJobObject:
- Introduce a temporary
int32with safe clamping from the possibly largeintinm.config.MaxRetries. Use explicit constants within theint32range (e.g.,0,10) for clarity. - Use that
int32variable directly asbackoffLimitwithout another cast.
Since the code already enforces 1 <= retries <= 10, we preserve the same logic, but we cast once after a constant-bound check, making it clear there’s no risk of exceeding int32 limits.
No new imports or helper methods are necessary; we can rely solely on basic language constructs.
| @@ -319,14 +319,15 @@ | ||
| activeDeadlineSeconds := int64(m.config.JobTimeoutMinutes * 60) | ||
|
|
||
| // backoffLimit from config. | ||
| retries := m.config.MaxRetries | ||
| if retries <= 0 { | ||
| retries = 1 | ||
| } else if retries > 10 { | ||
| rawRetries := m.config.MaxRetries | ||
| // Clamp to a safe range before converting to int32. | ||
| if rawRetries <= 0 { | ||
| rawRetries = 1 | ||
| } else if rawRetries > 10 { | ||
| // Prevent overflow and unreasonable retry counts. | ||
| retries = 10 | ||
| rawRetries = 10 | ||
| } | ||
| backoffLimit := int32(retries) | ||
| backoffLimit := int32(rawRetries) | ||
|
|
||
| ttl := int32(ttlSecondsAfterFinished) | ||
|
|
[Title]
📚 Description of Changes
Provide an overview of your changes and why they’re needed. Link to any related issues (e.g., "Fixes #123"). If your PR fixes a bug, resolves a feature request, or updates documentation, please explain how.
What Changed:
(Describe the modifications, additions, or removals.)
Why This Change:
(Explain the problem this PR addresses or the improvement it provides.)
Affected Components:
(Which component does this change affect? - put x for all components)
Compose
K8s
Other (please specify)
❓ Motivation and Context
Why is this change required? What problem does it solve?
Context:
(Provide background information or link to related discussions/issues.)
Relevant Tasks/Issues:
(e.g., Fixes: #GitHub Issue)
🔍 Types of Changes
Indicate which type of changes your code introduces (check all that apply):
🔬 QA / Verification Steps
Describe the steps a reviewer should take to verify your changes:
make testto verify all tests pass.")make create-kind && make deploy.")✅ Global Checklist
Please check all boxes that apply:
Summary by Gitar
ImageAnalysisResultCRD for storing dive analysis metrics, layer breakdown, and workload referencesThis will update automatically on new commits.