changeset 35594:468d7a1f6633

revsetlang: catch invalid value passed to formatspec() The scope of AttributeError is narrowed because it's more likely to be triggered by mistake.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 01 Apr 2017 17:44:07 +0900
parents 850cd045b1df
children 91201737d07a
files mercurial/revsetlang.py tests/test-command-template.t
diffstat 2 files changed, 34 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/revsetlang.py	Sat Apr 01 17:28:28 2017 +0900
+++ b/mercurial/revsetlang.py	Sat Apr 01 17:44:07 2017 +0900
@@ -599,7 +599,10 @@
         elif c == 'n':
             return _quote(node.hex(arg))
         elif c == 'b':
-            return _quote(arg.branch())
+            try:
+                return _quote(arg.branch())
+            except AttributeError:
+                raise TypeError
         raise error.ParseError(_('unexpected revspec format character %s') % c)
 
     def listexp(s, t):
@@ -615,7 +618,10 @@
         elif t == 'n':
             return "_hexlist('%s')" % "\0".join(node.hex(a) for a in s)
         elif t == 'b':
-            return "_list('%s')" % "\0".join(a.branch() for a in s)
+            try:
+                return "_list('%s')" % "\0".join(a.branch() for a in s)
+            except AttributeError:
+                raise TypeError
 
         m = l // 2
         return '(%s or %s)' % (listexp(s[:m], t), listexp(s[m:], t))
@@ -651,9 +657,15 @@
                 d = expr[pos]
             except IndexError:
                 raise error.ParseError(_('incomplete revspec format character'))
-            ret.append(listexp(list(arg), d))
+            try:
+                ret.append(listexp(list(arg), d))
+            except (TypeError, ValueError):
+                raise error.ParseError(_('invalid argument for revspec'))
         else:
-            ret.append(argtype(d, arg))
+            try:
+                ret.append(argtype(d, arg))
+            except (TypeError, ValueError):
+                raise error.ParseError(_('invalid argument for revspec'))
         pos += 1
 
     try:
--- a/tests/test-command-template.t	Sat Apr 01 17:28:28 2017 +0900
+++ b/tests/test-command-template.t	Sat Apr 01 17:44:07 2017 +0900
@@ -4103,6 +4103,24 @@
   $ hg log -T '{revset("%l", 0)}\n'
   hg: parse error: incomplete revspec format character
   [255]
+  $ hg log -T '{revset("%d", 'foo')}\n'
+  hg: parse error: invalid argument for revspec
+  [255]
+  $ hg log -T '{revset("%ld", files)}\n'
+  hg: parse error: invalid argument for revspec
+  [255]
+  $ hg log -T '{revset("%ls", 0)}\n'
+  hg: parse error: invalid argument for revspec
+  [255]
+  $ hg log -T '{revset("%b", 'foo')}\n'
+  hg: parse error: invalid argument for revspec
+  [255]
+  $ hg log -T '{revset("%lb", files)}\n'
+  hg: parse error: invalid argument for revspec
+  [255]
+  $ hg log -T '{revset("%r", 0)}\n'
+  hg: parse error: invalid argument for revspec
+  [255]
 
 Test files function