[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] BUG: UintFromObj from base.c does not check for errors



Thanks.

Does the change below look ok?   (non-compiled, non-tested code)

I'm without internet at home for a week or so...  so if someone else
can look over it, test it, and commit it that'd be cool?

from src/base.c

static int UintFromObj (PyObject* obj, Uint32* val) {
    PyObject* intobj;
    int tmp_val;

    if (PyNumber_Check (obj)) {

        if (!(intobj = PyNumber_Int (obj)))
            return 0;

        tmp_val = PyInt_AsLong (obj);
        if (tmp_val == -1 && PyErr_Occurred ()) {
            PyErr_Clear ();
            Py_DECREF (intobj);
            return 0;
        }
        *val = (Uint32) tmp_val;
        Py_DECREF (intobj);
        return 1;
    }
    return 0;
}







On Mon, Sep 1, 2008 at 6:08 AM, Devan <devlai@xxxxxxxxx> wrote:
> Hi,
> It appears that the utility function UintFromObj does not check if an
> error has occurred after calling PyInt_AsLong. If an error (namely an
> overflow error if the object is actually a long) occured, then it is
> silently ignored until the next call to PyErr_Occurred. In my case
> this propped up while working on an extension module that caught an
> OverflowError on a subsequent call.
>
> If there is a specific reason for ignoring any errors (IntFromObj
> seems to explicitly clear any errors and return 0 instead), then I
> think the best thing would be to replace PyInt_AsLong with the macro
> PyInt_AS_LONG which does not do error checking and will not leave an
> exception hanging around.
>
> Thanks,
> - Devan.
>
> PS: I ran into this while trying using surface.convert(masks). Is
> there a better way to create the masks than x |= (1 << 0) | (1 << 1) |
> (1 << 2)... | (1 <<7) ?
>