Vim expr parsing bug

54 views
Skip to first unread message

Andy Wokula

unread,
Nov 22, 2012, 12:44:39 PM11/22/12
to vim...@googlegroups.com
" Vim Parsing Bug

func! Add2(x1, x2)
return a:x1 + a:x2
endfunc

:echo function('Add2')(2,3)
" 5 (ok)

" Bug:
:echo 1 ? function('Add2')(1,2) : function('Add2')(2,3)
:echo 0 ? function('Add2')(1,2) : function('Add2')(2,3)

" Error detected while processing D1223.vim:
" line 17:
" E110: Missing ')'
" E15: Invalid expression: (2,3)
" line 18:
" E109: Missing ':' after '?'
" E15: Invalid expression: 0 ? function('Add2')(1,2) : function('Add2')(2,3)


Adding parentheses 1 ? (...) : (...) doesn't help.

--
Andy

Bram Moolenaar

unread,
Nov 22, 2012, 4:18:37 PM11/22/12
to Andy Wokula, vim...@googlegroups.com
Strange. I'll add it to the todo list.

--
"I simultaneously try to keep my head in the clouds and my feet on the
ground. Sometimes it's a stretch, though." -- Larry Wall

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Christian Brabandt

unread,
Nov 22, 2012, 5:28:58 PM11/22/12
to vim...@googlegroups.com
Hi Bram!

On Do, 22 Nov 2012, Bram Moolenaar wrote:

>
> Andy Wokula wrote:
>
> > " Vim Parsing Bug
> >
> > func! Add2(x1, x2)
> > return a:x1 + a:x2
> > endfunc
> >
> > :echo function('Add2')(2,3)
> > " 5 (ok)
> >
> > " Bug:
> > :echo 1 ? function('Add2')(1,2) : function('Add2')(2,3)
> > :echo 0 ? function('Add2')(1,2) : function('Add2')(2,3)
> >
> > " Error detected while processing D1223.vim:
> > " line 17:
> > " E110: Missing ')'
> > " E15: Invalid expression: (2,3)
> > " line 18:
> > " E109: Missing ':' after '?'
> > " E15: Invalid expression: 0 ? function('Add2')(1,2) : function('Add2')(2,3)
> >
> >
> > Adding parentheses 1 ? (...) : (...) doesn't help.
>
> Strange. I'll add it to the todo list.

Problem is, in expr1 ? expr2 : expr3
Vim explicitly resets evaluate in the false case which leads to being
rettv.v_type being VAR_UNKNOWN and then handle_subscript doesn't handle
the subscript anymore.

Here is a patch:

diff --git a/src/eval.c b/src/eval.c
--- a/src/eval.c
+++ b/src/eval.c
@@ -19574,7 +19574,7 @@
while (ret == OK
&& (**arg == '['
|| (**arg == '.' && rettv->v_type == VAR_DICT)
- || (**arg == '(' && rettv->v_type == VAR_FUNC))
+ || (**arg == '(' && (rettv->v_type == VAR_FUNC || rettv->v_type == VAR_UNKNOWN)))
&& !vim_iswhite(*(*arg - 1)))
{
if (**arg == '(')


regards,
Christian
--
Niemand ist so gut oder so schlecht, wie er w�hrend seiner Scheidung
gemacht wird.
-- J.J. Disenberg

Andy Wokula

unread,
Nov 22, 2012, 7:03:55 PM11/22/12
to vim...@googlegroups.com
Am 22.11.2012 23:28, schrieb Christian Brabandt:
> Problem is, in expr1 ? expr2 : expr3
> Vim explicitly resets evaluate in the false case which leads to being
> rettv.v_type being VAR_UNKNOWN and then handle_subscript doesn't handle
> the subscript anymore.
>
> Here is a patch:
>
> diff --git a/src/eval.c b/src/eval.c
> --- a/src/eval.c
> +++ b/src/eval.c
> @@ -19574,7 +19574,7 @@
> while (ret == OK
> && (**arg == '['
> || (**arg == '.' && rettv->v_type == VAR_DICT)
> - || (**arg == '(' && rettv->v_type == VAR_FUNC))
> + || (**arg == '(' && (rettv->v_type == VAR_FUNC || rettv->v_type == VAR_UNKNOWN)))
> && !vim_iswhite(*(*arg - 1)))
> {
> if (**arg == '(')

Now gVim crashes.

--
Andy

Christian Brabandt

unread,
Nov 23, 2012, 2:42:54 AM11/23/12
to vim...@googlegroups.com
Hi Andy!
Hm, interesting. For me it passes all tests. Can you get me a stack
trace?

regards,
Christian
--

Bram Moolenaar

unread,
Nov 23, 2012, 11:13:12 AM11/23/12
to Christian Brabandt, vim...@googlegroups.com
Thanks for the quick fix.

Now, how about a test?

--
Be thankful to be in a traffic jam, because it means you own a car.

Christian Brabandt

unread,
Nov 23, 2012, 3:39:47 PM11/23/12
to vim...@googlegroups.com
Hi Bram!
Well, I am not sure, what caused the segfault for Andy. But I have been
checking with him privately, that this patch works and added some tests.
So here we go, a different patch, including a test.

regards,
Christian
--
funcref_parsing.diff

Bram Moolenaar

unread,
Nov 23, 2012, 4:09:51 PM11/23/12
to Christian Brabandt, vim...@googlegroups.com
Thanks!

--
hundred-and-one symptoms of being an internet addict:
62. If your doorbell rings, you think that new mail has arrived. And then
you're disappointed that it's only someone at the door.

Andy Wokula

unread,
Jan 4, 2013, 9:11:06 AM1/4/13
to vim...@googlegroups.com, Bram Moolenaar
(obsolete patch)
>>>
>>> Thanks for the quick fix.
>>>
>>> Now, how about a test?
>>
>> Well, I am not sure, what caused the segfault for Andy. But I have been
>> checking with him privately, that this patch works and added some tests.
>> So here we go, a different patch, including a test.
>
> Thanks!

So far, this latest patch works ok for me.

--
Andy
Reply all
Reply to author
Forward
0 new messages