[tor-commits] [tor/master] Clamp (some) years supplied by the system to 1 CE

nickm at torproject.org nickm at torproject.org
Tue Oct 21 17:22:55 UTC 2014


commit d7b13543e2305de38f7e17e7bcd1e5174fd89152
Author: teor <teor2345 at gmail.com>
Date:   Mon Oct 20 02:47:31 2014 +1100

    Clamp (some) years supplied by the system to 1 CE
    
    Clamp year values returned by system localtime(_r) and
    gmtime(_r) to year 1. This ensures tor can read any
    values it might write out.
    
    Fixes bug 13476.
---
 changes/bug13476-improve-time-handling |    3 +++
 src/common/compat.c                    |   12 +++++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/changes/bug13476-improve-time-handling b/changes/bug13476-improve-time-handling
index 68dc3e6..1fe60c6 100644
--- a/changes/bug13476-improve-time-handling
+++ b/changes/bug13476-improve-time-handling
@@ -10,3 +10,6 @@
       for validity, taking leap years into account.
       Improves HTTP header validation.
       Implemented with bug 13476.
+    - Clamp year values returned by system localtime(_r) and gmtime(_r)
+      to year 1. This ensures tor can read any values it might write out.
+      Fixes bug 13476.
diff --git a/src/common/compat.c b/src/common/compat.c
index f8b1d15..b6fdb1a 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -2770,7 +2770,9 @@ correct_tm(int islocal, const time_t *timep, struct tm *resultbuf,
   const char *outcome;
 
   if (PREDICT_LIKELY(r)) {
-    if (r->tm_year > 8099) { /* We can't strftime dates after 9999 CE. */
+    /* We can't strftime dates after 9999 CE, and we want to avoid dates
+     * before 1 CE (avoiding the year 0 issue and negative years). */
+    if (r->tm_year > 8099) {
       r->tm_year = 8099;
       r->tm_mon = 11;
       r->tm_mday = 31;
@@ -2778,6 +2780,14 @@ correct_tm(int islocal, const time_t *timep, struct tm *resultbuf,
       r->tm_hour = 23;
       r->tm_min = 59;
       r->tm_sec = 59;
+    } else if (r->tm_year < (1-1900)) {
+      r->tm_year = (1-1900);
+      r->tm_mon = 0;
+      r->tm_mday = 1;
+      r->tm_yday = 0;
+      r->tm_hour = 0;
+      r->tm_min = 0;
+      r->tm_sec = 0;
     }
     return r;
   }





More information about the tor-commits mailing list