Skip to content

#5295: reject out-of-int-range numbers in Expect.Int and Expect.Natural#5307

Merged
yegor256 merged 1 commit into
objectionary:masterfrom
morphqdd:5295-expect-natural-int-range
Jul 8, 2026
Merged

#5295: reject out-of-int-range numbers in Expect.Int and Expect.Natural#5307
yegor256 merged 1 commit into
objectionary:masterfrom
morphqdd:5295-expect-natural-int-range

Conversation

@morphqdd

@morphqdd morphqdd commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Closes #5295.

Expect.Int.it() and Expect.Natural.it() narrow a Double to Integer via Double::intValue
without first checking the value fits in int range. Double.intValue() doesn't throw for a
value outside int range — it saturates, returning Integer.MAX_VALUE/Integer.MIN_VALUE. Since
the saturated value is still a "valid" integer (and, for Natural, still >= 0), no downstream
check catches it, e.g. malloc.of 1000000000000000 silently allocates a 2147483647-byte block
instead of raising a domain error for a size nowhere near what was requested.

Added a must step before the Double::intValue narrowing in both Expect.Int and
Expect.Natural that rejects any value outside [Integer.MIN_VALUE, Integer.MAX_VALUE] with a
"must fit into int range" message, matching the existing style of this validation pipeline
(must be an integer, must be greater or equal to zero).

Added two tests in ExpectTest (failsInTransformingToIntegerForTooLarge,
failsInTransformingToNonNegativeIntegerForTooLarge) mirroring the existing
failsInTransformingTo...ForNotInteger/ForNegative tests, using 1.0e15 as the out-of-range
value from the issue's reproduction.

Ran the full Expect-related test suite locally (ExpectTest, EOmallocAllocatedExpectTest,
EOmallocWritePhiExpectTest, EOiConvertersExpectTest, EObytesEOrightExpectTest,
EOi64ExpectTest) — all 36 tests pass.

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

🚀 Performance Analysis

All benchmarks are within the acceptable range. No critical degradation detected (threshold is 100%). Please refer to the detailed report for more information.

Click to see the detailed report
Test Base Score PR Score Change % Change Unit Mode
benchmarks.XmirBench.xmirToEO 10803.912 10940.517 136.606 1.26% ms/op Average Time

⚠️ Performance loss: benchmarks.XmirBench.xmirToEO is slower by 136.606 ms/op (1.26%)

@morphqdd

morphqdd commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

@yegor256, please take a look when you have a moment.

@morphqdd

morphqdd commented Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

@yegor256, please take a look when you have a moment.

@Test
void failsInTransformingToIntegerForTooLarge() {
MatcherAssert.assertThat(
"inner class Integer throws error for a number outside int range, "

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@morphqdd morphqdd force-pushed the 5295-expect-natural-int-range branch 2 times, most recently from 4a996c2 to 72d3197 Compare July 8, 2026 09:24
@morphqdd morphqdd force-pushed the 5295-expect-natural-int-range branch from 72d3197 to 205cc90 Compare July 8, 2026 14:45
@sonarqubecloud

sonarqubecloud Bot commented Jul 8, 2026

Copy link
Copy Markdown

@yegor256 yegor256 merged commit a4f0ccb into objectionary:master Jul 8, 2026
25 checks passed
@0crat

0crat commented Jul 9, 2026

Copy link
Copy Markdown

@yegor256 Thanks for the review! You've earned +7 points for this: +12 as a basis; -5 for very few (1) comments. Your running score is +1695; don't forget to check your Zerocracy account too).

@0crat

0crat commented Jul 9, 2026

Copy link
Copy Markdown

@morphqdd Thanks for the contribution! You've earned +16 points. Please, keep them coming. Your running score is +640; don't forget to check your Zerocracy account too).

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.

Expect.Natural and Expect.Int wrongly allocate blocks due to Double.intValue() saturation for large numbers

3 participants