ralna / spral

Sparse Parallel Robust Algorithms Library
https://ralna.github.io/spral/
Other
104 stars 27 forks source link

Fix n = 0 case for C interface of SSIDS #83

Closed mjacobse closed 2 years ago

mjacobse commented 2 years ago

A valid C implementation may return NULL for malloc(0). In that sense, zero-length arrays may be represented by a null pointer in C. When those are passed to the C interface of SSIDS, they are detected as not C_ASSOCIATED, and the corresponding Fortran pointers are nullified. A few of those nullified Fortran pointers are then used illegally without further checks. To fix this, I propose to avoid checking for C_ASSOCIATED altogether by declaring the C arguments as arrays directly.

The problem can be observed for example by setting

int n = 0;
long ptr[1] = {options.array_base};
int *row = NULL;
double *val = NULL;
double *x = NULL; 

in examples/C/ssids.c. A few issues about uninitialized values are reported by valgrind when running the example with these modifications. When additionally setting options.array_base = 0, the example segfaults.

See #82 for a discussion on a different solution that led to this simplified one.

jfowkes commented 2 years ago

Adding an explanation of the non-obvious changes made (see #82 for full discussion):