Patchwork templater: make sure expression is terminated by '}'

login
register
mail settings
Submitter Yuya Nishihara
Date Jan. 16, 2018, 2:21 p.m.
Message ID <1700a9086c9cd3777a35.1516112511@mimosa>
Download mbox | patch
Permalink /patch/26786/
State Accepted
Headers show

Comments

Yuya Nishihara - Jan. 16, 2018, 2:21 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1516106562 -32400
#      Tue Jan 16 21:42:42 2018 +0900
# Node ID 1700a9086c9cd3777a35f3e9212cdc0826d7b26b
# Parent  20dbbfdf7e253211ccbd31d9c44013f41f6e2461
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.
Augie Fackler - Jan. 17, 2018, 10:27 p.m.
> On Jan 16, 2018, at 09:21, Yuya Nishihara <yuya@tcha.org> wrote:
> 
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1516106562 -32400
> #      Tue Jan 16 21:42:42 2018 +0900
> # Node ID 1700a9086c9cd3777a35f3e9212cdc0826d7b26b
> # Parent  20dbbfdf7e253211ccbd31d9c44013f41f6e2461
> templater: make sure expression is terminated by '}'

queued, thanks

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -184,6 +184,8 @@  def _parsetemplate(tmpl, start, stop, qu
             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:
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
@@ -2756,6 +2756,15 @@  Error on syntax:
   $ 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