Skip to content

fix(hack): guard empty-array expansion in lint-drift for bash 3.2#117

Open
omergk28 wants to merge 1 commit into
ActiveMemory:mainfrom
omergk28:fix/lint-drift-bash32-empty-array
Open

fix(hack): guard empty-array expansion in lint-drift for bash 3.2#117
omergk28 wants to merge 1 commit into
ActiveMemory:mainfrom
omergk28:fix/lint-drift-bash32-empty-array

Conversation

@omergk28

Copy link
Copy Markdown
Contributor

Summary

make audit — the contributing guide's mandatory pre-PR gate — aborts on stock macOS before running a single drift check:

./hack/lint-drift.sh: line 39: exclude_args[@]: unbound variable

bash 3.2 (the newest Apple ships, frozen at the GPLv2 boundary) treats "${arr[@]}" on an empty array as an unbound variable under set -u; bash 4.4+ does not. drift_grep expands its exclude_args array unconditionally and several call sites pass no excludes, so every macOS contributor hits this on the very first audit.

Fix

Guard the expansion with the canonical parameter-expansion alternate form ${arr[@]+"${arr[@]}"}: an empty array expands to zero words, a populated array reproduces the original quoted expansion, and bash 4+ behavior is unchanged. The other hack/ array expansions were checked empirically — all safe today (count-guarded or hardcoded non-empty); details in the spec.

Verification

  • Reproduced the abort on bash 3.2.57 (stock macOS) before the fix; bash hack/lint-drift.sh now exits 0.
  • go build ./... clean.
  • Spec: specs/fix-lint-drift-bash32-empty-array.md; the gotcha is captured in LEARNINGS.md so the next session doesn't re-debug it.

Provenance

Split out of #113 (the #93 TOCTOU fix) so each PR carries a single concern, per the contributing guide. This is an independent commit touching disjoint files (hack/lint-drift.sh, its spec, and a LEARNINGS entry).

🤖 Generated with Claude Code

Stock macOS ships bash 3.2 (GPLv2 freeze), which treats
"${arr[@]}" on an empty array as an unbound variable under
set -u — bash 4.4+ does not. drift_grep expands its exclude
array unconditionally and several call sites pass no excludes,
so make lint-style (and therefore make audit, the mandatory
pre-PR gate) aborted on every stock Mac before running a single
drift check.

Guard with the parameter-expansion alternate form,
${arr[@]+"${arr[@]}"}: empty expands to zero words, populated
reproduces the original quoted expansion, bash 4+ behavior
unchanged. The other hack/ array expansions are safe today
(count-guarded or hardcoded non-empty); details in the spec.

Captures the gotcha in LEARNINGS.md so the next session does
not re-debug it.

Spec: specs/fix-lint-drift-bash32-empty-array.md
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Omer Kocaoglu <omergk28@gmail.com>
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