#805 SWIG_AsVal_int doesn't report overflow correctly

open
Marcelo Matus
python (259)
5
2012-12-21
2007-03-28
Michael Taylor
No

I am upgrading my software (Maya 3D) to use SWIG 1.3.31 from an older version. Some of my unit tests are failing after the upgrade because SWIG is now throwing RuntimeErrors where it used to throw OverflowErrors. SWIG should be throwing Overflow errors my test.

The problem is in SWIG_AsVal_int. It is doing this:

---
long v;
int res = SWIG_AsVal_long (obj, &v);
if (SWIG_IsOK(res)) {
if ((v < INT_MIN || v > INT_MAX)) {
return SWIG_OverflowError;
} else {
---

The problem is that on 32-bit systems, and Windows 64-bit, a long and an int are the same type. So, we never get to the overflow check because the value in "res" is going to be false if the Python number is to big.

And SWIG_AsVal_long does not correctly report the overflow either.

The number I am using in my test is 4000000000, which will fit in an unsigned int, but not a regular int...

Cheers!

Discussion