Skip to content

Fix Bun.Terminal termios flag NaN clamping - #32354

Merged
dylan-conway merged 1 commit into
mainfrom
farm/5b067376/terminal-termios-nan-clamp
Jun 16, 2026
Merged

dylan-conway merged 1 commit into
mainfrom
farm/5b067376/terminal-termios-nan-clamp

Conversation

@robobun

@robobun robobun commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Repro

const t = new Bun.Terminal({ cols: 80, rows: 24 });
t.localFlags = NaN;
console.log(t.localFlags);
t.close();

Rust HEAD prints 0; Zig 1.3.14 prints tcflag_t::MAX (4294967295 on Linux, 18446744073709550000 on macOS).

Cause

set_termios_flag in src/runtime/api/bun/Terminal.rs clamped with num.max(0.0).min(max_val). Rust's f64::max/f64::min return the non-NaN operand, so NaN.max(0.0) yields 0.0 and all termios bits are cleared.

The Zig reference (Terminal.zig:777) evaluates @max(0, @min(num, max_val)). The inner @min(NaN, max_val) yields max_val, then @max(0, max_val) stays max_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 to max_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.ts asserting that assigning NaN to each of inputFlags/outputFlags/localFlags/controlFlags reads back the same value as assigning Infinity (both clamp to tcflag_t::MAX, modulo kernel-enforced bits). Fails on main (fromNaN: 0 vs fromNaN: 2147483647), passes with this change.

@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: a089c776-cb89-4d33-90c1-0542e75b2310

📥 Commits

Reviewing files that changed from the base of the PR and between 1069902 and b9b9aa8.

📒 Files selected for processing (2)
  • src/runtime/api/bun/Terminal.rs
  • test/js/bun/terminal/terminal.test.ts

Walkthrough

In Terminal.rs, the POSIX set_termios_flag helper swaps the order of min/max calls so that NaN inputs clamp to max_val instead of 0. A new POSIX-only test validates that setting each termios flag property to NaN produces the same read-back value as setting it to Infinity.

Changes

NaN clamping fix for termios flags

Layer / File(s) Summary
Termios flag NaN clamp fix and test
src/runtime/api/bun/Terminal.rs, test/js/bun/terminal/terminal.test.ts
set_termios_flag now evaluates num.min(max_val).max(0.0) instead of num.max(0.0).min(max_val), making NaN resolve to max_val. The accompanying test assigns Infinity then NaN to each of inputFlags, outputFlags, localFlags, and controlFlags, asserting both produce the same positive clamped value.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and specifically describes the main change: fixing NaN clamping behavior for Bun.Terminal termios flags.
Description check ✅ Passed The description includes comprehensive sections covering reproduction, root cause analysis, the fix, and verification, exceeding the basic template requirements.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@robobun

robobun commented Jun 15, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 5:01 PM PT - Jun 15th, 2026

✅ @robobun, your commit 2cabb8200f984312b2fe5f96b9c8da761d22a8a2 passed in Build #62660! 🎉


🧪   To try this PR locally:

bunx bun-pr 32354

That installs a local version of the PR into your bun-32354 executable, so you can run:

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].
@robobun
robobun force-pushed the farm/5b067376/terminal-termios-nan-clamp branch from 2cabb82 to b9b9aa8 Compare June 16, 2026 18:24
@dylan-conway
dylan-conway merged commit 49269d3 into main Jun 16, 2026
5 of 6 checks passed
@dylan-conway
dylan-conway deleted the farm/5b067376/terminal-termios-nan-clamp branch June 16, 2026 18:27

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants