zsh-workers
 help / color / mirror / code / Atom feed
* Infinite recursions in math evaluation
@ 2011-05-13 21:37 Mikael Magnusson
  2011-05-14  4:36 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Mikael Magnusson @ 2011-05-13 21:37 UTC (permalink / raw)
  To: zsh workers

I was trying to do something completely different and noticed that
this causes an infinite recursion:
a=a; ${(#)a}
and this doesn't
${(#):-a}
(and surprised me by cding into my first $cdpath entry, turns out it
returned a null character, and that does that with autocd on.)

The thing seems to be the variable name having the same name as the
value of it. Aha, actually a=a; $(( a )) crashes in the same way. (On
a machine with a presumably less optimized/buggy compile, it does
print "zsh: math recursion limit exceeded" instead of crashing). Is it
simply a user error? I wouldn't expect either of these expressions to
recursively look up values of the parameter until it encountered a
number, but this is indeed what happens:
% a=b; b=c; c=d; d=e; e=f; f=5; echo $(( a ))
5
bash does the same...

It seems to be of somewhat random utility though:
a='b c'
b='5 +'
c='5*3'
echo $(( a ))
zsh: bad math expression: operator expected at `c'
a='b+c'
echo $(( a ))
zsh: bad math expression: operand expected at `'
b='55+32'
echo $(( a ))
102

I scanned quickly through the "Arithmetic Evaluation" section and
found nothing suggesting that this should happen. It is also perhaps
somewhat unclear from the description of the (#) flag in the manpage
that it causes the same eval as in $(( )), it simply says the
resulting words are parsed as numeric expressions. Executing echo
${(#):-'foo=bar, bar=65, foo'} twice outputs A the second time btw.

I would not be opposed to just describing this behaviour in the
manpage (if I didn't just miss a huge section that describes it
again.. :)

-- 
Mikael Magnusson

PS What I originally actually wanted to run was $(( #a )), but I
remembered the wrong syntax.


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

* Re: Infinite recursions in math evaluation
  2011-05-13 21:37 Infinite recursions in math evaluation Mikael Magnusson
@ 2011-05-14  4:36 ` Bart Schaefer
  2011-05-14  9:03   ` Mikael Magnusson
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2011-05-14  4:36 UTC (permalink / raw)
  To: zsh workers

On May 13, 11:37pm, Mikael Magnusson wrote:
}
} [...]  Aha, actually a=a; $(( a )) crashes in the same way. (On
} a machine with a presumably less optimized/buggy compile, it does
} print "zsh: math recursion limit exceeded" instead of crashing). Is it
} simply a user error? I wouldn't expect either of these expressions to
} recursively look up values of the parameter until it encountered a
} number, but this is indeed what happens:
} % a=b; b=c; c=d; d=e; e=f; f=5; echo $(( a ))
} 5
} bash does the same...
} 
} I scanned quickly through the "Arithmetic Evaluation" section and
} found nothing suggesting that this should happen.

It's implicit in this:

    Named parameters and subscripted arrays can be referenced by name
    within an arithmetic expression without using the parameter expansion
    syntax.  For example,

        ((val2 = val1 * 2))

    assigns twice the value of $val1 to the parameter named val2.

"The value of $val1" in an arithmetic context is the result of doing
arithmetic evaluation on whatever is stored in the parameter.  If
that's the name of another (or even the same) parameter, then ...


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

* Re: Infinite recursions in math evaluation
  2011-05-14  4:36 ` Bart Schaefer
@ 2011-05-14  9:03   ` Mikael Magnusson
  2011-05-14 18:00     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Mikael Magnusson @ 2011-05-14  9:03 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh workers

On 14 May 2011 06:36, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On May 13, 11:37pm, Mikael Magnusson wrote:
> }
> } [...]  Aha, actually a=a; $(( a )) crashes in the same way. (On
> } a machine with a presumably less optimized/buggy compile, it does
> } print "zsh: math recursion limit exceeded" instead of crashing). Is it
> } simply a user error? I wouldn't expect either of these expressions to
> } recursively look up values of the parameter until it encountered a
> } number, but this is indeed what happens:
> } % a=b; b=c; c=d; d=e; e=f; f=5; echo $(( a ))
> } 5
> } bash does the same...
> }
> } I scanned quickly through the "Arithmetic Evaluation" section and
> } found nothing suggesting that this should happen.
>
> It's implicit in this:
>
>    Named parameters and subscripted arrays can be referenced by name
>    within an arithmetic expression without using the parameter expansion
>    syntax.  For example,
>
>        ((val2 = val1 * 2))
>
>    assigns twice the value of $val1 to the parameter named val2.

Well, I read that part, but

> "The value of $val1" in an arithmetic context is the result of doing
> arithmetic evaluation on whatever is stored in the parameter.  If
> that's the name of another (or even the same) parameter, then ...

How did you get this from the quoted part?

-- 
Mikael Magnusson


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

* Re: Infinite recursions in math evaluation
  2011-05-14  9:03   ` Mikael Magnusson
@ 2011-05-14 18:00     ` Bart Schaefer
  2011-05-14 18:09       ` Mikael Magnusson
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2011-05-14 18:00 UTC (permalink / raw)
  To: zsh workers

On May 14, 11:03am, Mikael Magnusson wrote:
}
} > "The value of $val1" in an arithmetic context is the result of doing
} > arithmetic evaluation on whatever is stored in the parameter.  If
} > that's the name of another (or even the same) parameter, then ...
} 
} How did you get this from the quoted part?

I did say it was implicit. :-)

     At each nested level of substitution, the substituted words
     undergo all forms of single-word substitution (i.e. not filename
     generation), including command substitution, arithmetic expansion
     and filename expansion

In ((var)), "var" is a nested arithmetic expansion.  Inside the value
of var, any mention of another bare parameter name is thus also a
nested arithmetic expansion.


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

* Re: Infinite recursions in math evaluation
  2011-05-14 18:00     ` Bart Schaefer
@ 2011-05-14 18:09       ` Mikael Magnusson
  0 siblings, 0 replies; 5+ messages in thread
From: Mikael Magnusson @ 2011-05-14 18:09 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh workers

On 14 May 2011 20:00, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On May 14, 11:03am, Mikael Magnusson wrote:
> }
> } > "The value of $val1" in an arithmetic context is the result of doing
> } > arithmetic evaluation on whatever is stored in the parameter.  If
> } > that's the name of another (or even the same) parameter, then ...
> }
> } How did you get this from the quoted part?
>
> I did say it was implicit. :-)
>
>     At each nested level of substitution, the substituted words
>     undergo all forms of single-word substitution (i.e. not filename
>     generation), including command substitution, arithmetic expansion
>     and filename expansion
>
> In ((var)), "var" is a nested arithmetic expansion.  Inside the value
> of var, any mention of another bare parameter name is thus also a
> nested arithmetic expansion.

I can't see how this follows from the above. That whole section is
talking about multiple nested ${...} forms specifically, not nested
arithmetic expansions. I rather read the arithmetic expansion there to
mean that if you write ${$((5 + 3))} it will work, which it does. It
doesn't really say anything about what happens in arithmetic expansion
itself (does it?).

-- 
Mikael Magnusson


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

end of thread, other threads:[~2011-05-14 18:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-13 21:37 Infinite recursions in math evaluation Mikael Magnusson
2011-05-14  4:36 ` Bart Schaefer
2011-05-14  9:03   ` Mikael Magnusson
2011-05-14 18:00     ` Bart Schaefer
2011-05-14 18:09       ` Mikael Magnusson

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).