Patchwork [STABLE] templater: fix crash by passing invalid object to date() function

login
register
mail settings
Submitter Yuya Nishihara
Date May 3, 2015, 9:18 a.m.
Message ID <9e38782c73b331b3ffce.1430644684@mimosa>
Download mbox | patch
Permalink /patch/8862/
State Accepted
Commit 09124cce913f633e8ecafa6580751aaab3ddd4e2
Headers show

Comments

Yuya Nishihara - May 3, 2015, 9:18 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1430641994 -32400
#      Sun May 03 17:33:14 2015 +0900
# Branch stable
# Node ID 9e38782c73b331b3ffce5fa78afb311351cd81a6
# Parent  c48850339988bb8a5048c93ce1285304b96e0887
templater: fix crash by passing invalid object to date() function

"date information" is somewhat obscure, but we call it that way in
templatekw.showdate().
Matt Mackall - May 4, 2015, 4:52 p.m.
On Sun, 2015-05-03 at 18:18 +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1430641994 -32400
> #      Sun May 03 17:33:14 2015 +0900
> # Branch stable
> # Node ID 9e38782c73b331b3ffce5fa78afb311351cd81a6
> # Parent  c48850339988bb8a5048c93ce1285304b96e0887
> templater: fix crash by passing invalid object to date() function
>
> "date information" is somewhat obscure, but we call it that way in
> templatekw.showdate().

Hmm, ok. Queued for stable, thanks.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -226,10 +226,17 @@  def date(context, mapping, args):
         raise error.ParseError(_("date expects one or two arguments"))
 
     date = args[0][0](context, mapping, args[0][1])
+    fmt = None
     if len(args) == 2:
         fmt = stringify(args[1][0](context, mapping, args[1][1]))
-        return util.datestr(date, fmt)
-    return util.datestr(date)
+    try:
+        if fmt is None:
+            return util.datestr(date)
+        else:
+            return util.datestr(date, fmt)
+    except (TypeError, ValueError):
+        # i18n: "date" is a keyword
+        raise error.ParseError(_("date expects a date information"))
 
 def diff(context, mapping, args):
     """:diff([includepattern [, excludepattern]]): Show a diff, optionally
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
@@ -2236,6 +2236,12 @@  Test date format:
   date: 70 01 01 01 +0000
   date: 70 01 01 00 +0000
 
+Test invalid date:
+
+  $ hg log -R latesttag -T '{date(rev)}\n'
+  hg: parse error: date expects a date information
+  [255]
+
 Test string escaping:
 
   $ hg log -R latesttag -r 0 --template '>\n<>\\n<{if(rev, "[>\n<>\\n<]")}>\n<>\\n<\n'