Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expansion of $@ with empty IFS variable #2141

Open
laurenthuberdeau opened this issue Nov 25, 2024 · 2 comments
Open

Expansion of $@ with empty IFS variable #2141

laurenthuberdeau opened this issue Nov 25, 2024 · 2 comments

Comments

@laurenthuberdeau
Copy link

laurenthuberdeau commented Nov 25, 2024

Bug

When IFS='', $@ is expanded with no white space between the elements instead of with white space which breaks word splitting. This simple program shows the issue:

res=0
sum() {
  # implement callee-save calling convention using `set`
  # here, we save the value of $res after the function parameters
  set $@ $res           # $1 $2 $3 are now set
  res=$(($1 + $2))
  echo "$1 + $2 = $res"
  res=$3                # restore the value of $res
}

unset IFS
sum 12 30 # outputs "12 + 30 = 42"

IFS=' '
sum 12 30 # outputs "12 + 30 = 42"

IFS=
sum 12 30 # outputs "1230 + 0 = 1230"

set -u
IFS=
sum 12 30 # fails with "fatal: Undefined variable '2'" on res=$(($1 + $2))
@andychu
Copy link
Contributor

andychu commented Nov 26, 2024

Thanks for finding this and reporting it! I confirmed the bug, and copied the failing test case in

ea5db50

We have to do an overhaul of IFS, since there are a few other bugs there too ...

@andychu
Copy link
Contributor

andychu commented Nov 26, 2024

Note: I tried pnut and hit this

$ ./bootstrap-pnut-sh.sh  --shell ~/git/oilshell/oil/_bin/cxx-opt/osh --fast
Bootstrap with /home/andy/git/oilshell/oil/_bin/cxx-opt/osh
    : $((_$((_heap + result + 1)) = $3))
                                    ^~
build/pnut-sh.sh:195: fatal: Undefined variable '3'
Command exited with non-zero status 1

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

No branches or pull requests

2 participants