classification
Title: test_time fails: strftime('%Y', y) for negative year
Type: behavior Stage: resolved
Components: Tests Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: gregory.p.smith Nosy List: belopolsky, flox, gregory.p.smith, jwilk, martin.panter, miss-islington, python-dev, vstinner
Priority: normal Keywords: buildbot, patch

Created on 2011-11-01 16:52 by flox, last changed 2018-08-27 13:38 by vstinner. This issue is now closed.

Files
File name Uploaded Description Edit
issue13312.patch belopolsky, 2015-03-01 23:07
issue13312.v2.patch martin.panter, 2016-07-14 05:41 review
Pull Requests
URL Status Linked Edit
PR 8912 merged gregory.p.smith, 2018-08-25 00:29
PR 8913 merged miss-islington, 2018-08-25 01:09
PR 8914 merged miss-islington, 2018-08-25 01:09
Messages (19)
msg146793 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2011-11-01 16:52
On builder "AMD64 FreeBSD 8.2 3.x" for the TIME_MINYEAR:


======================================================================
FAIL: test_negative (test.test_time.TestStrftime4dyear)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/home/buildbot/buildarea/3.x.krah-freebsd/build/Lib/test/test_time.py", line 397, in test_negative
    return super().test_negative()
  File "/usr/home/buildbot/buildarea/3.x.krah-freebsd/build/Lib/test/test_time.py", line 425, in test_negative
    self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR))
AssertionError: '2147483648' != '-2147483648'
- 2147483648
+ -2147483648
? +
msg146818 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-11-02 00:28
New changeset 9cb1b85237a9 by Florent Xicluna in branch 'default':
Issue #13312: skip the single failing value for now.
http://hg.python.org/cpython/rev/9cb1b85237a9
msg146822 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-11-02 02:22
New changeset d877d7f3b679 by Florent Xicluna in branch 'default':
Actually, there's more than one failing value. (changeset 9cb1b85237a9, issue #13312).
http://hg.python.org/cpython/rev/d877d7f3b679
msg146828 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2011-11-02 07:09
It fails for very low negative years:
  -2147483648 <= year < -2147481748 
  (-1<<31)    <= year < (-1<<31) + 1900 

Every other value behaves correctly on this FreeBSD buildbot:
 - (-1<<31) + 1900 <= year < (+1<<31) : correctly formatted with '%Z'
 - year <  (-1<<31) : raises OverfowError
 - year >= (+1<<31) : raises OverfowError
msg146829 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2011-11-02 07:11
I mean formatted with '%Y', of course.
msg146830 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-11-02 07:14
New changeset 1a0bfc26af57 by Florent Xicluna in branch 'default':
Issue #13312: skip the failing negative years for now.
http://hg.python.org/cpython/rev/1a0bfc26af57
msg220505 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-06-13 22:00
The failing negative years test is still being skipped.  I'm assuming this was not originally intended.
msg236981 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2015-03-01 21:37
I believe that this can be closed as the test code was changed completely in #17960.
msg236985 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2015-03-01 21:45
Mark,

Issue #17960 ("Clarify the required behaviour of locals()") does not seem to be relevant here.  I think you meant to refer to a changeset, not issue number.  If so please use hash number such as d877d7f3b679.
msg236988 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2015-03-01 22:00
As far as I can tell, the original report was about a test failing due to a system-dependent behavior of time.asctime().  However, since changeset 1e62a0cee092 (see issue #8013), we no longer call system asctime.  I believe the test disabled in 1a0bfc26af57 can now be restored.
msg236989 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2015-03-01 22:03
Sorry should have been #17690.
msg236993 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2015-03-01 22:30
We still have the following in Lib/test/test_time.py:

        # Issue #13312: it may return wrong value for year < TIME_MINYEAR + 1900
        # Skip the value test, but check that no error is raised
        self.yearstr(TIME_MINYEAR)


I reviewed the current time.asctime() code and it does look like it invokes undefined behavior for extremely large negative years.

The problem is that in C struct tm, year is stored as year - 1900 and for year < -2147481748 (= -2**31 + 1900) we trigger an overflow of a signed integer.

Can someone confirm that on AMD64 FreeBSD subtracting 1900 from -2147483648 and then adding it back does not give -2147483648?
msg236994 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2015-03-01 23:07
Attached patch eliminates undefined behavior, but I am not sure fixing this is worth the trouble.
msg270371 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-07-14 05:41
If you enable GCC’s -ftrapv option, the subtraction overflow triggers an abort. Alexander’s patch works around the problem for asctime(), but the problem still exists in other cases, such as:

>>> time.mktime((-2**31 + 1899, *(0,) * 8))
Aborted (core dumped)
[Exit 134]

Attaching a version of the patch without the conflicting whitespace changes.

Why does Python even need to support such extreme time values? It would seem much simpler to raise an exception.
msg324026 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2018-08-25 01:08
New changeset 76be0fffff8b7dbe649ad4821144461800ffb0d0 by Gregory P. Smith in branch 'master':
bpo-13312: Avoid int underflow in time year. (GH-8912)
https://github.com/python/cpython/commit/76be0fffff8b7dbe649ad4821144461800ffb0d0
msg324038 - (view) Author: miss-islington (miss-islington) Date: 2018-08-25 05:53
New changeset d5f017bbd65f37ac53fd3c6c439a53155eef2475 by Miss Islington (bot) in branch '3.7':
bpo-13312: Avoid int underflow in time year. (GH-8912)
https://github.com/python/cpython/commit/d5f017bbd65f37ac53fd3c6c439a53155eef2475
msg324039 - (view) Author: miss-islington (miss-islington) Date: 2018-08-25 05:53
New changeset c47acc2bb1d0a3fb6dda14ced958d272fb2821a6 by Miss Islington (bot) in branch '3.6':
bpo-13312: Avoid int underflow in time year. (GH-8912)
https://github.com/python/cpython/commit/c47acc2bb1d0a3fb6dda14ced958d272fb2821a6
msg324040 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2018-08-25 05:54
OverflowError is now raised for negative values that would trigger a problem and the unittest has been updated to test this.
msg324177 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-08-27 13:38
Note for myself: Python 2.7 is not affected by this bug because it doesn't accept year < 1900.
History
Date User Action Args
2018-08-27 13:38:03vstinnersetmessages: + msg324177
2018-08-25 05:54:42gregory.p.smithsetstatus: open -> closed
resolution: fixed
messages: + msg324040

stage: patch review -> resolved
2018-08-25 05:53:24miss-islingtonsetmessages: + msg324039
2018-08-25 05:53:03miss-islingtonsetnosy: + miss-islington
messages: + msg324038
2018-08-25 01:09:10miss-islingtonsetpull_requests: + pull_request8386
2018-08-25 01:09:04miss-islingtonsetpull_requests: + pull_request8385
2018-08-25 01:08:53gregory.p.smithsetmessages: + msg324026
2018-08-25 00:29:09gregory.p.smithsetstage: needs patch -> patch review
pull_requests: + pull_request8384
2018-08-25 00:19:27gregory.p.smithsetassignee: gregory.p.smith

nosy: + gregory.p.smith
versions: + Python 3.6, Python 3.7, Python 3.8, - Python 3.5
2017-06-10 12:29:59jwilksetnosy: + jwilk
2016-07-15 02:53:36martin.panterlinkissue1621 dependencies
2016-07-14 14:51:38BreamoreBoysetnosy: - BreamoreBoy
2016-07-14 05:41:49martin.pantersetfiles: + issue13312.v2.patch
nosy: + martin.panter
messages: + msg270371

2015-03-01 23:07:52belopolskysetfiles: + issue13312.patch

nosy: + vstinner
messages: + msg236994

keywords: + patch
2015-03-01 22:30:58belopolskysetmessages: + msg236993
2015-03-01 22:03:41BreamoreBoysetmessages: + msg236989
2015-03-01 22:00:00belopolskysetmessages: + msg236988
components: + Tests, - Library (Lib)
versions: + Python 3.5, - Python 3.3
2015-03-01 21:45:10belopolskysetmessages: + msg236985
2015-03-01 21:37:09BreamoreBoysetmessages: + msg236981
2014-06-13 22:00:33BreamoreBoysetnosy: + BreamoreBoy
messages: + msg220505
2011-11-02 07:14:15python-devsetmessages: + msg146830
2011-11-02 07:11:12floxsetmessages: + msg146829
2011-11-02 07:09:26floxsetmessages: - msg146827
2011-11-02 07:09:10floxsetmessages: + msg146828
2011-11-02 07:04:31floxsetmessages: + msg146827
2011-11-02 02:22:56python-devsetmessages: + msg146822
2011-11-02 00:28:36python-devsetnosy: + python-dev
messages: + msg146818
2011-11-01 16:52:38floxcreate