Skip to content

expand: cap brace expansion words and fix sequence overflow - #855

Merged
ewhauser merged 2 commits into
mainfrom
claude/wizardly-sutherland-10a50e
Oct 19, 2026
Merged

ewhauser merged 2 commits into
mainfrom
claude/wizardly-sutherland-10a50e

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Summary

Brace expansion had no size limit. A single script line could exhaust host memory, and none of the existing policy limits stopped it:

$ echo {1..30000000} | wc -c     # before: ~16 GB RSS

Ranges that end near the int bounds also never finished, because n += incr wrapped around to MinInt:

$ echo {9223372036854775806..9223372036854775807}   # before: infinite loop

Changes

  • New limit policy.Limits.MaxBraceExpansionWords, default 100000. It is wired through the runtime defaults, WithLimitOverrides / LimitOverrides, the conformance runner, and fuzztest (2000). The count covers a whole command: every argv word, array literal element, and declaration operand together. So echo {1..60000} {1..60000} cannot get around it by splitting the range across words. The count resets for each command, so loops that reuse a moderate range are not affected.
  • shell/expand: adds Config.MaxBraceExpansionWords, BraceExpansionLimitError, and Config.BeginBraceExpansionScope / EndBraceExpansionScope. Outside a scope the cap applies to each Fields call. The interpreter opens a scope around argv, decl and array expansion, because it expands those one word at a time.
  • Error behavior: the shell prints brace expansion: too many words (limit N), increase policy.Limits.MaxBraceExpansionWords and exits the current shell with status 126, the same status the other budgets use. A ( ... ) subshell or $(...) returns 126 to its parent. Interactive mode aborts only that command.
  • Overflow fix: the sequence loop now compares the remaining distance as an unsigned value before stepping. Ranges and large steps at the int bounds terminate, and their output matches bash 5.3.
  • limitOverridePolicy now uses pointer receivers, because the larger Limits struct tripped gocritic's hugeParam check.

Compared with bash

Bash 5.3 has no fixed limit; it only fails when allocation fails. For {1..9223372036854775807} it prints brace expansion: failed to allocate memory for N elements, leaves the word literal, and keeps going with status 0. gbash fails hard at a fixed cap instead. This is a deliberate sandbox divergence, and SPEC.md now records it.

$ gbash -c 'echo {1..30000000} | wc -c; echo {1..30000000}; echo unreachable'; echo rc=$?
brace expansion: too many words (limit 100000), increase policy.Limits.MaxBraceExpansionWords
0
brace expansion: too many words (limit 100000), increase policy.Limits.MaxBraceExpansionWords
rc=126
# 0.02s, ~37 MB RSS

$ gbash -c 'echo {9223372036854775806..9223372036854775807}; echo {1..9223372036854775807..4611686018427387904}'
9223372036854775806 9223372036854775807
1 4611686018427387905

SPEC updates

  • Added the new default to the default-policy list, with the error text, the 126 status, and the bash divergence.
  • Noted under call interception that the brace budget is counted during expansion.
  • Added the new limit to the website security budget tables.

Testing

  • shell/expand: termination at the int bounds (run under a small budget so a regression fails fast instead of hanging), the cap at and over the limit, cartesian products, the budget spanning several words, and scope nesting and reset.
  • internal/runtime: the default cap on the 30M-word reproducer, the default still allowing 100000 words, overrides, the cap covering a whole command, array and declare operands, the count resetting per command in loops, subshell and $(...) behavior, and termination at the int bounds.
  • Package tests in go test ./shell/... ./internal/shell/... ./internal/runtime/... ./policy/... . pass.
  • Conformance slices pass: oils/(brace-expansion|assign|array|word-split|assign-extended|var-op-strip) and oils/(array-.*|assign-.*|builtin-meta-assign|word-eval) across bash, dash, mksh and zsh.
  • make build and make lint are clean.

Not covered

Repeated string doubling (x=$x$x in a loop) is still unbounded: 27 iterations give a 128 MB value and about 450 MB RSS. A proper fix needs a value-size cap applied to every assignment path, so it is left for a separate change.

Add policy.Limits.MaxBraceExpansionWords (default 100000 per command) so
a single brace expression such as {1..30000000} can no longer exhaust host
memory. Exceeding the cap prints a clear error and exits the current shell
with status 126.

Brace sequences now stop stepping before they would overflow int, so
ranges ending at the integer bounds terminate.
@ewhauser
ewhauser enabled auto-merge (squash) October 19, 2026 06:29
@ewhauser
ewhauser merged commit df54d54 into main Oct 19, 2026
16 checks passed
@ewhauser
ewhauser deleted the claude/wizardly-sutherland-10a50e branch October 19, 2026 06:36
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