summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2021-12-20 11:44:21 -0700
committerGitHub <noreply@github.com>2021-12-20 11:44:21 -0700
commit7033c36445485f82e1f0c4ab7ece74a442bacfa9 (patch)
tree574ba1a66ba806d8c3d650d32c0bafb81d254aa0
parentade90f36025dfa09165ac3e106f17823f5af304f (diff)
parent271010f1037150e95017f803f4214b8861e528f2 (diff)
downloadnumpy-7033c36445485f82e1f0c4ab7ece74a442bacfa9.tar.gz
Merge pull request #20630 from WarrenWeckesser/f2py-possible-strcpy-buffer-overflow
BUG: f2py: Simplify creation of an exception message. Closes gh-19000.
-rw-r--r--numpy/f2py/src/fortranobject.c28
1 files changed, 12 insertions, 16 deletions
diff --git a/numpy/f2py/src/fortranobject.c b/numpy/f2py/src/fortranobject.c
index 0b32137ef..c96378170 100644
--- a/numpy/f2py/src/fortranobject.c
+++ b/numpy/f2py/src/fortranobject.c
@@ -702,15 +702,14 @@ check_and_fix_dimensions(const PyArrayObject *arr, const int rank,
npy_intp *dims);
static int
-count_negative_dimensions(const int rank, const npy_intp *dims)
+find_first_negative_dimension(const int rank, const npy_intp *dims)
{
- int i = 0, r = 0;
- while (i < rank) {
- if (dims[i] < 0)
- ++r;
- ++i;
+ for (int i = 0; i < rank; ++i) {
+ if (dims[i] < 0) {
+ return i;
+ }
}
- return r;
+ return -1;
}
#ifdef DEBUG_COPY_ND_ARRAY
@@ -795,15 +794,12 @@ array_from_pyobj(const int type_num, npy_intp *dims, const int rank,
((intent & F2PY_INTENT_CACHE) && (obj == Py_None)) ||
((intent & F2PY_OPTIONAL) && (obj == Py_None))) {
/* intent(cache), optional, intent(hide) */
- if (count_negative_dimensions(rank, dims) > 0) {
- int i;
- strcpy(mess,
- "failed to create intent(cache|hide)|optional array"
- "-- must have defined dimensions but got (");
- for (i = 0; i < rank; ++i)
- sprintf(mess + strlen(mess), "%" NPY_INTP_FMT ",", dims[i]);
- strcat(mess, ")");
- PyErr_SetString(PyExc_ValueError, mess);
+ int i = find_first_negative_dimension(rank, dims);
+ if (i >= 0) {
+ PyErr_Format(PyExc_ValueError,
+ "failed to create intent(cache|hide)|optional array"
+ " -- must have defined dimensions, but dims[%d] = %"
+ NPY_INTP_FMT, i, dims[i]);
return NULL;
}
arr = (PyArrayObject *)PyArray_New(&PyArray_Type, rank, dims, type_num,