[Numpy-discussion] Correct C string handling in the NumPy C API?
Sturla Molden
sturla.molden at gmail.com
Sat Jan 3 13:15:27 EST 2015
Here is an example:
NPY_NO_EXPORT NpyIter_IterNextFunc *
NpyIter_GetIterNext(NpyIter *iter, char **errmsg)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
int ndim = NIT_NDIM(iter);
int nop = NIT_NOP(iter);
if (NIT_ITERSIZE(iter) < 0) {
if (errmsg == NULL) {
PyErr_SetString(PyExc_ValueError, "iterator is too large");
}
else {
*errmsg = "iterator is too large";
}
return NULL;
}
After NpyIter_GetIterNext returns, *errmsg points to a local variable in
a returned function.
Either I am wrong about C, or this code has undefied behavior...
My gutfeeling is that
*errmsg = "iterator is too large";
puts the string "iterator is too large" on the stack and points *errmsg
to the string.
Shouldn't this really be
strcpy(*errmsg, "iterator is too large");
and then *errmsg should point to a char buffer allocated before
NpyIter_GetIterNext is called?
Or will the statement
*errmsg = "iterator is too large";
put the string on the stack in the calling C function?
Before I open an issue I will ask if my understanding of C is correct or
not.
I am a bit confused here...
Regards,
Sturla
More information about the NumPy-Discussion
mailing list