Skip to content

fix: don't rely on an out-of-range cast in InRange() - #1734

Open
b84330808 wants to merge 1 commit into
open-source-parsers:masterfrom
b84330808:fix-inrange-out-of-range-cast
Open

b84330808 wants to merge 1 commit into
open-source-parsers:masterfrom
b84330808:fix-inrange-out-of-range-cast

Conversation

@b84330808

Copy link
Copy Markdown

#1519 made InRange() reject a double just over a 64-bit limit by casting it to the integer type and comparing with min. That cast is undefined, and the result depends on the compiler: with clang on x86-64, Value(18446744073709551616.0).asUInt64() returns 2^63 instead of throwing, so #1545 is still there. gcc happens to produce 0, which the check catches.

The same check also catches every value between 0 and 1, since those truncate to the unsigned min. Value(0.5).asUInt() throws and Value(0.5).isConvertibleTo(uintValue) is false, while 1.5 converts to 1.

This compares against the limit instead: strictly when it isn't exactly representable as a double, as isInt64() and isUInt64() already do.

InRange() rejected a double just over a 64-bit limit by casting it to the
integer type and comparing with min. That cast is undefined, and its result
differs by compiler and architecture. With clang on x86-64,
Value(18446744073709551616.0).asUInt64() gave 2^63 instead of throwing, which
is open-source-parsers#1545 again.

The same check also caught every value between 0 and 1, since those truncate
to the unsigned min. Value(0.5).asUInt() threw and
Value(0.5).isConvertibleTo(uintValue) was false, while 1.5 converts to 1.

Compare against the limit instead: strictly when it isn't exactly
representable as a double, as isInt64() and isUInt64() already do.
@greptile-apps

greptile-apps Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Replaces the out-of-range cast in InRange() with a comparison against the upper limit.

  • Real numbers are checked against integer limits before conversion.

No actionable issues found. Tests were not run.

Reviews (1) · Last reviewed commit: "fix: don't rely on an out-of-range cast ..." · Reviewed by Greptile

This branch has not been deployed

No deployments
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.

1 participant