[Numpy-discussion] fwrite() failure in PyArray_ToFile

Sturla Molden sturla@molden...
Fri Sep 4 22:22:20 CDT 2009


David Warde-Farley skrev:
>> The odd values might be from the format code in the error message:
>>
>>                PyErr_Format(PyExc_ValueError,
>>                        "%ld requested and %ld written",
>>                        (long) size, (long) n);
>>     
>
> Yes, I saw that. My C is rusty, but wouldn't the cast take care of it?  
> n is of type size_t, which is pretty big, and a cast to long shouldn't  
> be an issue. And if (hopefully) PyErr_Format is correct...
>   
A long is (usually) 32 bit, even on 64 bit systems. This means that size 
is casted to an integer between - 2**31 and 2*31 - 1. As 2**31 bytes are 
2 GB, the expression "(long) size" will overflow if a write of 2GB or 
more failed. Thus you get some bogus numbers in the formatted message. 
There is thus a bug in the call to PyErr_Format, as it only works 
reliably on 32 bits systems.

S.M.






More information about the NumPy-Discussion mailing list