Skip to content

[P2][rubash-bug] alias same-line suppression misfires across read-unit boundaries: source/eval-defined aliases collide with the caller's physical line numbers (command not found where GNU expands) #817

Closed

Description

@caomengxuan666

Summary

The alias same-read visibility guard approximates GNU's read-time rule with physical line identity: __RUBASH_ALIAS_LINE_<name> (recorded from the defining command's AST line) is compared against __RUBASH_CURRENT_LINE and equal values suppress expansion. Definition lines renumbered by a nested read-unit — a sourced file (restarts at 1 per file) or an eval string (renumbered caller_line + i - 1) — collide with the outer driver's later physical line numbers, so a perfectly legal later use is suppressed and reports command not found. GNU expands (the definition happened in an earlier read).

Found by the niubash#368 audit (read-unit / line-number identity). Engine base audited: origin/master 8ab35e89.

Load-bearing sites

  • Record: src/executor/alias_set_builtins.rs:65 + :88 (__RUBASH_ALIAS_LINE_<name> = cmd.line).
  • Suppress: src/executor/arithmetic_aliases.rs:832-834 (gate) + :867-878 (alias_defined_on_current_line comparison).
  • Renumbering sources that create the collisions: sourced files restart per file (run_source → read_next_source_group); eval bodies continue the caller counter (src/executor/trap_exec.rs:460-480, execute_eval_source(source, caller_line, cmd)).

Repro 1 — stdin script + source (def-line == use-line)

def5.sh:

:
:
:
:
alias x='echo AX'

Driver:

shopt -s expand_aliases
source def5.sh
:
:
x
echo rc=$?
Shell Result
GNU 5.3.0 (WSL, stdin method) AX / rc=0
rubash 8ab35e89 line 5: x: command not found / rc=127

Instrumented: after source, __RUBASH_ALIAS_LINE_x=5 while __RUBASH_CURRENT_LINE=3; the use at line 5 makes both sides read 5 → suppressed.

Repro 2 — eval-defined alias (eval-continuation line == caller line)

shopt -s expand_aliases
body=$': \n: \n: \nalias y=\'echo AY\''
eval "$body"
:
:
y
echo rc=$?

Eval body line 4 renumbers to caller line 6, marker y=6, use on caller line 6.

Shell Result
GNU AY / rc=0
rubash line 6: y: command not found / rc=127

Control: identical script with the use moved to line 7 → both shells print AY rc=0 (proves it is the line collision, not the source/eval definition path).

Semantics that must survive the fix (all verified equal to GNU on 8ab35e89)

  • alias ll='echo LL'; ll on one physical line (stdin script) → 127 both.
  • Same-line suppression inside a sourced file (alias x=…; x on the file's line 2) → 127 both; rubash's diagnostic carries the file's own line.
  • Same-line inside an eval string → 127 both.
  • Definition line N / use line N+1 (plain script) → expand both.
  • source/eval definition, use at a non-colliding later line → expand both.
  • -i (piped) multi-submission alias use → expand both.
  • bash -c collision layout → GNU expands, rubash expands (the -c route rides the grouped driver and never consults the marker).

Scope: the guard only bites where alias expansion runs executor-level against a pre-parsed AST — stdin scripts (run_stdin_script) and host fast paths that reach execute_command with aliases live. Grouped drivers expand at text level against the live table (script_driver.rs:1107-1109 → expand_group_aliases → lexer/alias_stream.rs:221, no marker logic), which is why niu interactive is unaffected (probe: first-prompt rc-line-1 alias holds marker=1, cur=1 yet expands).

Suggested direction

Replace the physical-line comparison with a read-unit identity (the "monotonic read generation" direction already proposed in niubash#368): record (generation, line) at definition, suppress only when both match. The generation boundary is exactly the nested-execution seam that already saves/restores __RUBASH_CURRENT_LINE — src/builtins/source/execution.rs:291-293,351-358 (GNU evalfile.c push_stream/run_unwind_frame) and the eval entry — so a generation counter pushed/popped there keeps every row of the must-survive table above while killing the cross-unit collision. Side benefit: the markers can leave the user-visible variable namespace (today ${__RUBASH_ALIAS_LINE_x} and ${__RUBASH_CURRENT_LINE} read via parameter expansion / declare -p, though set/export -p filter them; GNU has no such variables).

Audit context

Full audit report (host side): niubash docs/audit/findings/i368-read-unit-line-identity.md (PR on fix/368-audit). Related: niubash#367 (fixed — the interactive host now routes alias-bearing submissions through the grouped reader, which is why the host is immune), niubash#368 (this audit's host ticket).

Activity

  1. added
    bug
    P2-medium
    on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2-mediumbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions