templater: make sure expression is terminated by '}'
authorYuya Nishihara <yuya@tcha.org>
Tue, 16 Jan 2018 21:42:42 +0900
changeset 35680 077ee15b8493
parent 35679 5a2d505a9174
child 35681 e29823c6d3e8
templater: make sure expression is terminated by '}' This can't be ensured by the tokenize() generator since the parser stop consuming tokens once it reached an end of an operation. Spotted by Matt Harbison.
mercurial/templater.py
tests/test-command-template.t
--- a/mercurial/templater.py	Fri Jul 14 19:59:10 2017 +0200
+++ b/mercurial/templater.py	Tue Jan 16 21:42:42 2018 +0900
@@ -184,6 +184,8 @@
             return parsed, n + 1
 
         parseres, pos = p.parse(tokenize(tmpl, n + 1, stop, '}'))
+        if not tmpl.endswith('}', n + 1, pos):
+            raise error.ParseError(_("invalid token"), pos)
         parsed.append(parseres)
 
     if quote:
--- a/tests/test-command-template.t	Fri Jul 14 19:59:10 2017 +0200
+++ b/tests/test-command-template.t	Tue Jan 16 21:42:42 2018 +0900
@@ -2756,6 +2756,15 @@
   $ hg log -T '{date'
   hg: parse error at 1: unterminated template expansion
   [255]
+  $ hg log -T '{date(}'
+  hg: parse error at 7: not a prefix: end
+  [255]
+  $ hg log -T '{date)}'
+  hg: parse error at 5: invalid token
+  [255]
+  $ hg log -T '{date date}'
+  hg: parse error at 6: invalid token
+  [255]
 
 Behind the scenes, this will throw TypeError