[Patches] Re: bug in PyLong_FromLongLong (PR#324)

Trent Mick trentm@ActiveState.com
Thu, 1 Jun 2000 11:09:57 -0700


This is a revival post. Guido, you asked me to submit a patch for this a
while back and I did, but the patch got lost in the fun of recent times. Here
it is again.


Discussion:

This patch fixes bug PR#324.

[Thomas Malik]
> there's a bug in PyLong_FromLongLong, resulting in truncation of negative 64 bi
> t
> 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 i
> n
> 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.

This patch correct bounds checking in PyLong_FromLongLong. Currently, it does
not check properly for negative values when checking to see if the incoming
value fits in a long or unsigned long. This results in possible silent
truncation of the value for very large negative values.


Legal:

I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims").  To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation.  I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.



Patch:

*** /home/trentm/main/contrib/python/dist/src/Objects/longobject.c	Thu Jun  1 00:13:40 2000
--- /home/trentm/main/Apps/Perlium/Python/dist/src/Objects/longobject.c	Wed May 31 23:54:19 2000
***************
*** 355,364 ****
  	/* In case the compiler is faking it. */
  	return PyLong_FromLong( (long)ival );
  #else
! 	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 {
--- 355,364 ----
  	/* In case the compiler is faking it. */
  	return PyLong_FromLong( (long)ival );
  #else
! 	if ((LONG_LONG)LONG_MIN <= ival && ival <= (LONG_LONG)LONG_MAX) {
  		return PyLong_FromLong( (long)ival );
  	}
! 	else if (0 <= ival && ival <= (unsigned LONG_LONG)ULONG_MAX) {
  		return PyLong_FromUnsignedLong( (unsigned long)ival );
  	}
  	else {

-- 
Trent Mick
trentm@activestate.com