Skip to content

Commit

Permalink
Do not cast pointers to arrays of differently sized integers.
Browse files Browse the repository at this point in the history
Casting between `int64_t *` and `int32_t *` does *not* maintain the values in
the array. Instead, it tells the compiler to interpret the memory at that
pointer as an array of a different type (i.e., two `int32_t` elements "become"
one `int64_t` element). That can lead to all kinds of errors and is very not
what was intended.

Remove the integer pointer casts to allow the compiler to emit an error on
compile-time instead of potentially causing, e.g., an array overflow on
runtime if `sunindextype` has a different size from `KLU_INDEXTYPE`.

Signed-off-by: Markus Mützel <[email protected]>
  • Loading branch information
mmuetzel committed May 16, 2024
1 parent 8bdca73 commit 87fdab6
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions src/sunlinsol/klu/sunlinsol_klu.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,6 @@
#define COMMON(S) (KLU_CONTENT(S)->common)
#define SOLVE(S) (KLU_CONTENT(S)->klu_solver)

/*
* -----------------------------------------------------------------
* typedef to handle pointer casts from sunindextype to KLU type
* -----------------------------------------------------------------
*/

#if defined(SUNDIALS_INT64_T)
#define KLU_INDEXTYPE int64_t
#else
#define KLU_INDEXTYPE int
#endif

/*
* -----------------------------------------------------------------
* exported functions
Expand Down Expand Up @@ -268,8 +256,8 @@ int SUNLinSolSetup_KLU(SUNLinearSolver S, SUNMatrix A)
if (SYMBOLIC(S)) { sun_klu_free_symbolic(&SYMBOLIC(S), &COMMON(S)); }
SYMBOLIC(S) =
sun_klu_analyze(SUNSparseMatrix_NP(A),
(KLU_INDEXTYPE*)SUNSparseMatrix_IndexPointers(A),
(KLU_INDEXTYPE*)SUNSparseMatrix_IndexValues(A), &COMMON(S));
SUNSparseMatrix_IndexPointers(A),
SUNSparseMatrix_IndexValues(A), &COMMON(S));
if (SYMBOLIC(S) == NULL)
{
LASTFLAG(S) = SUN_ERR_EXT_FAIL;
Expand All @@ -280,8 +268,8 @@ int SUNLinSolSetup_KLU(SUNLinearSolver S, SUNMatrix A)
Compute the LU factorization of the matrix
------------------------------------------------------------*/
if (NUMERIC(S)) { sun_klu_free_numeric(&NUMERIC(S), &COMMON(S)); }
NUMERIC(S) = sun_klu_factor((KLU_INDEXTYPE*)SUNSparseMatrix_IndexPointers(A),
(KLU_INDEXTYPE*)SUNSparseMatrix_IndexValues(A),
NUMERIC(S) = sun_klu_factor(SUNSparseMatrix_IndexPointers(A),
SUNSparseMatrix_IndexValues(A),
SUNSparseMatrix_Data(A), SYMBOLIC(S), &COMMON(S));
if (NUMERIC(S) == NULL)
{
Expand All @@ -294,8 +282,8 @@ int SUNLinSolSetup_KLU(SUNLinearSolver S, SUNMatrix A)
else
{ /* not the first decomposition, so just refactor */

retval = sun_klu_refactor((KLU_INDEXTYPE*)SUNSparseMatrix_IndexPointers(A),
(KLU_INDEXTYPE*)SUNSparseMatrix_IndexValues(A),
retval = sun_klu_refactor(SUNSparseMatrix_IndexPointers(A),
SUNSparseMatrix_IndexValues(A),
SUNSparseMatrix_Data(A), SYMBOLIC(S), NUMERIC(S),
&COMMON(S));
if (retval == 0)
Expand All @@ -321,7 +309,7 @@ int SUNLinSolSetup_KLU(SUNLinearSolver S, SUNMatrix A)
{
/* Condition number may be getting large.
Compute more accurate estimate */
retval = sun_klu_condest((KLU_INDEXTYPE*)SUNSparseMatrix_IndexPointers(A),
retval = sun_klu_condest(SUNSparseMatrix_IndexPointers(A),
SUNSparseMatrix_Data(A), SYMBOLIC(S), NUMERIC(S),
&COMMON(S));
if (retval == 0)
Expand All @@ -336,8 +324,8 @@ int SUNLinSolSetup_KLU(SUNLinearSolver S, SUNMatrix A)
large, so recompute the numeric factorization */
sun_klu_free_numeric(&NUMERIC(S), &COMMON(S));
NUMERIC(S) =
sun_klu_factor((KLU_INDEXTYPE*)SUNSparseMatrix_IndexPointers(A),
(KLU_INDEXTYPE*)SUNSparseMatrix_IndexValues(A),
sun_klu_factor(SUNSparseMatrix_IndexPointers(A),
SUNSparseMatrix_IndexValues(A),
SUNSparseMatrix_Data(A), SYMBOLIC(S), &COMMON(S));
if (NUMERIC(S) == NULL)
{
Expand Down

0 comments on commit 87fdab6

Please sign in to comment.