davidmalcolm / gcc-python-plugin

GCC plugin that embeds CPython inside the compiler
GNU General Public License v3.0
197 stars 58 forks source link

False positive in a function marked raising on negative result #158

Open dvarrazzo opened 5 years ago

dvarrazzo commented 5 years ago

Found a false positive analysing this function:

A more synthetic case is:

RAISES_NEG static int
test_cpychecker(PyObject *pyval)
{
    int rv = -1;
    long val;

    if (PyInt_Check(pyval)) {
        val = PyInt_AsLong(pyval);
        if (val == -1 && PyErr_Occurred()) { goto exit; }
        if (val < 1 || val > 4) {
            PyErr_SetString(PyExc_ValueError,
                "isolation_level must be between 1 and 4");
            goto exit;
        }

        rv = val;

        /* Work around cpychecker false positive */
        /* if (rv < 0) { */
        /*     PyErr_SetString(PyExc_ValueError, */
        /*         "isolation_level must be between 1 and 4"); */
        /*     goto exit; */
        /* } */
    }

    else {
        rv = 1;
    }

exit:
    return rv;
}

where the case that 1 <= val <= 4 is not transferred to rv, so it seems a negative number can be returned with no exception, resulting in the following trace.

image

Enabling the commented block removes the false positive. I tried to add a test to the plugin test suite but no success.