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

Manually merged
github merged 2 commits from gh-pr/52/head into gh-pr/52/base 2026-11-26 14:44:04 +00:00
Owner

@⁠Repla09 on GitHub · 2026-11-26 11:31 UTC · original
Repla09:fix/ucrt-float-overflow-errno → main · +29 −4 · 2 files · 2 commits · merged 2026-11-26 12:41 UTC

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.
**@⁠Repla09** on GitHub · 2026-11-26 11:31 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52) `Repla09:fix/ucrt-float-overflow-errno` → `main` · +29 −4 · 2 files · 2 commits · merged 2026-11-26 12:41 UTC 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.
The module sets ERANGE for an infinity from finite arguments, but the
float forms (expf, powf, coshf, hypotf, ldexpf and the rest made by the
unary and binary macros) checked the double result before rounding it
to a float. A float overflow is finite as a double, so expf(100) and
ldexpf(1, 200) returned infinity with errno untouched, while exp(1000)
set ERANGE.

Check the result after rounding it to the float it returns.
The test seeds a nonzero errno and checks that a finite result and an
infinity from an infinite argument leave it alone, so it tells a valid
infinity from an overflow. native-host.md scopes the rule to the
checked float wrappers and ldexpf; fdimf, scalbnf and scalblnf still
set no errno.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:44:04 +00:00
github manually merged commit b14697e521 into gh-pr/52/base 2026-11-26 14:44:04 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 11:42 UTC · original · requested changes

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.
**@⁠LakshanSivakumar** on GitHub · 2026-11-26 11:42 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52#pullrequestreview-6073546518) · **requested changes** 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.
Author
Owner

@⁠Repla09 on GitHub · 2026-11-26 11:50 UTC · original

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.

**@⁠Repla09** on GitHub · 2026-11-26 11:50 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52#issuecomment-7191228240) 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.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 12:01 UTC · original · review (dismissed)

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

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 12:01 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52#pullrequestreview-6073791278) · **review (dismissed)** Thanks for addressing the earlier requests; the regression passes under Rosetta and fails against main. Approved.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 12:09 UTC · original

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.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 12:09 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52#issuecomment-7191641944) 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.
Author
Owner

@⁠Repla09 on GitHub · 2026-11-26 12:13 UTC · original

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.

**@⁠Repla09** on GitHub · 2026-11-26 12:13 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52#issuecomment-7191722178) 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.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 12:41 UTC · original · approved these changes

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

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 12:41 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/52#pullrequestreview-6074288902) · **approved these changes** Thanks for the rebase. The docs paragraph now keeps both the remquo and the ERANGE sentences, and the code is unchanged. Approved.
This discussion has been locked. Commenting is limited to contributors.
No description provided.