Skip to content

fix: clamp ceilToInt64 to prevent float-to-int truncation overflow#7819

Open
Fedosin wants to merge 1 commit into
kedacore:mainfrom
Fedosin:fix/ceiltoint64-overflow-7796
Open

fix: clamp ceilToInt64 to prevent float-to-int truncation overflow#7819
Fedosin wants to merge 1 commit into
kedacore:mainfrom
Fedosin:fix/ceiltoint64-overflow-7796

Conversation

@Fedosin

@Fedosin Fedosin commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

ceilToInt64 in pkg/scaling/scaledjob/metrics.go silently wraps to math.MinInt64 instead of returning math.MaxInt64 when the input float64 is >= 2^63 (≈ 9.22 × 10^18). The conversion int64(math.Ceil(x)) compiles to a CVTTSD2SI instruction on x86-64; IEEE 754 specifies that this instruction returns the "integer indefinite" value (0x8000000000000000 = math.MinInt64) for any out-of-range input — there is no runtime panic and no error, just a silently wrong result.

This adds overflow guards that clamp to math.MaxInt64 / math.MinInt64 before the float-to-int conversion, along with regression tests covering normal values, boundary values (2^63 exactly), large positive/negative values, and ±Inf.

Checklist

  • When introducing a new scaler, I agree with the scaling governance policy
  • I have verified that my change is according to the deprecations & breaking changes policy
  • Tests have been added (if applicable)
  • Ensure make generate-scalers-schema has been run to update any outdated generated files
  • Changelog has been updated and is aligned with our changelog requirements, only when the change impacts end users
  • A PR is opened to update our Helm chart (repo) (if applicable, ie. when deployment manifests are modified)
  • A PR is opened to update the documentation on (repo) (if applicable)
  • Commits are signed with Developer Certificate of Origin (DCO - learn more)

Fixes #7796

@Fedosin Fedosin requested a review from a team as a code owner June 2, 2026 10:56
@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown

Thank you for your contribution! 🙏

Please understand that we will do our best to review your PR and give you feedback as soon as possible, but please bear with us if it takes a little longer as expected.

While you are waiting, make sure to:

  • Add an entry in our changelog in alphabetical order and link related issue
  • Update the documentation, if needed
  • Add unit & e2e tests for your changes
  • GitHub checks are passing
  • Is the DCO check failing? Here is how you can fix DCO issues

Once the initial tests are successful, a KEDA member will ensure that the e2e tests are run. Once the e2e tests have been successfully completed, the PR may be merged at a later date. Please be patient.

Learn more about our contribution guide.

@keda-automation keda-automation requested a review from a team June 2, 2026 10:56
@snyk-io

snyk-io Bot commented Jun 2, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@rickbrouwer rickbrouwer added the merge-conflict This PR has a merge conflict label Jun 4, 2026
ceilToInt64 silently wraps to math.MinInt64 when the input float64 is
>= 2^63 due to IEEE 754 CVTTSD2SI returning "integer indefinite" for
out-of-range values. This can cause unintended scale-down to zero for
ScaledJobs when a scaler returns an extremely large metric value.

Add overflow guards that clamp to math.MaxInt64 / math.MinInt64 before
the float-to-int conversion, and add regression tests covering boundary
values and infinities.

Fixes kedacore#7796

Signed-off-by: Mikhail Fedosin <mfedosin@redhat.com>
@Fedosin Fedosin force-pushed the fix/ceiltoint64-overflow-7796 branch from c820fd2 to 187c45a Compare June 9, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-conflict This PR has a merge conflict

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: ceilToInt64 Float-to-Int Truncation Overflow

2 participants