Repository navigation
Fix Bun.Terminal termios flag NaN clamping - #32354
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (2)
WalkthroughIn ChangesNaN clamping fix for termios flags
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
LGTM — one-line clamp-order swap to match the Zig reference, with a regression test.
Extended reasoning...
Overview
This PR swaps a single expression in set_termios_flag (src/runtime/api/bun/Terminal.rs) from num.max(0.0).min(max_val) to num.min(max_val).max(0.0), and adds one regression test in test/js/bun/terminal/terminal.test.ts. The change affects only how NaN is clamped when assigned to inputFlags/outputFlags/localFlags/controlFlags on a Bun.Terminal — previously it resolved to 0, now it resolves to tcflag_t::MAX, matching the Zig reference's @max(0, @min(num, max)) evaluation order.
Security risks
None. This is termios flag clamping on a PTY the caller already owns and controls. The only inputs affected are explicit NaN assignments from user JS; finite values, ±Infinity, and out-of-range values are provably unchanged (min/max commute for non-NaN inputs). No auth, crypto, fd handling, or memory-safety surface is touched.
Level of scrutiny
Low. This is a one-line port-fidelity fix for an extreme edge case (assigning NaN to a termios flag setter). The PR description walks through the exact f64::min/f64::max NaN semantics in both Rust and Zig, and I verified the reasoning: Rust's f64::max/f64::min return the non-NaN operand, so applying min(max_val) first yields max_val for NaN, then max(0.0) keeps it. All other input classes (negative, in-range, above-max, ±inf) produce identical results before and after.
Other factors
The added test is well-constructed: it compares the NaN result against the Infinity result per-field (avoiding hard-coded tcflag_t::MAX and kernel-masked bits), asserts > 0 to catch the regression, and is correctly skipIf(isWindows) since the setter is a no-op on non-unix. No bugs were found by the bug-hunting system, and there are no prior reviewer comments on the PR.
|
Updated 5:01 PM PT - Jun 15th, 2026
✅ @robobun, your commit 2cabb8200f984312b2fe5f96b9c8da761d22a8a2 passed in 🧪 To try this PR locally: bunx bun-pr 32354That installs a local version of the PR into your bun-32354 --bun |
set_termios_flag computed num.max(0.0).min(max_val), which resolves NaN to 0 (all flags cleared) because f64::max returns the non-NaN operand first. The Zig reference evaluates @max(0, @min(num, max_val)), where the inner @min resolves NaN to max_val, so all flags end up set. Swap the order to num.min(max_val).max(0.0) so NaN resolves to max_val, restoring the original behavior. Normal values are unaffected since min and max commute for non-NaN inputs in [0, max_val].
2cabb82 to
b9b9aa8
Compare
There was a problem hiding this comment.
LGTM — one-line min/max reorder so NaN clamps to max_val to match the Zig reference, with a regression test.
Extended reasoning...
Overview
This PR swaps a single expression in set_termios_flag (src/runtime/api/bun/Terminal.rs) from num.max(0.0).min(max_val) to num.min(max_val).max(0.0), adds an explanatory comment, and adds a POSIX-only regression test in test/js/bun/terminal/terminal.test.ts.
Correctness
Rust's f64::min/f64::max propagate the non-NaN operand. I traced every input class:
- NaN: old →
0.0; new →max_val(the only behavioral change, now matching Zig's@max(0, @min(num, max))). - Finite in-range, +∞, −∞, > max_val, < 0: both orderings produce identical results.
So the change affects only the NaN edge case and nothing else. The new test sensibly compares NaN's read-back against Infinity's (both clamp to tcflag_t::MAX) rather than a hard-coded constant, since the kernel may mask bits per field.
Security risks
None. This is numeric clamping of user-supplied termios flag bits before tcsetattr; no auth, crypto, memory safety, or injection surface is touched.
Level of scrutiny
Low. A one-line arithmetic reorder in an obscure edge case (assigning NaN to a termios flag setter), with a clear root-cause analysis, a comment explaining the intent, a targeted regression test, and green CI. The bug-hunting system found nothing.
Other factors
No outstanding reviewer comments, no CODEOWNERS conflicts evident, and the test is properly gated with skipIf(isWindows) since termios is POSIX-only.
Repro
Rust HEAD prints
0; Zig 1.3.14 printstcflag_t::MAX(4294967295on Linux,18446744073709550000on macOS).Cause
set_termios_flaginsrc/runtime/api/bun/Terminal.rsclamped withnum.max(0.0).min(max_val). Rust'sf64::max/f64::minreturn the non-NaN operand, soNaN.max(0.0)yields0.0and all termios bits are cleared.The Zig reference (
Terminal.zig:777) evaluates@max(0, @min(num, max_val)). The inner@min(NaN, max_val)yieldsmax_val, then@max(0, max_val)staysmax_val, so all bits are set.Fix
Swap the order to
num.min(max_val).max(0.0), mirroring the Zig evaluation order. NaN now resolves tomax_val; finite and infinite inputs are unchanged since min and max commute for non-NaN values already inside[0, max_val].Verification
Added a test in
test/js/bun/terminal/terminal.test.tsasserting that assigningNaNto each ofinputFlags/outputFlags/localFlags/controlFlagsreads back the same value as assigningInfinity(both clamp totcflag_t::MAX, modulo kernel-enforced bits). Fails on main (fromNaN: 0vsfromNaN: 2147483647), passes with this change.