[PATCH STABLE] do not crash on or store negative timestamps (issue2513)

Adrian Buehlmann adrian at cadifra.com
Wed Nov 24 11:38:15 CST 2010


On 2010-11-22 23:13, Benjamin Pollack wrote:
> # HG changeset patch
> # User Benjamin Pollack <benjamin at bitquabit.com>
> # Date 1290463597 28800
> # Node ID 72557ca9f1d3405a88ffe83960da6d8fedbb7414
> # Parent  77aa74fe0e0b92945ec793e9e7af02fdda36ca7c
> do not crash on or store negative timestamps (issue2513)
> 
> Mercurial will now reject timestamps preceding the Unix epoch. When
> encountering them in existing repos, it will return January 1, 1970.
> 

[..]

> @@ -1072,6 +1074,8 @@ def strdate(string, format, defaults=[])
>          offset = unixtime - localunixtime
>      else:
>          unixtime = localunixtime + offset
> +    if unixtime < 0:
> +        raise ValueError
>      return unixtime, offset
>  
>  def parsedate(date, formats=None, defaults=None):

I've started working on a separate patch for this part as well.

FWIW, the above patch hunk doesn't seem to catch

  $ hg ci -m1 -d '-645465465 3600'

Looks like strdate() is only called in parsedate(), where ValueError is
catched and ignored.

We probably need to insert a check near the end of parsedate():

@@ -1116,6 +1125,8 @@ def parsedate(date, formats=None, defaul
     # to UTC+14
     if abs(when) > 0x7fffffff:
         raise Abort(_('date exceeds 32 bits: %d') % when)
+    if when < 0:
+        raise Abort(_('negative date value: %d') % when)
     if offset < -50400 or offset > 43200:
         raise Abort(_('impossible time zone offset: %d') % offset)
     return when, offset

and we possibly need to catch weird clocks in makedate():

@@ -1018,7 +1018,10 @@ def makedate():
         tz = time.altzone
     else:
         tz = time.timezone
-    return time.mktime(lt), tz
+    t = time.mktime(lt)
+    if t < 0:
+        raise Abort(_('negative timestamp: %d') % t)
+    return t, tz


anywhere else, too?


More information about the Mercurial-devel mailing list