Skip to content

fix(arithmetic): bound nested dollar expansion - #2668

Merged
chaliy merged 3 commits into
mainfrom
codex/arithmetic-expansion-remediation
Oct 10, 2026
Merged

chaliy merged 3 commits into
mainfrom
codex/arithmetic-expansion-remediation

Conversation

@chaliy

@chaliy chaliy commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

What changed

Nested arithmetic dollar expansion ($((...)) inside $((...))) no longer escapes the shared evaluation budget. Each nested expansion now charges fuel and borrows the parent budget instead of starting fresh, so hostile nesting aborts with a bounded error instead of exhausting the stack.

Why

Finding arter-y7s8: textual expansion ran before the shared depth/fuel checks, and every recursive $((...)) got a brand-new budget, so the 8-deep / 1024-byte limits never engaged across nesting levels.

Before / After

Sensor (echo \$((1+\ x 5000 + 1\ + ))\ x 5000`):

  • Before: never returns (stack exhaustion, unbounded diagnostics).
  • After: exits 1 in ~2.7s, 327-byte stderr ending in expression recursion level exceeded.

Tests: 7 regression tests in arithmetic_resource_tests.rs (recursion aborts, valid nesting keeps working, bash parity within limits); 40/40 arithmetic integration tests pass. cargo fmt, clippy --all-targets, cargo vet, capability/OKF/doc-link/workflow/changelog checks all green.

Note (macOS-local only, unrelated paths, Linux CI is the gate): blackbox depth_50 cmd-subst test needs a bigger thread stack locally (passes with RUST_MIN_STACK=16777216); scripts/tests python suite needs Python 3.11+ (local python is 3.9).

Risk

  • Low: only the arithmetic evaluation path; valid expressions (nesting <= 8, subscripts, namerefs) verified unchanged against real bash.
  • What can break: scripts relying on >8-deep arithmetic nesting now get an error instead of a hang.

Checklist

  • Tests added or updated
  • Backward compatibility considered (internal code, no compat needed)

Produced by yolop

Negative indices need bash 4.3+; macOS ships bash 3.2 while Linux CI
has bash 5.x, so ${a[-1]} makes the assertion environment-dependent.
${a[i-0]} still exercises arithmetic inside the subscript everywhere.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
bashkit 7e87c12 Commit Preview URL

Branch Preview URL
Oct 10 2026, 10:44 PM

@chaliy
chaliy merged commit be8ec33 into main Oct 10, 2026
49 checks passed
@chaliy
chaliy deleted the codex/arithmetic-expansion-remediation branch October 10, 2026 23:13
chaliy added a commit that referenced this pull request Oct 10, 2026
## What changed

Nested arithmetic dollar expansion (`$((...))` inside `$((...))`) no
longer escapes the shared evaluation budget. Each nested expansion now
charges fuel and borrows the parent budget instead of starting fresh, so
hostile nesting aborts with a bounded error instead of exhausting the
stack.

## Why

Finding arter-y7s8: textual expansion ran before the shared depth/fuel
checks, and every recursive `$((...))` got a brand-new budget, so the
8-deep / 1024-byte limits never engaged across nesting levels.

## Before / After

Sensor (`echo \$((1+\` x 5000 + `1\` + `))\` x 5000`):

- Before: never returns (stack exhaustion, unbounded diagnostics).
- After: exits 1 in ~2.7s, 327-byte stderr ending in `expression
recursion level exceeded`.

Tests: 7 regression tests in `arithmetic_resource_tests.rs` (recursion
aborts, valid nesting keeps working, bash parity within limits); 40/40
arithmetic integration tests pass. `cargo fmt`, `clippy --all-targets`,
`cargo vet`, capability/OKF/doc-link/workflow/changelog checks all
green.

Note (macOS-local only, unrelated paths, Linux CI is the gate):
`blackbox depth_50 cmd-subst` test needs a bigger thread stack locally
(passes with `RUST_MIN_STACK=16777216`); `scripts/tests` python suite
needs Python 3.11+ (local python is 3.9).

## Risk

- Low: only the arithmetic evaluation path; valid expressions (nesting
<= 8, subscripts, namerefs) verified unchanged against real bash.
- What can break: scripts relying on >8-deep arithmetic nesting now get
an error instead of a hang.

## Checklist

- [x] Tests added or updated
- [x] Backward compatibility considered (internal code, no compat
needed)

Produced by [yolop](https://everruns.com/yolop)
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.

1 participant