[PATCH STABLE] templater: catch exception caused by template ends with backslash (issue4798)

Yuya Nishihara yuya at tcha.org
Fri Aug 28 14:54:35 UTC 2015


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1440733316 -32400
#      Fri Aug 28 12:41:56 2015 +0900
# Branch stable
# Node ID 534ed63b7cf44765e68ef7f358293fb4cafe82ce
# Parent  b930d4ef7739440161c3ab7e3c831bfdb541c81e
templater: catch exception caused by template ends with backslash (issue4798)

This patch adds try-except block only to the "n < 0" case. The other string-
escapes should be safe because they are processed after splitting template
fragments.

The error message is still unclear. Perhaps we'll want to provide a context
of ParseError.

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -147,7 +147,11 @@ def _parsetemplate(tmpl, start, stop, qu
         n = min((tmpl.find(c, pos, stop) for c in sepchars),
                 key=lambda n: (n < 0, n))
         if n < 0:
-            parsed.append(('string', tmpl[pos:stop].decode('string-escape')))
+            try:
+                parsed.append(('string',
+                               tmpl[pos:stop].decode('string-escape')))
+            except ValueError: # unbalanced escapes
+                raise error.ParseError(_("syntax error"), pos)
             pos = stop
             break
         c = tmpl[n]
diff --git a/tests/test-schemes.t b/tests/test-schemes.t
--- a/tests/test-schemes.t
+++ b/tests/test-schemes.t
@@ -9,6 +9,8 @@
   > parts = http://{1}:$HGPORT/
   > z = file:\$PWD/
   > EOF
+  $ printf 'trslash = c:\\' >> $HGRCPATH
+
   $ hg init test
   $ cd test
   $ echo a > a
@@ -52,6 +54,12 @@ check that paths are expanded
   no changes found
   [1]
 
+common mistake of template syntax, trailing slash (issue4798)
+
+  $ hg id trslash://
+  hg: parse error at 0: syntax error
+  [255]
+
 errors
 
   $ cat errors.log


More information about the Mercurial-devel mailing list