#1270 unsigned long input can overflow

None
closed
nobody
python (259)
5
2013-02-23
2012-09-10
Michel Zou
No

Hi,

For python3, the default unsigned long input typemap can lead to overflow if the value is exceeds the long type max (but not unsigned long).
I tried with swig 2.0.8, with and without the -py3 flag.

class Foo
{
public:
static const unsigned long ALL;
void doStuff(unsigned long bitField);
};

If Foo::ALL is the maximum of an unsigned long in the wrapped library:
const unsigned long Foo::ALL = ~0;

And we call the function that takes an ulong as argument:
foo.doStuff(Foo.ALL)

We get the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "example.py", line 88, in show
def doStuff(self, *args) -> "void" : return _example.Foo_doStuff(self, *args)
OverflowError: in method 'Foo_doStuff', argument 2 of type 'unsigned long'

Now let's look at the input typemap for our unsigned long argument:
ecode2 = SWIG_AsVal_unsigned_SS_long(obj1, &val2);
if (!SWIG_IsOK(ecode2)) {
SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Foo_doStuff" "', argument " "2"" of type '" "unsigned long""'");
}
arg2 = static_cast< unsigned long >(val2);

It uses the default 'input typemap' function SWIG_AsVal_unsigned_SS_long:
if (PyInt_Check(obj)) {
long v = PyInt_AsLong(obj);
if (v >= 0) {
if (val) *val = v;
return SWIG_OK;
} else {
return SWIG_OverflowError;
}
} else if (PyLong_Check(obj)) {
unsigned long v = PyLong_AsUnsignedLong(obj);
if (!PyErr_Occurred()) {
if (val) *val = v;
return SWIG_OK;
} else {
PyErr_Clear();
}
}
...

We see that there's a different code path between if PyInt_Check or if PyLong_Check.
but in python3 there's only the PyLong type,
and PyInt_Check is mapped through a macro definition to PyLong_Check:
#define PyInt_Check(x) PyLong_Check(x)

so the conversion function used is PyInt_AsLong, which also maps to PyLong_AsLong
and when it tries to convert to long it obviously fails.

Regards,
x.

Discussion

  • Michel Zou
    Michel Zou
    2012-09-24

    Hi,

    I noticed this bug was already reported: 3544716.

    X;

     
  • Michel Zou
    Michel Zou
    2013-02-22

    Hey,

    It's been fixed too!

    Thanks.

     
  • William Fulton
    William Fulton
    2013-02-23

    • status: open --> closed
    • milestone: -->
     
  • William Fulton
    William Fulton
    2013-02-23

    Closing as reported as fixed.