gcc optimization breaks NumPy?

Fredrik Lundh fredrik at pythonware.com
Tue Aug 24 04:39:31 EDT 1999

John Fisher <jfisher at are.berkeley.edu> wrote:
> I was getting consistent but inexplicable segmentation faults with the
> following code, using NumPy's C API to move data between Python and C
> (for sparse matrix multiplication; the data structure is from Meschach,
> in case that's relevant).  I found the problem to be caused by gcc's
> optimization options.

well, there's also a little strange thing in that¨
code of yours:

> PyObject * MakeFromMes(SPMAT *in) {
>   int i, j, m, n, nzs, count, pos, therow, thecol;
>   int dummy[0];

(I didn't even know you could have arrays
with zero elements...)

>   double elem;
>   PyArrayObject *pr, *ir, *jc;
>   m = in->m; n = in->n; nzs = 0;
>   /* Count the number of nonzero elements of in
>      ISZERO is a macro to test for "good enough" floating point 0 */
>   for(i = 0; i < m; i++) {
>     for(j = 0; j < n; j++) {
>       if(!ISZERO(sp_get_val(in, i, j))) nzs++;
>     }
>   }
>   dummy[0] = nzs;

but here you assume it has one element more than
you requested...

depending on the stack layout, this may smash one
of the PyArrayObject pointers.  looks like a great
way to get a segmentation fault ;-)


More information about the Python-list mailing list