cpython/Objects
Guido van Rossum f753181272 Subject: Buglet in PyLong_AsLong
From: "Tim Peters" <tim_one@email.msn.com>
To: "Guido van Rossum" <guido@CNRI.Reston.VA.US>
Date: Sat, 23 May 1998 21:45:53 -0400

Guido, the overflow checking in PyLong_AsLong is off a little:

1) If the C in use sign-extends right shifts on signed longs, there's a
spurious overflow error when converting the most-negative int:

Python 1.5.1 (#0, Apr 13 1998, 20:22:04) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> x = -1L << 31
>>> x
-2147483648L
>>> int(x)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
OverflowError: long int too long to convert
>>>

2) If C does not sign-extend, some genuine overflows won't be caught.

The attached should repair both, and, because I installed a new disk and a C
compiler today, it's even been compiled this time <wink>.

Python 1.5.1 (#0, May 23 1998, 20:24:58) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> x = -1L << 31
>>> x
-2147483648L
>>> int(x)
-2147483648
>>> int(-x)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
OverflowError: long int too long to convert
>>> int(-x-1)
2147483647
>>> int(x-1)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
OverflowError: long int too long to convert
>>>

end-casing-ly y'rs  - tim
1998-05-26 14:33:37 +00:00
..
Makefile.in New build procedure. 1997-07-19 19:39:57 +00:00
abstract.c Address some gcc -Wall warnings (e.g. include <ctype.h>). 1998-05-22 15:23:36 +00:00
classobject.c Remove a redundant statement from halfbinop(). 1998-05-13 22:02:16 +00:00
cobject.c New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and 1997-10-21 19:48:35 +00:00
complexobject.c Add check to conjugate() that there are no excess arguments. 1998-05-07 16:29:10 +00:00
dictobject.c Make sure that PyDict_GetItem[String]() *never* raises an exception. 1998-05-14 01:00:51 +00:00
fileobject.c Ugly band-aid to work around a bug in Linux ftell(). 1998-05-05 22:21:35 +00:00
floatobject.c Hack suggested by Matthias Klose to pull in all relevant entry points 1997-10-31 17:00:30 +00:00
frameobject.c Since PyDict_GetItem() can't raise an exception any more, there's no 1998-05-14 01:49:48 +00:00
funcobject.c Make function objects somewhat mutable -- the members func_code, 1998-05-22 00:55:34 +00:00
intobject.c Added _Fini() routines to free up some memory 1997-08-05 02:16:08 +00:00
listobject.c Tim's quicksort on May 13. 1998-05-13 21:21:24 +00:00
longobject.c Subject: Buglet in PyLong_AsLong 1998-05-26 14:33:37 +00:00
methodobject.c Added separate free list for cfunction (builtin method) objects, for a 1997-08-05 02:11:41 +00:00
moduleobject.c Add internal routine _PyModule_Clear(), which does approximately what 1998-02-19 20:51:52 +00:00
object.c In PyObject_IsTrue(), don't call function pointers that are NULL 1998-05-22 00:53:24 +00:00
rangeobject.c Quickly renamed the last directory. 1997-05-02 03:12:38 +00:00
sliceobject.c Ellipses -> Ellipsis rename (the dictionary really says that it should 1996-10-11 16:25:41 +00:00
stringobject.c Make new gcc -Wall happy 1998-04-10 22:16:39 +00:00
tupleobject.c PyTuple_SetItem should require that the tuple's refcnt is one! 1997-08-17 16:25:45 +00:00
typeobject.c American spelling in doc string. 1997-06-02 14:43:07 +00:00
xxobject.c New permission notice, includes CNRI. 1996-10-25 14:44:06 +00:00