ucrt: set ERANGE when a checked float wrapper or ldexpf overflows #52

Merged
Repla09 merged 2 commits from upstream-pr/52 into main 2026-11-26 12:41:21 +00:00
Repla09 commented 2026-11-26 11:31:07 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Repla09 on 2026-11-26T11:31:07Z.


The math module sets ERANGE for an infinity from finite arguments (docs/native-host.md), but the float forms (expf, powf, coshf, hypotf, ldexpf and the rest made by the unary!/binary! macros) checked the double result before rounding it to a float. A float overflow is still finite as a double, so expf(100) and ldexpf(1, 200) returned infinity with errno untouched, while exp(1000) set ERANGE.

Now the result is checked after rounding it to the float it returns.

a_float_that_overflows_sets_erange fails without the fix (errno 0, not 34) and checks five overflowing calls plus a finite one that leaves errno alone.

Testing

  • cargo test --target x86_64-apple-darwin -p victory-hle passes (220 unit, 15 native_probe; the D3D12 probe test skipped, no Metal Shader Converter here).
  • Clippy on x86_64-apple-darwin shows no new warnings; rustfmt --check and cargo xtask policy pass.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6625723318 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52). Originally posted by [Repla09](https://github.com/Repla09) on 2026-11-26T11:31:07Z. --- The math module sets `ERANGE` for an infinity from finite arguments (docs/native-host.md), but the float forms (`expf`, `powf`, `coshf`, `hypotf`, `ldexpf` and the rest made by the `unary!`/`binary!` macros) checked the double result before rounding it to a float. A float overflow is still finite as a double, so `expf(100)` and `ldexpf(1, 200)` returned infinity with `errno` untouched, while `exp(1000)` set `ERANGE`. Now the result is checked after rounding it to the float it returns. `a_float_that_overflows_sets_erange` fails without the fix (`errno` 0, not 34) and checks five overflowing calls plus a finite one that leaves `errno` alone. ## Testing - `cargo test --target x86_64-apple-darwin -p victory-hle` passes (220 unit, 15 native_probe; the D3D12 probe test skipped, no Metal Shader Converter here). - Clippy on `x86_64-apple-darwin` shows no new warnings; `rustfmt --check` and `cargo xtask policy` pass.
LakshanSivakumar commented 2026-11-26 11:42:45 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T11:42:45Z.

Review: CHANGES_REQUESTED


The five overflow cases all reproduce on main and pass here. Two things:

  1. Scope. Please describe the change as the checked float wrappers plus ldexpf, in the title and in the UCRT notes. fdimf, scalbnf and scalblnf still return infinity without setting errno. That predates this PR, so another PR or a mention is fine.
  2. Test. Seed a nonzero errno before the success check and verify it survives. Also include a true infinity from an infinite input, so the test tells a valid infinity apart from an overflow.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6073546518 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52#pullrequestreview-6073546518). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T11:42:45Z. **Review:** CHANGES_REQUESTED --- The five overflow cases all reproduce on main and pass here. Two things: 1. **Scope.** Please describe the change as the checked float wrappers plus `ldexpf`, in the title and in the UCRT notes. `fdimf`, `scalbnf` and `scalblnf` still return infinity without setting `errno`. That predates this PR, so another PR or a mention is fine. 2. **Test.** Seed a nonzero `errno` before the success check and verify it survives. Also include a true infinity from an infinite input, so the test tells a valid infinity apart from an overflow.
Repla09 commented 2026-11-26 11:50:16 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Repla09 on 2026-11-26T11:50:16Z.


Thanks. Retitled to the checked float wrappers plus ldexpf, and the UCRT notes say the same, mentioning that fdimf, scalbnf and scalblnf still set no errno (left for another PR). In 2315032 the test seeds errno = 7 and checks it survives a finite result and infinities from infinite inputs (expf(inf), powf(inf, 2), ldexpf(inf, 1)), so a valid infinity is told apart from an overflow. Rebased onto main.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7191228240 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52#issuecomment-7191228240). Originally posted by [Repla09](https://github.com/Repla09) on 2026-11-26T11:50:16Z. --- Thanks. Retitled to the checked float wrappers plus `ldexpf`, and the UCRT notes say the same, mentioning that `fdimf`, `scalbnf` and `scalblnf` still set no errno (left for another PR). In 2315032 the test seeds errno = 7 and checks it survives a finite result and infinities from infinite inputs (`expf(inf)`, `powf(inf, 2)`, `ldexpf(inf, 1)`), so a valid infinity is told apart from an overflow. Rebased onto main.
LakshanSivakumar commented 2026-11-26 12:01:59 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T12:01:59Z.

Review: DISMISSED


Thanks for addressing the earlier requests; the regression passes under Rosetta and fails against main. Approved.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6073791278 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52#pullrequestreview-6073791278). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T12:01:59Z. **Review:** DISMISSED --- Thanks for addressing the earlier requests; the regression passes under Rosetta and fails against main. Approved.
LakshanSivakumar commented 2026-11-26 12:09:35 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T12:09:35Z.


Approved, but #51 just landed and both edit the UCRT errno paragraph in docs/native-host.md, so this now conflicts. Please rebase on main (or merge main in) and resolve that paragraph; I'll merge once CI is green.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7191641944 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52#issuecomment-7191641944). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T12:09:35Z. --- Approved, but #51 just landed and both edit the UCRT `errno` paragraph in `docs/native-host.md`, so this now conflicts. Please rebase on main (or merge main in) and resolve that paragraph; I'll merge once CI is green.
Repla09 commented 2026-11-26 12:13:17 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Repla09 on 2026-11-26T12:13:17Z.


Rebased onto main after #51 merged: both added a sentence to the same errno line in native-host.md, so the conflict kept both (the remquo guarantee, then the float-wrapper rule). No code change.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7191722178 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52#issuecomment-7191722178). Originally posted by [Repla09](https://github.com/Repla09) on 2026-11-26T12:13:17Z. --- Rebased onto main after #51 merged: both added a sentence to the same `errno` line in native-host.md, so the conflict kept both (the remquo guarantee, then the float-wrapper rule). No code change.
LakshanSivakumar commented 2026-11-26 12:41:08 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T12:41:08Z.

Review: APPROVED


Thanks for the rebase. The docs paragraph now keeps both the remquo and the ERANGE sentences, and the code is unchanged. Approved.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6074288902 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52#pullrequestreview-6074288902). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T12:41:08Z. **Review:** APPROVED --- Thanks for the rebase. The docs paragraph now keeps both the remquo and the ERANGE sentences, and the code is unchanged. Approved.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS!52
No description provided.