]> granicus.if.org Git - graphviz/commitdiff
SparseMatrix_copy: avoid calling memcpy with null pointers
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 11 Jul 2021 18:49:20 +0000 (11:49 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 17 Jul 2021 19:34:10 +0000 (12:34 -0700)
The memcpy function technically requires both its pointer inputs to be non-null.
There is no special case for the length being 0. When running the example from
#2088 under UBSan, it detects this memcpy call as being performed with both null
source and destination pointers. It is unlikely any real world memcpy would
misbehave in this circumstance, but it is still good practice to avoid this.

lib/sparse/SparseMatrix.c

index 1e81eaef2b1fec421752159440765a4e42f0883d..df0ad6ced475410724c2587f7984a4f1fbdd4535 100644 (file)
@@ -1895,7 +1895,9 @@ SparseMatrix SparseMatrix_copy(SparseMatrix A){
   if (!A) return A;
   B = SparseMatrix_general_new(A->m, A->n, A->nz, A->type, A->size, A->format);
   memcpy(B->ia, A->ia, sizeof(int)*((size_t)(A->m+1)));
-  memcpy(B->ja, A->ja, sizeof(int)*((size_t)(A->ia[A->m])));
+  if (A->ia[A->m] != 0) {
+    memcpy(B->ja, A->ja, sizeof(int)*((size_t)(A->ia[A->m])));
+  }
   if (A->a) memcpy(B->a, A->a, A->size*((size_t)A->nz));
   B->property = A->property;
   B->nz = A->nz;