Patchwork [4,of,7] revsetlang: catch invalid value passed to formatspec()

login
register
mail settings
Submitter Yuya Nishihara
Date Jan. 8, 2018, 1:36 p.m.
Message ID <ecabe925e5997399ca48.1515418587@mimosa>
Download mbox | patch
Permalink /patch/26613/
State Accepted
Headers show

Comments

Yuya Nishihara - Jan. 8, 2018, 1:36 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1491036247 -32400
#      Sat Apr 01 17:44:07 2017 +0900
# Node ID ecabe925e5997399ca48624b73bbde33a6e9c1c5
# Parent  32eb47c4665d7353ed4f1f403a6f061344213152
revsetlang: catch invalid value passed to formatspec()

The scope of AttributeError is narrowed because it's more likely to be
triggered by mistake.

Patch

diff --git a/mercurial/revsetlang.py b/mercurial/revsetlang.py
--- a/mercurial/revsetlang.py
+++ b/mercurial/revsetlang.py
@@ -599,7 +599,10 @@  def formatspec(expr, *args):
         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 @@  def formatspec(expr, *args):
         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 @@  def formatspec(expr, *args):
                 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:
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -4103,6 +4103,24 @@  Invalid arguments passed to revset()
   $ 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