Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#1201 not handling 0xffffffff correctly as UINT32 input to C func

pending-fixed
nobody
python (258)
5
2011-11-05
2011-10-27
Hongbin Wei
No

I have a C function “void Uint32_Test(UINT32 num)”. when I pass in a 0xffffffff as the argument from Python to the wrapper function, I get “overflowerror : in method ‘Uint32_Test’, argument 1 of type ‘UINT32’”.

Looking into the wrappers, I found that the error is generated at the lines of following method marked by "<==". Obviously 0xffffffff is being treated as a singed value although it's expected to be an unsigned.

SWIGINTERN int
SWIG_AsVal_unsigned_SS_long (PyObject *obj, unsigned long *val)
{
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();
}
}
#ifdef SWIG_PYTHON_CAST_MODE
{
int dispatch = 0;
unsigned long v = PyLong_AsUnsignedLong(obj);
if (!PyErr_Occurred()) {
if (val) *val = v;
return SWIG_AddCast(SWIG_OK);
} else {
PyErr_Clear();
}
if (!dispatch) {
double d;
int res = SWIG_AddCast(SWIG_AsVal_double (obj,&d));
if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, 0, ULONG_MAX)) {
if (val) *val = (unsigned long)(d);
return res;
}
}
}
#endif
return SWIG_TypeError;
}

Discussion

  • szager
    szager
    2011-10-27

    I have a proposed fix for this, but I'm unable to reproduce the problem. Can you please provide some details about the system you're running on:

    - Please send the output of this command:

    python -c 'import platform; print platform.architecture(); print platform.python_version()'

    - If you're on a linux system, please send the output of this command:

    uname -a

    Thanks,

    Stefan

     
  • Hongbin Wei
    Hongbin Wei
    2011-10-27

    Thanks Stefan. Here's the output of the commands.
    >>> platform.architecture()
    ('32bit', 'WinddowsPE')
    >>> platform.python_version()
    '3.2.0'

     
  • szager
    szager
    2011-10-29

    I can't reproduce this with 64-bit python, and I don't have a 32-bit python installation to try it with. Can you please try applying the patch attached to this bug, and let me know if it fixes the problem?

     
  • Hongbin Wei
    Hongbin Wei
    2011-10-31

    I looked at the patch.
    1) On my system, it's SWIGINTERN int SWIG_AsVal_unsigned_SS_long (PyObject *obj, unsigned long *val) that got generated, called and triggered the failure, not SWIG_AsVal_dec(unsigned long)(PyObject *obj, unsigned long *val).
    2) I merged the patch to SWIGINTERN int SWIG_AsVal_unsigned_SS_long (PyObject *obj, unsigned long *val), and replacing PyInt_AsUnsignedLongMask(obj) with PyLong_AsUnsignedLongMask(obj) since the PyInt_AsUnsignedLongMask(obj) is not defined. And found out that PyErr_Occurred() returns TRUE, thus the same error message as before.
    3) I tried replacing "if (v >= 0) {" with "if (!PyErr_Occurred()) {" only. That seems working fine.

     
  • szager
    szager
    2011-11-01

    OK, I have updated the patch. Note that the new patch has diffs for two files, not one.

    Please try applying the patches to the SWIG source files, and *not* to your generated wrappers. A fix which can't be applied to source files is useless. Although it is confusing to see a change only in SWIG_AsVal_dec, that is in fact the correct place to fix this.

    Thanks,

    Stefan

     
  • szager
    szager
    2011-11-01

     
    Attachments
  • Hongbin Wei
    Hongbin Wei
    2011-11-05

    Thanks, Stefan. Your latest patch works.

     
  • szager
    szager
    2011-11-05

    • status: open --> pending-fixed
     
  • szager
    szager
    2011-11-05

    Fix committed as revision 12835.

    This will appear in the next SWIG release (2.0.5).

     
  • William Fulton
    William Fulton
    2012-04-14

    This patch should not have gone in as it introduces failures in the python test-suite. Reverted (in rev 12983) until patch is fixed.