Skip to content

[set -u] unquoted heredoc 内 \$ 被展开致 unbound(安装器形)+ 后台子 shell 内关联数组元素赋值 building[$1]=1 误报下标 unbound(并行 worker 形)— WSL GNU 5.3 均正常 #2486

Closed

Description

@caomengxuan666

现象(两条独立 set -u 面缺陷,oracle = WSL GNU bash 5.3.20 脚本文件,均确定性复现)

A. unquoted heredoc 内 \$ 在 set -u 下被展开并误报 unbound

set -u
PKGV=xyz
cat <<EOF
PREFIX="\${0%/*}/.."
LIBDIR="\$PREFIX/lib"
plain=$PKGV
EOF
echo "rc=$?"
输出
GNU 5.3.20(WSL) PREFIX="${0%/*}/.." / LIBDIR="$PREFIX/lib" / plain=xyz / rc=0
rubash master 1c93ea87 line 3: PREFIX: unbound variable / rc=1

GNU:未引号 heredoc 正文里 \$ 是转义(保留字面 $),只有裸 $ 才展开(parse.y heredoc 正文走 expand_string-族并由 \ 保护 $/`/\,见 subst.c 的 Q_HERE_DOCUMENT 处理与 string_extract/skip_single_quoted 邻近注释)。rubash 把 \${0%/*} 当作展开再在 set -u 下报 PREFIX(实际是 $PREFIX 第二行)unbound——set -u 只是放大器,根因是 heredoc 正文的 \$ 转义未被尊重。安装器类脚本(生成 wrapper 脚本的 heredoc)直接中招。

B. set -u 下后台子 shell 内函数的关联数组元素赋值误报 unbound

set -u
declare -A building=()
b1() { building[$1]=1; echo "set $1 ok"; }
b1 app
( b1 app > log1.txt 2>&1 ) &
wait
cat log1.txt
echo "main-ok"
输出
GNU 5.3.20(WSL) set app ok / set app ok / main-ok
rubash master 1c93ea87 set app ok / line 3: app: unbound variable / main-ok

顶层调用正常,后台子 shell 内同一调用在 building[$1]=1 处报 app unbound——下标 $1 已展开为 app 后,子 shell 里把 app 当变量名再查了一次(关联数组下标在 set -u 下不应作为变量引用求值;GNU arrayfunc.c 的 array_expand_index/assoc 赋值对字面下标不走 unbound 检查)。并行任务(& + wait 的 worker 模式)全灭。

证据

D:\repo\nbbuild\evidence\pre-1.5.0\t8\repro\niu-heredoc-escaped-dollar-setu.sh、niu-assoc-assign-bg-subshell-setu.sh(LF);两侧输出见上(主会话 2026-11-01 23:45 WSL/独立 rubash 复核)。来源:1.5.0 发版总单 #792 §4.5 脚本负载(Windows 真机 T8,安装器与并行 worker 两个真实形状的根因)。

Activity

  1. added
    bug
    on Nov 1, 2026
  2. added 2 commits that reference this issue on Nov 2, 2026
  3. caomengxuan666 commented on Nov 2, 2026

    @caomengxuan666
    MemberAuthor

    Both halves fixed and merged via PR #2491 (merge d269fb0).

    1. Heredoc nounset scan: the scan for unguarded expansions now runs on the escape-prepared body text (the same text the expansion pass consumes), so \${x} — an escaped dollar, literal in GNU — no longer counts as a reference and no longer errors under set -u; the other three [bug] $[...] 提取不感知引号状态:外层双引号内含未转义引号的形状定界分歧(#2217 残差,既存) #2227 escape shapes keep working.
    2. Respawn attribute transport: the four respawn env-build sites now iterate env_vars_snapshot(), so attribute markers (+A etc.) materialize on the wire form; a respawned subshell child keeps the parent's associative-array attributes instead of degrading them to strings.

    Verification (harvest): branch rebased onto master; local scrubbed pins on the rebased tree — issue2486_assoc_assign_subshell_nounset 5/5, issue2486_heredoc_escaped_dollar 4/4; full CI + cross-repo ledger green on the pre-merge head, canary green on the final merge head.

  4. added a commit that references this issue on Nov 12, 2026
    d269fb0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions