]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
parser: better date parsing
authorDaniel Axtens <dja@axtens.net>
Wed, 28 Jun 2017 07:48:48 +0000 (17:48 +1000)
committerStephen Finucane <stephen@that.guru>
Wed, 28 Jun 2017 20:41:28 +0000 (21:41 +0100)
It turns out that there is a lot that can go wrong in parsing a
date. OverflowError, ValueError and OSError have all been observed.

If these go wrong, substitute the current datetime.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Stephen Finucane <stephen@that.guru>
Reviewed-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com>
patchwork/parser.py

index 47407cbe22d859c452137428ac0f57e3043498b0..8a07a69822f02d8988005c5dc14bef142c5f6e37 100644 (file)
@@ -344,10 +344,24 @@ def find_date(mail):
     h = clean_header(mail.get('Date', ''))
     if not h:
         return datetime.datetime.utcnow()
+
     t = parsedate_tz(h)
     if not t:
         return datetime.datetime.utcnow()
-    return datetime.datetime.utcfromtimestamp(mktime_tz(t))
+
+    try:
+        d = datetime.datetime.utcfromtimestamp(mktime_tz(t))
+    except (OverflowError, ValueError, OSError):
+        # If you have a date like:
+        # - Date: Wed, 4 Jun 207777777777777777777714 17:50:46 0
+        #   -> OverflowError
+        # - Date:, 11 Sep 2016 23:22:904070804030804 +0100
+        #   -> ValueError
+        # - Date:, 11 Sep 2016 407080403080105:04 +0100
+        #   -> OSError (Python 3)
+        d = datetime.datetime.utcnow()
+
+    return d
 
 
 def find_headers(mail):