Repository: numpy/numpy
File: numpy/_core/src/multiarray/ctors.c (C)

Code before:
        if (PyDataType_GetArrFuncs(dtype)->scanfunc == NULL) {
            PyErr_SetString(PyExc_ValueError,
                    "Unable to read character files of that array type");
            Py_DECREF(dtype);
            return NULL;
        }
        ret = array_from_text(dtype, num, sep, &nread, fp,
                (next_element) fromfile_next_element,
                (skip_separator) fromfile_skip_separator, NULL);
    }
    if (ret == NULL) {
        Py_DECREF(dtype);
        return NULL;
    }
    if (((npy_intp) nread) < num) {
        /*
         * Resize array to smaller number of elements. Note that original
         * dtype may have included a subarray, so we may not be 1-d.
         */
        dims[0] = (npy_intp)nread;
        for (int i = 1; i < PyArray_NDIM(ret); i++) {
            dims[i] = PyArray_DIMS(ret)[i];
        }
        PyArray_Dims new_dims = {dims, PyArray_NDIM(ret)};
        PyObject *tmp = PyArray_Resize(ret, &new_dims, 0, 0);
        if(tmp == NULL) {
            Py_DECREF(dtype);
            Py_DECREF(ret);
            return NULL;
        }
    }
    Py_DECREF(dtype);
    return (PyObject *)ret;
}

/*NUMPY_API*/
NPY_NO_EXPORT PyObject *
PyArray_FromBuffer(PyObject *buf, PyArray_Descr *type,
                   npy_intp count, npy_intp offset)
{
    PyArrayObject *ret;
    char *data;
    Py_buffer view;
    Py_ssize_t ts;
    npy_intp s, n;
    int itemsize;
    int writeable = 1;

    if (type == NULL) {
        return NULL;
    }

Review comment (suggestion):
```suggestion
        if (tmp == NULL) {
```

More importantly, `Py_DECREF(tmp)` is missing.

Code after:
        if (PyDataType_GetArrFuncs(dtype)->scanfunc == NULL) {
            PyErr_SetString(PyExc_ValueError,
                    "Unable to read character files of that array type");
            Py_DECREF(dtype);
            return NULL;
        }
        ret = array_from_text(dtype, num, sep, &nread, fp,
                (next_element) fromfile_next_element,
                (skip_separator) fromfile_skip_separator, NULL);
    }
    if (ret == NULL) {
        Py_DECREF(dtype);
        return NULL;
    }
    if (((npy_intp) nread) < num) {
        /*
         * Resize array to smaller number of elements. Note that original
         * dtype may have included a subarray, so we may not be 1-d.
         */
        npy_intp dims[NPY_MAXDIMS];
        dims[0] = (npy_intp)nread;
        for (int i = 1; i < PyArray_NDIM(ret); i++) {
            dims[i] = PyArray_DIMS(ret)[i];
        }
        PyArray_Dims new_dims = {dims, PyArray_NDIM(ret)};
        if (PyArray_Resize_int(ret, &new_dims, 0) < 0) {
            Py_DECREF(dtype);
            Py_DECREF(ret);
            return NULL;
        }
    }
    Py_DECREF(dtype);
    return (PyObject *)ret;
}

/*NUMPY_API*/
NPY_NO_EXPORT PyObject *
PyArray_FromBuffer(PyObject *buf, PyArray_Descr *type,
                   npy_intp count, npy_intp offset)
{
    PyArrayObject *ret;
    char *data;
    Py_buffer view;
    Py_ssize_t ts;
    npy_intp s, n;
    int itemsize;
    int writeable = 1;

    if (type == NULL) {
        return NULL;
    }