-
Notifications
You must be signed in to change notification settings - Fork 424
[code-scanning-fix] Fix go/incorrect-integer-conversion: unsafe int64 to int conversion (CWE-190) #40599
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
[code-scanning-fix] Fix go/incorrect-integer-conversion: unsafe int64 to int conversion (CWE-190) #40599
Changes from all commits
848d237
171bafa
6447928
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,7 +53,7 @@ const ( | |
| // otherwise returns the parsed override as a string. | ||
| func ResolveDefaultMaxTurns(fallback string) string { | ||
| if parsed, ok := parsePositiveIntEnvVar(DefaultMaxTurns); ok { | ||
| return strconv.FormatInt(parsed, 10) | ||
| return strconv.Itoa(parsed) | ||
| } | ||
| return fallback | ||
| } | ||
|
|
@@ -62,7 +62,7 @@ func ResolveDefaultMaxTurns(fallback string) string { | |
| // otherwise returns the parsed override. | ||
| func ResolveDefaultTimeoutMinutes(fallback int) int { | ||
| if parsed, ok := parsePositiveIntEnvVar(DefaultTimeoutMinutes); ok { | ||
| return int(parsed) | ||
| return parsed | ||
| } | ||
| return fallback | ||
| } | ||
|
|
@@ -71,7 +71,7 @@ func ResolveDefaultTimeoutMinutes(fallback int) int { | |
| // otherwise returns the parsed override. | ||
| func ResolveDefaultMaxTurnCacheMisses(fallback int) int { | ||
| if parsed, ok := parsePositiveIntEnvVar(DefaultMaxTurnCacheMisses); ok { | ||
| return int(parsed) | ||
| return parsed | ||
| } | ||
| return fallback | ||
| } | ||
|
|
@@ -98,15 +98,15 @@ func ResolveDefaultUTC(fallback string) string { | |
| return raw | ||
| } | ||
|
|
||
| // parsePositiveIntEnvVar parses an environment variable as a base-10 positive int64. | ||
| // parsePositiveIntEnvVar parses an environment variable as a base-10 positive int. | ||
| // It returns (value, true) when the variable is set to a valid value > 0. | ||
| // For unset, empty, non-numeric, or non-positive values, it returns (0, false). | ||
| func parsePositiveIntEnvVar(name string) (int64, bool) { | ||
| func parsePositiveIntEnvVar(name string) (int, bool) { | ||
| raw := strings.TrimSpace(os.Getenv(name)) | ||
| if raw == "" { | ||
| return 0, false | ||
| } | ||
| parsed, err := strconv.ParseInt(raw, 10, 64) | ||
| parsed, err := strconv.Atoi(raw) | ||
| if err != nil || parsed <= 0 { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] Negative integer inputs (e.g. 💡 Suggested test caset.Run("negative value uses fallback", func(t *testing.T) {
t.Setenv(DefaultTimeoutMinutes, "-1")
assert.Equal(t, 20, ResolveDefaultTimeoutMinutes(20))
})Adding this makes the test suite serve as a complete specification: it documents that non-positive values (zero and negative) are all treated as absent. |
||
| return 0, false | ||
|
Comment on lines
+109
to
111
|
||
| } | ||
|
|
||
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.
[/diagnose] The CWE-190 fix is correct, but there is no regression test exercising the overflow boundary that the fix specifically addresses.
💡 Suggested regression test
On a 32-bit platform, the old code silently wrapped any value >
math.MaxInt32(e.g."2147483648"). The newstrconv.Atoicorrectly rejects it. On a 64-bit CI machine the boundary lies atmath.MaxInt, so you can exercise rejection portably with a value that exceedsmath.MaxInt64:This locks in the behavioral change and prevents silent regression back to an unchecked cast.