Patchwork [STABLE] parser: fix crash by parsing "()" in keyword argument position

login
register
mail settings
Submitter Yuya Nishihara
Date May 3, 2019, 11:32 a.m.
Message ID <caaf1fa0f73f56f8fcfd.1556883144@mimosa>
Download mbox | patch
Permalink /patch/39927/
State Accepted
Headers show

Comments

Yuya Nishihara - May 3, 2019, 11:32 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1556881563 -32400
#      Fri May 03 20:06:03 2019 +0900
# Branch stable
# Node ID caaf1fa0f73f56f8fcfd5408d7fa0dfaa8a2a510
# Parent  724dae82c4bd935c19377c666bb5ef230aa32dd3
parser: fix crash by parsing "()" in keyword argument position

A tree node can be either None or a tuple because x=("group", None) is
reduced to x[1].
via Mercurial-devel - May 3, 2019, 4:28 p.m.
On Fri, May 3, 2019 at 4:35 AM Yuya Nishihara <yuya@tcha.org> wrote:

> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1556881563 -32400
> #      Fri May 03 20:06:03 2019 +0900
> # Branch stable
> # Node ID caaf1fa0f73f56f8fcfd5408d7fa0dfaa8a2a510
> # Parent  724dae82c4bd935c19377c666bb5ef230aa32dd3
> parser: fix crash by parsing "()" in keyword argument position
>

Queued, thanks.

Patch

diff --git a/mercurial/parser.py b/mercurial/parser.py
--- a/mercurial/parser.py
+++ b/mercurial/parser.py
@@ -147,7 +147,8 @@  def buildargsdict(trees, funcname, argsp
     arguments are rejected, but missing keyword arguments are just omitted.
     """
     poskeys, varkey, keys, optkey = argspec
-    kwstart = next((i for i, x in enumerate(trees) if x[0] == keyvaluenode),
+    kwstart = next((i for i, x in enumerate(trees)
+                    if x and x[0] == keyvaluenode),
                    len(trees))
     if kwstart < len(poskeys):
         raise error.ParseError(_("%(func)s takes at least %(nargs)d positional "
@@ -171,7 +172,7 @@  def buildargsdict(trees, funcname, argsp
     if optkey:
         args[optkey] = util.sortdict()
     for x in trees[kwstart:]:
-        if x[0] != keyvaluenode or x[1][0] != keynode:
+        if not x or x[0] != keyvaluenode or x[1][0] != keynode:
             raise error.ParseError(_("%(func)s got an invalid argument")
                                    % {'func': funcname})
         k = x[1][1]
diff --git a/tests/test-revset.t b/tests/test-revset.t
--- a/tests/test-revset.t
+++ b/tests/test-revset.t
@@ -472,6 +472,12 @@  keyword arguments
   $ log 'extra(unknown=branch)'
   hg: parse error: extra got an unexpected keyword argument 'unknown'
   [255]
+  $ log 'extra((), x)'
+  hg: parse error: first argument to extra must be a string
+  [255]
+  $ log 'extra(label=x, ())'
+  hg: parse error: extra got an invalid argument
+  [255]
 
   $ try 'foo=bar|baz'
   (keyvalue