[Patches] Re: bug in PyLong_FromLongLong (PR#324)
Trent Mick
trentm@activestate.com
Thu, 11 May 2000 14:52:41 -0700
[Thomas]
> 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.
On Thu, May 11, 2000 at 09:23:16AM -0400, Guido van Rossum wrote:
> I think he's right. I have no access to a 64-bit machine -- can you
> come up with a fix and test it?
Yup, Thomas is right. Actually I demonstrated the bug and the fix on linux32
and win32. You need a machine where sizeof(long) != sizeof(LONG_LONG).
Please, see a related email on python-dev about being able to test for this
kind of thing.
Patch Discussion:
Correct bounds checking in PyLong_FromLongLong. Previously, it did not check
properly for negative values when looking checking to see if the given value
fit in a long or unsigned long.
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:
diff -c3 /home/trentm/main/contrib/python/dist/src/Objects/longobject.c ./Objects/longobject.c
*** /home/trentm/main/contrib/python/dist/src/Objects/longobject.c Wed May 3 16:44:35 2000
--- ./Objects/longobject.c Thu May 11 14:22:34 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