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

login
register
mail settings
Submitter Yuya Nishihara
Date Aug. 28, 2015, 2:54 p.m.
Message ID <534ed63b7cf44765e68e.1440773675@mimosa>
Download mbox | patch
Permalink /patch/10314/
State Changes Requested
Headers show

Comments

Yuya Nishihara - Aug. 28, 2015, 2:54 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@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.
Matt Mackall - Aug. 28, 2015, 10:35 p.m.
On Fri, 2015-08-28 at 23:54 +0900, Yuya Nishihara wrote:
> +common mistake of template syntax, trailing slash (issue4798)
> +
> +  $ hg id trslash://
> +  hg: parse error at 0: syntax error
> +  [255]

I don't think this is an improvement. Before this would complain about a
trailing backslash (but not say why that was a problem: because it's not
a valid template), now this has an even less helpful "syntax error" and
points to position 0 (where nothing is amiss). It does avoid spewing a
backtrace though!

I think changing "syntax error" to "string escaping error in template"
might be better.

Might be more clear to test against:

$ hg log -T "\\"

Also: you write slash instead of backslash a few places.

Patch

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