[Python-bugs-list] bug in PyLong_FromLongLong (PR#324)

Thomas.Malik@t-online.de Thomas.Malik@t-online.de
Wed, 10 May 2000 15:37:30 -0400 (EDT)


Full_Name: Thomas Malik
Version: 1.5.2
OS: all
Submission from: p3e9ed447.dip.t-dialin.net (62.158.212.71)


there's a bug in PyLong_FromLongLong, resulting in truncation of negative 64 bit
integers. PyLong_FromLongLong starts with: 
	if( ival <= (LONG_LONG)LONG_MAX ) {
		return PyLong_FromLong( (long)ival );
	}
	else if( ival <= (unsigned LONG_LONG)ULONG_MAX ) {
		return PyLong_FromUnsignedLong( (unsigned long)ival );
	}
	else {
             ....

Now, if ival is smaller than -LONG_MAX, it falls outside the long integer range
(being a 64 bit negative integer), but gets handled by the first if-then-case in
above code ('cause it is, of course, smaller than LONG_MAX). This results in
truncation of the 64 bit negative integer to a more or less arbitrary 32 bit
number. The way to fix it is to compare the absolute value of imax against
LONG_MAX in the first condition. The second condition (ULONG_MAX) must, at
least, check wether ival is positive.