Skip to content

Fix panic in format64_to_fixed when rounding carries past all integer digits#56

Merged
HalidOdat merged 1 commit into
boa-dev:mainfrom
Ansh-699:fix/toFixed-rounding-panic
Jul 5, 2026
Merged

Fix panic in format64_to_fixed when rounding carries past all integer digits#56
HalidOdat merged 1 commit into
boa-dev:mainfrom
Ansh-699:fix/toFixed-rounding-panic

Conversation

@Ansh-699

Copy link
Copy Markdown
Contributor

Summary

Fixes #55.

format64_to_fixed panics in debug builds when rounding causes a carry that propagates through all integer digits (e.g. 9.5 with fraction_digits=0).

Root Cause

The rounding loop calls result.get(round_index) before checking round_index == -1, causing an out-of-bounds read when every integer digit is 9 and the carry reaches index -1.

Fix

Move the round_index == -1 boundary check before the result.get() call, and split it from the c == b'-' check into its own branch.

Tests

Added rounding_carry_past_all_digits and rounding_carry_negative tests. All 70 tests pass.

… digits

Check round_index == -1 before calling result.get(round_index) in the
rounding loop, preventing an out-of-bounds read when the carry
propagates through all integer digits (e.g. 9.5 with fraction_digits=0
should produce "10" but instead panics in debug builds).

Fixes boa-dev#55

@HalidOdat HalidOdat left a comment

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.

Looks great to me! Thank you for the contribution!! (and sorry for the delay 😅)

I'll adjust the round_index to be usize type (as well as Cursor) to ensure that this type of issue with negative indices does not happen again, and will release a fix ASAP 😄

@HalidOdat HalidOdat enabled auto-merge July 5, 2026 12:31
@HalidOdat HalidOdat closed this Jul 5, 2026
auto-merge was automatically disabled July 5, 2026 12:32

Pull request was closed

@HalidOdat HalidOdat reopened this Jul 5, 2026
@HalidOdat

Copy link
Copy Markdown
Member

Note: Had to close and re-open to trigger the CI to run 😄

@HalidOdat HalidOdat added this pull request to the merge queue Jul 5, 2026
Merged via the queue into boa-dev:main with commit 4beec89 Jul 5, 2026
14 checks passed
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.

Panic in format64_to_fixed when rounding carries past all integer digits

2 participants