From 0853d73b46f525d5ee78daad4ebc7de966ff3ed5 Mon Sep 17 00:00:00 2001 From: Erwin Janssen Date: Sat, 19 Nov 2016 02:06:57 +0100 Subject: [PATCH] Fixed: 2 'dereference before null check' in SparseMatrix In the file lib/sparse/SparseMatric.x, there were two functions were a pointer is dereferenced before checking if the pointer is NULL. The check `if(!A)` caused an early return in both functions. The fix is placing the check and the early return as the first line in both functions. Dereferencing then only occures if the pointer A is not NULL. --- lib/sparse/SparseMatrix.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/sparse/SparseMatrix.c b/lib/sparse/SparseMatrix.c index c5d37f495..777c9085f 100644 --- a/lib/sparse/SparseMatrix.c +++ b/lib/sparse/SparseMatrix.c @@ -67,11 +67,12 @@ SparseMatrix SparseMatrix_make_undirected(SparseMatrix A){ return SparseMatrix_remove_upper(B); } SparseMatrix SparseMatrix_transpose(SparseMatrix A){ + if (!A) return NULL; + int *ia = A->ia, *ja = A->ja, *ib, *jb, nz = A->nz, m = A->m, n = A->n, type = A->type, format = A->format; SparseMatrix B; int i, j; - if (!A) return NULL; assert(A->format == FORMAT_CSR);/* only implemented for CSR right now */ B = SparseMatrix_new(n, m, nz, type, format); @@ -175,6 +176,8 @@ SparseMatrix SparseMatrix_symmetrize_nodiag(SparseMatrix A, int pattern_symmetri } int SparseMatrix_is_symmetric(SparseMatrix A, int test_pattern_symmetry_only){ + if (!A) return FALSE; + /* assume no repeated entries! */ SparseMatrix B; int *ia, *ja, *ib, *jb, type, m; @@ -183,8 +186,6 @@ int SparseMatrix_is_symmetric(SparseMatrix A, int test_pattern_symmetry_only){ int i, j; assert(A->format == FORMAT_CSR);/* only implemented for CSR right now */ - if (!A) return FALSE; - if (SparseMatrix_known_symmetric(A)) return TRUE; if (test_pattern_symmetry_only && SparseMatrix_known_strucural_symmetric(A)) return TRUE; -- 2.40.0