summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorSebastian Berg <sebastianb@nvidia.com>2023-02-21 12:20:22 +0100
committerSebastian Berg <sebastianb@nvidia.com>2023-02-21 12:20:22 +0100
commit880cde5eec890c01bd96d0778989cb9dc292fa5b (patch)
tree35b4cb598f6b2a0158c704060f7b975a98935e02 /numpy
parentce7faad619c3457eb3ca48bafaedc7ae44712c29 (diff)
downloadnumpy-880cde5eec890c01bd96d0778989cb9dc292fa5b.tar.gz
MAINT: Avoid malloc(0) in string (and generic) sorting
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/npysort/heapsort.cpp3
-rw-r--r--numpy/core/src/npysort/npysort_heapsort.h4
2 files changed, 7 insertions, 0 deletions
diff --git a/numpy/core/src/npysort/heapsort.cpp b/numpy/core/src/npysort/heapsort.cpp
index 3956de51f..77a4bda74 100644
--- a/numpy/core/src/npysort/heapsort.cpp
+++ b/numpy/core/src/npysort/heapsort.cpp
@@ -55,6 +55,9 @@ npy_heapsort(void *start, npy_intp num, void *varr)
PyArrayObject *arr = (PyArrayObject *)varr;
npy_intp elsize = PyArray_ITEMSIZE(arr);
PyArray_CompareFunc *cmp = PyArray_DESCR(arr)->f->compare;
+ if (elsize == 0) {
+ return 0; /* no need for sorting elements of no size */
+ }
char *tmp = (char *)malloc(elsize);
char *a = (char *)start - elsize;
npy_intp i, j, l;
diff --git a/numpy/core/src/npysort/npysort_heapsort.h b/numpy/core/src/npysort/npysort_heapsort.h
index 442320094..16750b817 100644
--- a/numpy/core/src/npysort/npysort_heapsort.h
+++ b/numpy/core/src/npysort/npysort_heapsort.h
@@ -128,6 +128,10 @@ int string_heapsort_(type *start, npy_intp n, void *varr)
{
PyArrayObject *arr = (PyArrayObject *)varr;
size_t len = PyArray_ITEMSIZE(arr) / sizeof(type);
+ if (len == 0) {
+ return 0; /* no need for sorting if strings are empty */
+ }
+
type *tmp = (type *)malloc(PyArray_ITEMSIZE(arr));
type *a = (type *)start - len;
npy_intp i, j, l;