time.mktime memory access violation bug

Peter Otten __peter__ at web.de
Thu Nov 20 15:01:18 EST 2003


Bengt Richter wrote:

>  Python 2.3.2 (#49, Oct  2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on
>  win32 Type "help", "copyright", "credits" or "license" for more
>  information.
>  >>> import time
>  >>> time.mktime((1969, 12, 31, 17, 0, 0, 0, 0, 0))
>  3600.0
>  >>> time.mktime((1969, 12, 31, 17, 0, 0, 0, 0, 1))
>  0.0
>  >>> time.mktime((1969, 12, 31, 16, 0, 0, 0, 0, 1))
>  [crash with popup]

More data points:

import time
n = 1000000
for i in range(-n, n):
    lt = time.localtime(i)
    try:
        time.mktime(lt)
    except OverflowError:
        print "Cannot cope with %r %r" % (i, lt)

Python 2.3.2 (#1, Oct 21 2003, 10:03:19)
[GCC 3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import check_mktime
Cannot cope with -1 (1970, 1, 1, 0, 59, 59, 3, 1, 0)
>>>

Now omitting localtime():

>>> time.mktime((1970, 1, 1, 0, 59, 59, 3, 1, 0))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OverflowError: mktime argument out of range
>>>

So, on Linux:

- times < 0 seem to be OK.
- but -1 is used (abused?) as an error value

Attempts to provoke an OverflowError with the above technique for the lower
limit failed due to the following behaviour of localtime():

>>> time.localtime(-3600*24*365*80)
(1901, 12, 13, 21, 45, 52, 4, 347, 0)

I. e. always the same for low values, so, "guessing" the lower limit:

>>> time.localtime(-2**31)
(1901, 12, 13, 21, 45, 52, 4, 347, 0)
>>> time.localtime(-2**31+1)
(1901, 12, 13, 21, 45, 53, 4, 347, 0)
>>> time.localtime(-2**31+2)
(1901, 12, 13, 21, 45, 54, 4, 347, 0)
>>>

But mktime() seems OK:

>>> time.mktime((1901, 12, 13, 21, 45, 51, 4, 347, 0))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OverflowError: mktime argument out of range
>>>

Conclusion: The fatal error is probably due to NT's mktime() implementation.
Times before (1901, ...) should raise an Overflow error. I don't like the
-1 special value, but I am not sure how the error value of the underlying C
library and one second before the Unix epoch could be disambiguated.

Peter





More information about the Python-list mailing list