Skip to content

[bug] Multi-line function definition nested in a loop inside a function fails to parse (syntax error; GNU accepts) — kills liquidprompt test_disk/test_ram at rc=2 #314

Closed

Description

@caomengxuan666

Summary

rubash fails to PARSE a multi-line function definition nested directly inside a loop body, when the loop itself is inside a function. GNU bash 5.3.0 parses and executes it fine. One-line function definitions in the same position parse fine on rubash; the multi-line body alone (without the outer function wrapper) also parses fine — the failure needs all three ingredients: (1) enclosing function, (2) loop (for ((;;)), for-in, while), (3) multi-line g() { … } body.

Found by the ecosweep2 lane running liquidprompt's own shunit2 suite: tests/test_disk.sh and tests/test_ram.sh both define a mock function (df() { printf …; }) as the first statement of a for (( i=0; i < ${#outputs[@]}; i++ )) loop inside function test_disk { — both files die at parse time (syntax error: ';' unexpected / syntax error: (( i=0; … ))), rc=2, zero tests run. GNU runs them green.

Minimal reproducer (6 lines, script file)

f() {
for x in a b; do
    g() {
        echo
    }
done
}
  • GNU 5.3.0 (wsl /usr/local/bin/bash -n): rc=0
  • rubash (release @116b12aa): syntax error: unexpected end of file from '{' command on line 3, rc=2

With for (( i=0; i < 3; i++ )) instead of for x in, rubash instead reports line 2: syntax error: ';' unexpected + (( i=0; i < 3; i++ )) — two renderings of the same desync depending on loop flavor.

Variant matrix (rubash rc for -n; GNU rc=0 on every row)

shape rubash
wrapper fn + for ((;;)) + multi-line nested g(){…} CRASH
wrapper fn + for x in + multi-line nested CRASH
wrapper fn + while read + multi-line nested CRASH
f() {-syntax wrapper instead of function f { CRASH (same)
wrapper fn + loop + ONE-LINE nested g() { echo; } ok
wrapper fn + loop + non-function multi-line body ok
NO wrapper fn + loop + multi-line nested g(){…} ok
nested funcdef with ${#arr[@]} in the for-arith condition (test_disk exact shape) CRASH

GNU C source anchor (vendored third_party/bash)

parse.y:1054-1061 function_def productions (WORD '(' ')' newline_list function_body, FUNCTION WORD newline_list function_body) — a function definition is an ordinary compound command element in simple_command/body positions; nothing about a loop body inside a function restricts nested definitions. parse.y:419 grammar types; body parsed via the same parse_compound_command path as top level.

Environment / artifacts

  • rubash release @116b12aa (wt13/ecosweep2); oracle WSL GNU bash 5.3.0 script-file runs
  • liquidprompt @afd7e83 suite outputs: target/issue-suites/results/ecosweep2/lp/rb/test_disk.sh.{out,err,rc}, test_ram.sh.*
  • Variant probes: pv-n1..n10.sh, pv-m1..m5.sh, pv-a2..h.sh, p-fornest.sh under target/issue-suites/results/ecosweep2/

Activity

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

    @caomengxuan666
    MemberAuthor

    Fixed by wt13/sweepfix (54349c7a, master 203c2b5): the LOOP frame accepted ANY boundary close-brace as terminator (for the for x; { } brace-form), so a nested g() { ... } close-brace was double-counted - popping the loop frame AND decrementing the outer function group depth, truncating the function body at the nested brace. Frame-exact pairing per parse.y:1054-1061 function_def + :879-880 (two body forms; after do only done closes). Both repros + 11-shape matrix + execution semantics pass; liquidprompt test_disk/test_ram rc=2 -> rc=0.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions