Patchwork [1,of,2] fileset: make it robust for bad function calls

login
register
mail settings
Submitter Yuya Nishihara
Date Jan. 13, 2018, 6:53 a.m.
Message ID <7a283960200aedf44612.1515826438@mimosa>
Download mbox | patch
Permalink /patch/26722/
State Accepted
Headers show

Comments

Yuya Nishihara - Jan. 13, 2018, 6:53 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1515823657 -32400
#      Sat Jan 13 15:07:37 2018 +0900
# Node ID 7a283960200aedf4461273ebc0c90f6f677c4a59
# Parent  706aa203b3961372f359df16214b4c10bc8a66fa
fileset: make it robust for bad function calls

Before, it could crash or show cryptic message.

Patch

diff --git a/mercurial/fileset.py b/mercurial/fileset.py
--- a/mercurial/fileset.py
+++ b/mercurial/fileset.py
@@ -99,6 +99,11 @@  def parse(expr):
         raise error.ParseError(_("invalid token"), pos)
     return tree
 
+def getsymbol(x):
+    if x and x[0] == 'symbol':
+        return x[1]
+    raise error.ParseError(_('not a symbol'))
+
 def getstring(x, err):
     if x and (x[0] == 'string' or x[0] == 'symbol'):
         return x[1]
@@ -225,8 +230,8 @@  def clean(mctx, x):
     return [f for f in mctx.subset if f in s]
 
 def func(mctx, a, b):
-    if a[0] == 'symbol' and a[1] in symbols:
-        funcname = a[1]
+    funcname = getsymbol(a)
+    if funcname in symbols:
         enabled = mctx._existingenabled
         mctx._existingenabled = funcname in _existingcallers
         try:
@@ -237,7 +242,7 @@  def func(mctx, a, b):
     keep = lambda fn: getattr(fn, '__doc__', None) is not None
 
     syms = [s for (s, fn) in symbols.items() if keep(fn)]
-    raise error.UnknownIdentifier(a[1], syms)
+    raise error.UnknownIdentifier(funcname, syms)
 
 def getlist(x):
     if not x:
diff --git a/mercurial/minifileset.py b/mercurial/minifileset.py
--- a/mercurial/minifileset.py
+++ b/mercurial/minifileset.py
@@ -56,9 +56,8 @@  def _compile(tree):
             'size': lambda n, s: fileset.sizematcher(tree[2])(s),
         }
 
-        x = tree[1]
-        name = x[1]
-        if x[0] == 'symbol' and name in symbols:
+        name = fileset.getsymbol(tree[1])
+        if name in symbols:
             return symbols[name]
 
         raise error.UnknownIdentifier(name, symbols.keys())
diff --git a/tests/test-fileset.t b/tests/test-fileset.t
--- a/tests/test-fileset.t
+++ b/tests/test-fileset.t
@@ -53,6 +53,22 @@  Test operators and basic patterns
   hg: parse error: invalid \x escape
   [255]
 
+Test invalid syntax
+
+  $ fileset -v '"added"()'
+  (func
+    (string 'added')
+    None)
+  hg: parse error: not a symbol
+  [255]
+  $ fileset -v '()()'
+  (func
+    (group
+      None)
+    None)
+  hg: parse error: not a symbol
+  [255]
+
 Test files status
 
   $ rm a1