isl_basic_set_opt: avoid invalid access on error path
[platform/upstream/isl.git] / basis_reduction_tab.c
index afe51fe..6c7b040 100644 (file)
@@ -1,5 +1,15 @@
+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ *
+ * Use of this software is governed by the MIT license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ */
+
 #include <assert.h>
-#include "isl_seq.h"
+#include <isl_map_private.h>
+#include <isl/seq.h>
 #include "isl_tab.h"
 
 struct tab_lp {
@@ -20,14 +30,14 @@ struct tab_lp {
        int              is_fixed;
 };
 
-static struct tab_lp *init_lp(struct isl_basic_set *bset);
+static struct tab_lp *init_lp(struct isl_tab *tab);
 static void set_lp_obj(struct tab_lp *lp, isl_int *row, int dim);
 static int solve_lp(struct tab_lp *lp);
 static void get_obj_val(struct tab_lp* lp, mpq_t *F);
 static void delete_lp(struct tab_lp *lp);
 static int add_lp_row(struct tab_lp *lp, isl_int *row, int dim);
 static void get_alpha(struct tab_lp* lp, int row, mpq_t *alpha);
-static void del_lp_row(struct tab_lp *lp);
+static int del_lp_row(struct tab_lp *lp) WARN_UNUSED;
 static int cut_lp_to_hyperplane(struct tab_lp *lp, isl_int *row);
 
 #define GBR_LP                             struct tab_lp
@@ -58,21 +68,16 @@ static int cut_lp_to_hyperplane(struct tab_lp *lp, isl_int *row);
  * This could be optimized by first setting up a tableau for bset
  * and then performing the Cartesian product on the tableau.
  */
-static struct isl_tab *gbr_tab(struct isl_basic_set *bset,
-       struct isl_vec *row)
+static struct isl_tab *gbr_tab(struct isl_tab *tab, struct isl_vec *row)
 {
-       int i, j;
        unsigned dim;
-       struct isl_tab *tab;
        struct isl_tab *prod;
 
-       if (!bset || !row)
+       if (!tab || !row)
                return NULL;
 
-       dim = isl_basic_set_total_dim(bset);
-       tab = isl_tab_from_basic_set(bset);
+       dim = tab->n_var;
        prod = isl_tab_product(tab, tab);
-       isl_tab_free(tab);
        if (isl_tab_extend_cons(prod, 3 * dim + 1) < 0) {
                isl_tab_free(prod);
                return NULL;
@@ -80,16 +85,14 @@ static struct isl_tab *gbr_tab(struct isl_basic_set *bset,
        return prod;
 }
 
-static struct tab_lp *init_lp(struct isl_basic_set *bset)
+static struct tab_lp *init_lp(struct isl_tab *tab)
 {
        struct tab_lp *lp = NULL;
 
-       if (!bset)
+       if (!tab)
                return NULL;
 
-       isl_assert(bset->ctx, bset->n_eq == 0, return NULL);
-
-       lp = isl_calloc_type(bset->ctx, struct tab_lp);
+       lp = isl_calloc_type(tab->mat->ctx, struct tab_lp);
        if (!lp)
                return NULL;
 
@@ -98,9 +101,9 @@ static struct tab_lp *init_lp(struct isl_basic_set *bset)
        isl_int_init(lp->tmp);
        isl_int_init(lp->tmp2);
 
-       lp->dim = isl_basic_set_total_dim(bset);
+       lp->dim = tab->n_var;
 
-       lp->ctx = bset->ctx;
+       lp->ctx = tab->mat->ctx;
        isl_ctx_ref(lp->ctx);
 
        lp->stack = isl_alloc_array(lp->ctx, struct isl_tab_undo *, lp->dim);
@@ -108,7 +111,7 @@ static struct tab_lp *init_lp(struct isl_basic_set *bset)
        lp->row = isl_vec_alloc(lp->ctx, 1 + 2 * lp->dim);
        if (!lp->row)
                goto error;
-       lp->tab = gbr_tab(bset, lp->row);
+       lp->tab = gbr_tab(tab, lp->row);
        if (!lp->tab)
                goto error;
        lp->con_offset = lp->tab->n_con;
@@ -177,11 +180,13 @@ static int cut_lp_to_hyperplane(struct tab_lp *lp, isl_int *row)
                return -1;
 
        isl_int_neg(lp->row->el[0], lp->tmp);
-       lp->tab = isl_tab_add_eq(lp->tab, lp->row->el);
+       if (isl_tab_add_eq(lp->tab, lp->row->el) < 0)
+               return -1;
 
        isl_seq_cpy(lp->row->el + 1 + lp->dim, row, lp->dim);
        isl_seq_clr(lp->row->el + 1, lp->dim);
-       lp->tab = isl_tab_add_eq(lp->tab, lp->row->el);
+       if (isl_tab_add_eq(lp->tab, lp->row->el) < 0)
+               return -1;
 
        lp->con_offset += 2;
 
@@ -218,7 +223,8 @@ static int add_lp_row(struct tab_lp *lp, isl_int *row, int dim)
        isl_seq_cpy(lp->row->el + 1, row, lp->dim);
        isl_seq_neg(lp->row->el + 1 + lp->dim, row, lp->dim);
 
-       lp->tab = isl_tab_add_valid_eq(lp->tab, lp->row->el);
+       if (isl_tab_add_valid_eq(lp->tab, lp->row->el) < 0)
+               return -1;
 
        return lp->neq++;
 }
@@ -230,8 +236,8 @@ static void get_alpha(struct tab_lp* lp, int row, mpq_t *alpha)
        isl_int_set(mpq_denref(*alpha), lp->tab->dual->el[0]);
 }
 
-static void del_lp_row(struct tab_lp *lp)
+static int del_lp_row(struct tab_lp *lp)
 {
        lp->neq--;
-       isl_tab_rollback(lp->tab, lp->stack[lp->neq]);
+       return isl_tab_rollback(lp->tab, lp->stack[lp->neq]);
 }