diff options
author | peter <peter@FreeBSD.org> | 2004-08-24 00:15:37 +0000 |
---|---|---|
committer | peter <peter@FreeBSD.org> | 2004-08-24 00:15:37 +0000 |
commit | f0aa809d4494f811a9973378c9b4cf3c477f8979 (patch) | |
tree | 718e1bccef2b0a5b4b39e8a60628dbe977f7939d /lib/libc/stdtime | |
parent | 5c1c6f267416745ed6723b5867a4fc911dff48d1 (diff) | |
download | FreeBSD-src-f0aa809d4494f811a9973378c9b4cf3c477f8979.zip FreeBSD-src-f0aa809d4494f811a9973378c9b4cf3c477f8979.tar.gz |
struct tm.tm_year is listed as 'years since 1900', and is signed. On
64 bit systems, years roughly -2^31 through 2^31 can be represented in
time_t without any trouble. 32 bit time_t systems only range from
roughly 1902 through 2038. As a consequence, none of the date munging
code for all the various calendar tweaks before then is present. There
are other problems including the fact that there was no 'year zero' and
so on. So rather than get excited about trying to figure out when the
calendar jumped by two weeks etc, simply disallow negative (ie: prior to
1900) years.
This happens to have an important side effect. If you bzero a 'struct
tm', it corresponds to 'Jan 0, 1900, 00:00 GMT'. This happens to be
representable (after canonification) in 64 bit time_t space. Zero tm
structs are generally an error and mktime normally returns -1 for them.
Interestingly, it tries to canonify the 'jan 0' to 'dec 31, 1899', ie:
year -1. This conveniently trips the negative year test above, which
means we can trivially detect the null 'tm' struct.
This actually tripped up code at work. :-/ (Don't ask)
Diffstat (limited to 'lib/libc/stdtime')
-rw-r--r-- | lib/libc/stdtime/localtime.c | 3 |
1 files changed, 3 insertions, 0 deletions
diff --git a/lib/libc/stdtime/localtime.c b/lib/libc/stdtime/localtime.c index 9efff47..d7b5a36 100644 --- a/lib/libc/stdtime/localtime.c +++ b/lib/libc/stdtime/localtime.c @@ -1487,6 +1487,9 @@ const int do_norm_secs; } if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE)) return WRONG; + /* Don't go below 1900 for POLA */ + if (yourtm.tm_year < 0) + return WRONG; if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN) saved_seconds = 0; else if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) { |