Skip to content

Missing return-value validation of the function PyArray_DescrNew #19038

@awen-li

Description

@awen-li

Reproducing code example:

The definition of PyArray_DescrNew

NPY_NO_EXPORT PyArray_Descr * PyArray_DescrNew(PyArray_Descr *base)
{
    PyArray_Descr *newdescr = PyObject_New(PyArray_Descr, Py_TYPE(base));
    if (newdescr == NULL) {
        return NULL; ----------------> **point 1**
    }
    .........
    if (base->c_metadata != NULL) {
        newdescr->c_metadata = NPY_AUXDATA_CLONE(base->c_metadata);
        if (newdescr->c_metadata == NULL) {
            PyErr_NoMemory();
            /* TODO: This seems wrong, as the old fields get decref'd? */
            Py_DECREF(newdescr);
            return NULL; ----------------> **point 2**
        }
    }
    ........
    if (newdescr->subarray) {
        newdescr->subarray = PyArray_malloc(sizeof(PyArray_ArrayDescr));
        if (newdescr->subarray == NULL) {
            Py_DECREF(newdescr);
            return (PyArray_Descr *)PyErr_NoMemory();   ----------------> **point 3**
        }
    }
   .........
    return newdescr;
}

Call-site example for PyArray_DescrNew

NPY_NO_EXPORT PyArray_Descr *
PyArray_DescrNewByteorder(PyArray_Descr *self, char newendian)
{
    PyArray_Descr *new;
    char endian;

    new = PyArray_DescrNew(self);
    endian = new->byteorder; -----> direct read through "new“ 
    ......
}

Error message:

At most call-sites for PyArray_DescrNew, there are no validations of its return,
but an invalid address may be returned.
example

NumPy/Python version information:

the main branch

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions