zsh-workers
 help / Atom feed
* [bug] Problem with functions -s (string arg) math function & specific input
@ 2019-08-19  1:00 Sebastian Gniazdowski
  2019-08-19  1:18 ` Bart Schaefer
  2019-08-19  7:01 ` Stephane Chazelas
  0 siblings, 2 replies; 7+ messages in thread
From: Sebastian Gniazdowski @ 2019-08-19  1:00 UTC (permalink / raw)
  To: Zsh hackers list

Hello,
the contents of the string argument seems to be evaluated in some way
(while it shouldn't). Reproducing snippet:

mtest() { print ${#1}; }
functions -s -M mtest 1 1 mtest
input=') &>/dev/null'
(( mtest($input) ))

Output:
0
zsh: bad math expression: operand expected at `>/dev/null...'

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

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

* Re: [bug] Problem with functions -s (string arg) math function & specific input
  2019-08-19  1:00 [bug] Problem with functions -s (string arg) math function & specific input Sebastian Gniazdowski
@ 2019-08-19  1:18 ` Bart Schaefer
  2019-08-19  1:55   ` Sebastian Gniazdowski
  2019-08-19  7:01 ` Stephane Chazelas
  1 sibling, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2019-08-19  1:18 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh hackers list

[-- Attachment #1: Type: text/plain, Size: 303 bytes --]

On Sun, Aug 18, 2019, 8:01 PM Sebastian Gniazdowski <sgniazdowski@gmail.com>
wrote:

>
> functions -s -M mtest 1 1 mtest
>

I'm not at a computer where I can confirm this, but I think the order of
options is significant, you have to use

functions -M -s mtest 1 1 mtest

to define a string function.

>

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

* Re: [bug] Problem with functions -s (string arg) math function & specific input
  2019-08-19  1:18 ` Bart Schaefer
@ 2019-08-19  1:55   ` Sebastian Gniazdowski
  0 siblings, 0 replies; 7+ messages in thread
From: Sebastian Gniazdowski @ 2019-08-19  1:55 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

On Mon, 19 Aug 2019 at 03:19, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Sun, Aug 18, 2019, 8:01 PM Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
>> functions -s -M mtest 1 1 mtest
>
> I'm not at a computer where I can confirm this, but I think the order of options is significant, you have to use
>
> functions -M -s mtest 1 1 mtest
>
> to define a string function.

It still reports the same error.
-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

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

* Re: [bug] Problem with functions -s (string arg) math function & specific input
  2019-08-19  1:00 [bug] Problem with functions -s (string arg) math function & specific input Sebastian Gniazdowski
  2019-08-19  1:18 ` Bart Schaefer
@ 2019-08-19  7:01 ` Stephane Chazelas
  2019-08-19  9:53   ` Sebastian Gniazdowski
  1 sibling, 1 reply; 7+ messages in thread
From: Stephane Chazelas @ 2019-08-19  7:01 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh hackers list

2019-08-19 03:00:24 +0200, Sebastian Gniazdowski:
> Hello,
> the contents of the string argument seems to be evaluated in some way
> (while it shouldn't). Reproducing snippet:
> 
> mtest() { print ${#1}; }
> functions -s -M mtest 1 1 mtest
> input=') &>/dev/null'
> (( mtest($input) ))
> 
> Output:
> 0
> zsh: bad math expression: operand expected at `>/dev/null...'
[...]

Expansions are done first, and then the result evaluated as
an arithmetic expression. That's a POSIX requirement (for
$((...)))

So here, you're evaluating the:

mtest() &>/dev/null

arithmetic expression.

Just like in:

a='1] + b[2'
echo $((b[$a]))

you're evaluating the b[1] + b[2] arithmetic expression.

-- 
Stephane

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

* Re: [bug] Problem with functions -s (string arg) math function & specific input
  2019-08-19  7:01 ` Stephane Chazelas
@ 2019-08-19  9:53   ` Sebastian Gniazdowski
  2019-08-19  9:59     ` Sebastian Gniazdowski
  0 siblings, 1 reply; 7+ messages in thread
From: Sebastian Gniazdowski @ 2019-08-19  9:53 UTC (permalink / raw)
  To: Sebastian Gniazdowski, Zsh hackers list

On Mon, 19 Aug 2019 at 09:01, Stephane Chazelas
<stephane.chazelas@gmail.com> wrote:
> 2019-08-19 03:00:24 +0200, Sebastian Gniazdowski:
> > Hello,
> > the contents of the string argument seems to be evaluated in some way
> > (while it shouldn't). Reproducing snippet:
> >
> > mtest() { print ${#1}; }
> > functions -s -M mtest 1 1 mtest
> > input=') &>/dev/null'
> > (( mtest($input) ))
> >
> > Output:
> > 0
> > zsh: bad math expression: operand expected at `>/dev/null...'
> [...]
>
> Expansions are done first, and then the result evaluated as
> an arithmetic expression. That's a POSIX requirement (for
> $((...)))
>
> So here, you're evaluating the:
>
> mtest() &>/dev/null

So it seems that ${MATCH//\)/\\\)} should suffice to fix this problem,
as it seems that once a string is within parens (correctly), further
expansions are put on hold for the -s functions.
-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

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

* Re: [bug] Problem with functions -s (string arg) math function & specific input
  2019-08-19  9:53   ` Sebastian Gniazdowski
@ 2019-08-19  9:59     ` Sebastian Gniazdowski
  2019-08-19 13:29       ` Stephane Chazelas
  0 siblings, 1 reply; 7+ messages in thread
From: Sebastian Gniazdowski @ 2019-08-19  9:59 UTC (permalink / raw)
  To: Sebastian Gniazdowski, Zsh hackers list

On Mon, 19 Aug 2019 at 11:53, Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
> So it seems that ${MATCH//\)/\\\)} should suffice to fix this problem,
> as it seems that once a string is within parens (correctly), further
> expansions are put on hold for the -s functions.

Quoting of ( is also needed. It doesn't error without it, however it
then includes the closing paren in the string argument.

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

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

* Re: [bug] Problem with functions -s (string arg) math function & specific input
  2019-08-19  9:59     ` Sebastian Gniazdowski
@ 2019-08-19 13:29       ` Stephane Chazelas
  0 siblings, 0 replies; 7+ messages in thread
From: Stephane Chazelas @ 2019-08-19 13:29 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh hackers list

2019-08-19 11:59:27 +0200, Sebastian Gniazdowski:
> On Mon, 19 Aug 2019 at 11:53, Sebastian Gniazdowski
> <sgniazdowski@gmail.com> wrote:
> > So it seems that ${MATCH//\)/\\\)} should suffice to fix this problem,
> > as it seems that once a string is within parens (correctly), further
> > expansions are put on hold for the -s functions.
> 
> Quoting of ( is also needed. It doesn't error without it, however it
> then includes the closing paren in the string argument.
[...]

It appears so. Or more exactly, you'd need to escape any
unmatched unescaped ( or ). (input='()' is fine).

-- 
Stephane

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

end of thread, back to index

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-19  1:00 [bug] Problem with functions -s (string arg) math function & specific input Sebastian Gniazdowski
2019-08-19  1:18 ` Bart Schaefer
2019-08-19  1:55   ` Sebastian Gniazdowski
2019-08-19  7:01 ` Stephane Chazelas
2019-08-19  9:53   ` Sebastian Gniazdowski
2019-08-19  9:59     ` Sebastian Gniazdowski
2019-08-19 13:29       ` Stephane Chazelas

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/ public-inbox