Fix precision loss in compareDoubleInt/compareDoubleUint for values outside float64 safe integer range#1328
Open
XananasX7 wants to merge 2 commits into
Open
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fix precision loss in
compareDoubleIntandcompareDoubleUintwhen comparingdoublewithint/uintvalues outside the safe float64 integer range (> 2^53).Problem
The current implementation casts
Int(int64) orUint(uint64) tofloat64before comparison:IEEE 754
float64has a 53-bit mantissa. Any integer with absolute value greater than2^53 = 9007199254740992cannot be represented exactly asfloat64. This means two distinctint64values can map to the samefloat64, causing the comparison to incorrectly return equal.Demonstrated bug
Other affected values:
int(100000000000000001) == double(1e17)→true(should befalse)int(1000000000000000001) == double(1e18)→true(should befalse)Impact
CEL is used as the expression engine in GCP IAM Conditions, Firebase Security Rules, and Kubernetes admission webhooks. A policy rule that compares an integer field to a double literal near these boundary values can produce incorrect authorization decisions.
Fix
Use
math/big.Floatfor exact comparison, eliminating the int→float64 precision loss:The same fix is applied to
compareDoubleUint.The fast-path bounds checks (
< MinInt64,> MaxInt64) remain unchanged and avoid thebig.Floatallocation for values clearly out of range. Thebig.Floatpath is only reached for values in the representable int64 range where precision matters.Testing
All existing comparison tests continue to pass. The previously incorrect cases now return the correct result: