isl_mat_left_hermite: avoid undefined null pointer arithmetic
authorMichael Kruse <isl@meinersbur.de>
Sat, 19 Dec 2020 01:33:26 +0000 (18 19:33 -0600)
committerSven Verdoolaege <sven.verdoolaege@gmail.com>
Sat, 19 Dec 2020 16:41:02 +0000 (19 17:41 +0100)
Technically, arithmetic on null pointers is undefined behavior,
even if only adding zero.  This may happen inside
isl_mat_left_hermite if the number of columns is zero.
Simply skip the entire computation in that case.

Signed-off-by: Michael Kruse <isl@meinersbur.de>
Signed-off-by: Sven Verdoolaege <sven.verdoolaege@gmail.com>
isl_mat.c

index 48a8234..37468ee 100644 (file)
--- a/isl_mat.c
+++ b/isl_mat.c
@@ -650,9 +650,6 @@ __isl_give isl_mat *isl_mat_left_hermite(__isl_take isl_mat *M, int neg,
                *Q = NULL;
        if (!M)
                goto error;
-       M = isl_mat_cow(M);
-       if (!M)
-               goto error;
        if (U) {
                *U = isl_mat_identity(M->ctx, M->n_col);
                if (!*U)
@@ -664,6 +661,13 @@ __isl_give isl_mat *isl_mat_left_hermite(__isl_take isl_mat *M, int neg,
                        goto error;
        }
 
+       if (M->n_col == 0)
+               return M;
+
+       M = isl_mat_cow(M);
+       if (!M)
+               goto error;
+
        col = 0;
        isl_int_init(c);
        for (row = 0; row < M->n_row; ++row) {