isl_basic_set_opt: avoid invalid access on error path
[platform/upstream/isl.git] / isl_scan.c
index e708923..bc239c8 100644 (file)
@@ -1,21 +1,23 @@
 /*
  * Copyright 2008-2009 Katholieke Universiteit Leuven
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * 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 <isl_ctx_private.h>
+#include <isl_map_private.h>
 #include "isl_basis_reduction.h"
 #include "isl_scan.h"
-#include "isl_seq.h"
+#include <isl/seq.h>
 #include "isl_tab.h"
-#include <isl_map_private.h>
 
 struct isl_counter {
        struct isl_scan_callback callback;
        isl_int count;
+       isl_int max;
 };
 
 static int increment_counter(struct isl_scan_callback *cb,
@@ -27,7 +29,9 @@ static int increment_counter(struct isl_scan_callback *cb,
 
        isl_vec_free(sample);
 
-       return 0;
+       if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
+               return 0;
+       return -1;
 }
 
 static int increment_range(struct isl_scan_callback *cb, isl_int min, isl_int max)
@@ -38,7 +42,10 @@ static int increment_range(struct isl_scan_callback *cb, isl_int min, isl_int ma
        isl_int_sub(cnt->count, cnt->count, min);
        isl_int_add_ui(cnt->count, cnt->count, 1);
 
-       return 0;
+       if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
+               return 0;
+       isl_int_set(cnt->count, cnt->max);
+       return -1;
 }
 
 /* Call callback->add with the current sample value of the tableau "tab".
@@ -72,7 +79,7 @@ static int scan_0D(struct isl_basic_set *bset,
        return callback->add(callback, sample);
 }
 
-/* Look for all integer points in "bset", which is assumed to be unbounded,
+/* Look for all integer points in "bset", which is assumed to be bounded,
  * and call callback->add on each of them.
  *
  * We first compute a reduced basis for the set and then scan
@@ -117,7 +124,7 @@ int isl_basic_set_scan(struct isl_basic_set *bset,
        if (!min || !max || !snap)
                goto error;
 
-       tab = isl_tab_from_basic_set(bset);
+       tab = isl_tab_from_basic_set(bset, 0);
        if (!tab)
                goto error;
        if (isl_tab_extend_cons(tab, dim + 1) < 0)
@@ -179,7 +186,8 @@ int isl_basic_set_scan(struct isl_basic_set *bset,
                        continue;
                }
                isl_int_neg(B->row[1 + level][0], min->el[level]);
-               tab = isl_tab_add_valid_eq(tab, B->row[1 + level]);
+               if (isl_tab_add_valid_eq(tab, B->row[1 + level]) < 0)
+                       goto error;
                isl_int_set_si(B->row[1 + level][0], 0);
                if (level < dim - 1) {
                        ++level;
@@ -210,37 +218,87 @@ error:
        return -1;
 }
 
-int isl_set_count(__isl_keep isl_set *set, isl_int *count)
+int isl_set_scan(__isl_take isl_set *set, struct isl_scan_callback *callback)
 {
        int i;
-       struct isl_counter cnt = { { &increment_counter } };
-
-       if (!set)
-               return -1;
 
-       isl_int_init(cnt.count);
+       if (!set || !callback)
+               goto error;
 
-       set = isl_set_copy(set);
        set = isl_set_cow(set);
        set = isl_set_make_disjoint(set);
        set = isl_set_compute_divs(set);
        if (!set)
                goto error;
 
-       isl_int_set_si(cnt.count, 0);
        for (i = 0; i < set->n; ++i)
                if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
-                                       &cnt.callback) < 0)
+                                       callback) < 0)
                        goto error;
 
+       isl_set_free(set);
+       return 0;
+error:
+       isl_set_free(set);
+       return -1;
+}
+
+int isl_basic_set_count_upto(__isl_keep isl_basic_set *bset,
+       isl_int max, isl_int *count)
+{
+       struct isl_counter cnt = { { &increment_counter } };
+
+       if (!bset)
+               return -1;
+
+       isl_int_init(cnt.count);
+       isl_int_init(cnt.max);
+
+       isl_int_set_si(cnt.count, 0);
+       isl_int_set(cnt.max, max);
+       if (isl_basic_set_scan(isl_basic_set_copy(bset), &cnt.callback) < 0 &&
+           isl_int_lt(cnt.count, cnt.max))
+               goto error;
+
        isl_int_set(*count, cnt.count);
+       isl_int_clear(cnt.max);
+       isl_int_clear(cnt.count);
 
-       isl_set_free(set);
+       return 0;
+error:
+       isl_int_clear(cnt.count);
+       return -1;
+}
+
+int isl_set_count_upto(__isl_keep isl_set *set, isl_int max, isl_int *count)
+{
+       struct isl_counter cnt = { { &increment_counter } };
+
+       if (!set)
+               return -1;
+
+       isl_int_init(cnt.count);
+       isl_int_init(cnt.max);
+
+       isl_int_set_si(cnt.count, 0);
+       isl_int_set(cnt.max, max);
+       if (isl_set_scan(isl_set_copy(set), &cnt.callback) < 0 &&
+           isl_int_lt(cnt.count, cnt.max))
+               goto error;
+
+       isl_int_set(*count, cnt.count);
+       isl_int_clear(cnt.max);
        isl_int_clear(cnt.count);
 
        return 0;
 error:
-       isl_set_free(set);
        isl_int_clear(cnt.count);
        return -1;
 }
+
+int isl_set_count(__isl_keep isl_set *set, isl_int *count)
+{
+       if (!set)
+               return -1;
+       return isl_set_count_upto(set, set->ctx->zero, count);
+}