zsh-workers
 help / color / mirror / code / Atom feed
* Parser bugs: "local a=()"
@ 2015-07-12  9:13 ryosuke_i_628
  2015-07-12 17:19 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: ryosuke_i_628 @ 2015-07-12  9:13 UTC (permalink / raw)
  To: zsh-workers

--Summary--

Zsh parser recognize "local a=()" as a local function named "a=".
Sometimes that causes segmentation fault.

(in Japanese)
http://qiita.com/mpyw/items/e9e4c3b872b30c7024ee

--Version--

zsh 5.0.8 (x86_64-apple-darwin14.3.0)

--Reproduction--

[funcs.zsh]

    func1() {
        a=( )
        b=false
        echo "a in func1 length: ${#a[@]}"
    }
    func2() {
        local a=( )
        local b=false
        echo "a in func2 length: ${#a[@]}"
    }
    func3() {
        a=()
        b=false
        echo "a in func3 length: ${#a[@]}"
    }
    func4() {
        local a=()
        local b=false
        echo "a in func4 length: ${#a[@]}"
    }

[Your shell-session]

    example@local:~$ zsh
    example@local:~$ zsh --version
    zsh 5.0.8 (x86_64-apple-darwin14.3.0)
    example@local:~$ source funcs.zsh
    example@local:~$ func1
    a in func1 length: 0
    example@local:~$ func2
    func2:1: unknown file attribute:
    example@local:~$ func3
    a in func3 length: 0
    example@local:~$ func4
    a in func4 length: 0
    [1]    35208 segmentation fault  zsh


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Parser bugs: "local a=()"
  2015-07-12  9:13 Parser bugs: "local a=()" ryosuke_i_628
@ 2015-07-12 17:19 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2015-07-12 17:19 UTC (permalink / raw)
  To: zsh-workers

On Sun, 12 Jul 2015 18:13:18 +0900 (JST)
ryosuke_i_628@yahoo.co.jp wrote:
> func4() {
>   local a=()
>   local b=false
>   echo "a in func4 length: ${#a[@]}"
> }
> example@local:~$ func4
>  a in func4 length: 0
>  [1]  135208 segmentation fault  zsh
  
Parsing of this has changed recently (since 5.0.8), but you can see what
the effect is by doing (whatever the version)

func4() {
  \local a=()
  \local b=false
  echo "a in func4 length: ${#a[@]}"
}

(this suppresses the new special parsing of "local").

Running this gives me:

 :3: maximum nested function level reached

with no segmentation violation, but I would guess the recursive call is
the key to the problem --- there's no robust way for the shell to find
out it's used as many resources and it can and unwind safely, so the
builtin limit is just a guess whose effect is highly system dependent and
that tends to get worse as time goes on and specs change.

You can see why by doing:

  functions local
  local () {
	\local b=false
  }

What's happened is the multiple function definition code has defined
functions called "local" and "a=" with the body on the next
line (as there are no braces, it's a single line definition). "\local
b=false".  So invoking local causes infinite recursion.

At the time, this was actually correct parsing according to the rules in
force, if extremely obscure.

Things you can do to protect yourself (apart from upgrading to the next
release, currently vapourware) include "unsetopt multifuncdef" to
disallow the also rather obscure and not often used feature of defining
multiple functions in one go.

When something like this  has come up before. we've never managed to
work out a safer way of handling recursive functions that's both robust
and non-intrusive, i.e. doesn't forbid useful behaviour.

pws


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-07-12 17:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-12  9:13 Parser bugs: "local a=()" ryosuke_i_628
2015-07-12 17:19 ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).