fileset: make it robust for bad function calls
authorYuya Nishihara <yuya@tcha.org>
Sat, 13 Jan 2018 15:07:37 +0900
changeset 35691 735f47b41521
parent 35690 3e394e0558d7
child 35692 a62b08f6626b
fileset: make it robust for bad function calls Before, it could crash or show cryptic message.
mercurial/fileset.py
mercurial/minifileset.py
tests/test-fileset.t
--- a/mercurial/fileset.py	Thu Jan 04 14:20:58 2018 +0900
+++ b/mercurial/fileset.py	Sat Jan 13 15:07:37 2018 +0900
@@ -99,6 +99,11 @@
         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 @@
     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 @@
     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:
--- a/mercurial/minifileset.py	Thu Jan 04 14:20:58 2018 +0900
+++ b/mercurial/minifileset.py	Sat Jan 13 15:07:37 2018 +0900
@@ -56,9 +56,8 @@
             '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())
--- a/tests/test-fileset.t	Thu Jan 04 14:20:58 2018 +0900
+++ b/tests/test-fileset.t	Sat Jan 13 15:07:37 2018 +0900
@@ -53,6 +53,22 @@
   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