Repository navigation
expand: cap brace expansion words and fix sequence overflow - #855
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Brace expansion had no size limit. A single script line could exhaust host memory, and none of the existing policy limits stopped it:
Ranges that end near the int bounds also never finished, because
n += incrwrapped around to MinInt:Changes
policy.Limits.MaxBraceExpansionWords, default100000. 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. Soecho {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: addsConfig.MaxBraceExpansionWords,BraceExpansionLimitError, andConfig.BeginBraceExpansionScope/EndBraceExpansionScope. Outside a scope the cap applies to eachFieldscall. The interpreter opens a scope around argv, decl and array expansion, because it expands those one word at a time.brace expansion: too many words (limit N), increase policy.Limits.MaxBraceExpansionWordsand 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.limitOverridePolicynow uses pointer receivers, because the largerLimitsstruct tripped gocritic'shugeParamcheck.Compared with bash
Bash 5.3 has no fixed limit; it only fails when allocation fails. For
{1..9223372036854775807}it printsbrace 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.SPEC updates
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 anddeclareoperands, the count resetting per command in loops, subshell and$(...)behavior, and termination at the int bounds.go test ./shell/... ./internal/shell/... ./internal/runtime/... ./policy/... .pass.oils/(brace-expansion|assign|array|word-split|assign-extended|var-op-strip)andoils/(array-.*|assign-.*|builtin-meta-assign|word-eval)across bash, dash, mksh and zsh.make buildandmake lintare clean.Not covered
Repeated string doubling (
x=$x$xin 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.