Skip to content

fix(value): prevent saturation in ToInt8/ToUint8/ToInt16/ToUint16 for… - #5639

Open
Paraspandey-debugs wants to merge 2 commits into
boa-dev:mainfrom
Paraspandey-debugs:fix/small-ints-saturation
Open

Paraspandey-debugs wants to merge 2 commits into
boa-dev:mainfrom
Paraspandey-debugs:fix/small-ints-saturation

Conversation

@Paraspandey-debugs

Copy link
Copy Markdown

Fixes : #5632

Fixed integer conversions for ToInt8, ToUint8, ToInt16, and ToUint16.

Previously, these conversions cast f64 to i64 before applying modulo. Since Rust's f64 as i64 saturates at i64::MAX/i64::MIN, large values (such as 1e20) produced incorrect results.

Now, the conversions reuse f64_to_int32 and f64_to_uint32, which correctly handle truncation and modulo 2^32 including NaN, ±0, and ±∞. The results are then narrowed using as casts to preserve the lower 8 or 16 bits.

Added tests:

  • Unit tests in value/tests.rs.
  • TypedArray and DataView tests in typed_array/tests.rs.
  • Coverage for large values, boundary cases, and non-finite inputs.
Screenshot From 2026-11-05 17-47-26

@Paraspandey-debugs
Paraspandey-debugs requested a review from a team as a code owner November 5, 2026 12:30
@github-actions github-actions Bot added the Waiting On Review Waiting on reviews from the maintainers label Nov 5, 2026
@github-actions github-actions Bot added this to the v0.23 milestone Nov 5, 2026
@github-actions github-actions Bot added C-Tests Issues and PRs related to the tests. C-Builtins PRs and Issues related to builtins/intrinsics labels Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Test262 conformance changes

Test result main count PR count difference
Total 53,578 53,578 0
Passed 51,450 51,450 0
Ignored 1,648 1,648 0
Failed 480 480 0
Panics 0 0 0
Conformance 96.03% 96.03% 0.00%

Tested main commit: bfaa15a7d60d8c92370a423d4134ab19b1b1fae0
Tested PR commit: 229946383684afd67942ef0c7de9d257744ad4a8
Compare commits: bfaa15a...2299463

@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 63.01%. Comparing base (6ddc2b4) to head (2299463).
⚠️ Report is 1086 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5639       +/-   ##
===========================================
+ Coverage   47.24%   63.01%   +15.76%     
===========================================
  Files         476      538       +62     
  Lines       46892    60589    +13697     
===========================================
+ Hits        22154    38179    +16025     
+ Misses      24738    22410     -2328     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@abdulhamid999

Copy link
Copy Markdown

Hi @Paraspandey-debugs, thanks for working on this!

The logic here makes a lot of sense. Reusing f64_to_int32 and f64_to_uint32 directly is much cleaner than the previous manual modulo implementation, and it properly avoids the f64 as i64 saturation issue when handling floats $\ge 2^{63}$.

Because $2^8 \mid 2^{32}$ and $2^{16} \mid 2^{32}$, reducing modulo $2^{32}$ first and then truncating the lower bits via as i8/as u8/as i16/as u16 is mathematically equivalent to the ECMAScript specification for ToInt8, ToUint8, ToInt16, and ToUint16. As an added bonus, this automatically inherits the fast-path instructions (such as FJCVTZS on ARMv8.3) already present in f64_to_int32.

One small suggestion for the tests:
In to_small_ints_large_values, you test large positive values with remainder, like 2f64.powi(53) + 2.0. It might be good to also include a large negative value with non-zero lower bits (e.g. -(2f64.powi(53) + 2.0), which should yield 254 for to_uint8 and -2 for to_int8). That would ensure negative two's complement behavior on large floats is explicitly covered.

Overall, great cleanup and fix!

@Paraspandey-debugs

Copy link
Copy Markdown
Author

Thanks @abdulhamid999 I've updated the tests to cover large values, boundary cases, and non-finite inputs (NaN, +∞, -∞).

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

C-Builtins PRs and Issues related to builtins/intrinsics C-Tests Issues and PRs related to the tests. Waiting On Review Waiting on reviews from the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ToInt8/ToUint8/ToInt16/ToUint16 saturate for |x| ≥ 2^63 (new Uint8Array([1e20]) is 255)

2 participants