Skip to content

Conversation

@joel-rieke
Copy link
Collaborator

Potential fix for https://github.com/trimble-oss/go-mysql-server/security/code-scanning/271

To address the issue, the conversion logic must be updated to include explicit bounds checks before performing the type conversion from float64 to int64. This ensures that only values safely within the range of int64 are processed. If the value falls outside this range or has a fractional part that cannot be accurately represented as an int64, an appropriate error should be returned.

Steps:

  1. Before converting the float64 value to int64, add upper and lower bound checks specific to the int64 type using constants math.MinInt64 and math.MaxInt64.
  2. Ensure that the fractional part is properly handled by rejecting values that cannot be precisely represented as an integer.
  3. Replace the problematic conversion logic on line 94 with a safer implementation.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…etween integer types

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@joel-rieke joel-rieke marked this pull request as ready for review July 15, 2025 18:47
joel-rieke and others added 3 commits July 15, 2025 11:47
…etween integer types

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
if value < 0 || value > math.MaxInt64 { // Additional bounds check
return nil, ErrInvalidSystemVariableValue.New(t.varName, v) // Reject out-of-range values
}
intValue := int64(value)

Check failure

Code scanning / CodeQL

Incorrect conversion between integer types High

Incorrect conversion of an unsigned 64-bit integer from
strconv.ParseUint
to a lower bit size type int64 without an upper bound check.
Incorrect conversion of an unsigned 64-bit integer from
strconv.ParseUint
to a lower bit size type int64 without an upper bound check.
Incorrect conversion of an unsigned 64-bit integer from
strconv.ParseUint
to a lower bit size type int64 without an upper bound check.

Copilot Autofix

AI 5 months ago

The issue can be fixed by explicitly rejecting values that exceed the valid range of int64 when interpreted as signed integers. Additionally, the code can clarify its intent by distinguishing between signed and unsigned conversions, ensuring no ambiguity or misuse occurs.

  1. Update the Convert function to include more explicit checks for float64 to int64 conversion.
  2. Make the boundary checks explicit and ensure they are tailored to the type being converted (int64 in this case).
  3. Return an appropriate error if the value is out of bounds.

The changes will improve code clarity and ensure that boundary conditions are adequately addressed.


Suggested changeset 1
sql/system_settype.go

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/sql/system_settype.go b/sql/system_settype.go
--- a/sql/system_settype.go
+++ b/sql/system_settype.go
@@ -92,10 +92,10 @@
 		// Therefore, if the float doesn't have a fractional portion, we treat it as an int.
 		if value >= float64(math.MinInt64) && value <= float64(math.MaxInt64) {
 			if math.Trunc(value) == value { // Ensure no fractional part exists
-				if value < 0 || value > math.MaxInt64 { // Additional bounds check
+				intValue := int64(value)
+				if intValue < math.MinInt64 || intValue > math.MaxInt64 { // Explicit bounds check
 					return nil, ErrInvalidSystemVariableValue.New(t.varName, v) // Reject out-of-range values
 				}
-				intValue := int64(value)
 				return t.SetType.Convert(intValue)
 			}
 		}
EOF
@@ -92,10 +92,10 @@
// Therefore, if the float doesn't have a fractional portion, we treat it as an int.
if value >= float64(math.MinInt64) && value <= float64(math.MaxInt64) {
if math.Trunc(value) == value { // Ensure no fractional part exists
if value < 0 || value > math.MaxInt64 { // Additional bounds check
intValue := int64(value)
if intValue < math.MinInt64 || intValue > math.MaxInt64 { // Explicit bounds check
return nil, ErrInvalidSystemVariableValue.New(t.varName, v) // Reject out-of-range values
}
intValue := int64(value)
return t.SetType.Convert(intValue)
}
}
Copilot is powered by AI and may make mistakes. Always verify output.
@joel-rieke joel-rieke merged commit 65a127c into main Jul 15, 2025
8 of 12 checks passed
@joel-rieke joel-rieke deleted the alert-autofix-271 branch July 15, 2025 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants