isl_basic_set_opt: avoid invalid access on error path
[platform/upstream/isl.git] / isl_sample_piplib.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the MIT license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
10 #include <isl/mat.h>
11 #include <isl/vec.h>
12 #include <isl/seq.h>
13 #include "isl_piplib.h"
14 #include "isl_sample_piplib.h"
15
16 struct isl_vec *isl_pip_basic_set_sample(struct isl_basic_set *bset)
17 {
18         PipOptions      *options = NULL;
19         PipMatrix       *domain = NULL;
20         PipQuast        *sol = NULL;
21         struct isl_vec *vec = NULL;
22         unsigned        dim;
23         struct isl_ctx *ctx;
24
25         if (!bset)
26                 goto error;
27         ctx = isl_basic_set_get_ctx(bset);
28         isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
29         isl_assert(ctx, isl_basic_set_dim(bset, isl_dim_div) == 0, goto error);
30         dim = isl_basic_set_n_dim(bset);
31         domain = isl_basic_map_to_pip((struct isl_basic_map *)bset, 0, 0, 0);
32         if (!domain)
33                 goto error;
34
35         options = pip_options_init();
36         if (!options)
37                 goto error;
38         sol = pip_solve(domain, NULL, -1, options);
39         if (!sol)
40                 goto error;
41         if (!sol->list)
42                 vec = isl_vec_alloc(ctx, 0);
43         else {
44                 PipList *l;
45                 int i;
46                 vec = isl_vec_alloc(ctx, 1 + dim);
47                 if (!vec)
48                         goto error;
49                 isl_int_set_si(vec->block.data[0], 1);
50                 for (i = 0, l = sol->list; l && i < dim; ++i, l = l->next) {
51                         isl_seq_cpy_from_pip(&vec->block.data[1+i],
52                                         &l->vector->the_vector[0], 1);
53                         isl_assert(ctx, !entier_zero_p(l->vector->the_deno[0]),
54                                         goto error);
55                 }
56                 isl_assert(ctx, i == dim, goto error);
57         }
58
59         pip_quast_free(sol);
60         pip_options_free(options);
61         pip_matrix_free(domain);
62
63         isl_basic_set_free(bset);
64         return vec;
65 error:
66         isl_vec_free(vec);
67         isl_basic_set_free(bset);
68         if (sol)
69                 pip_quast_free(sol);
70         if (domain)
71                 pip_matrix_free(domain);
72         return NULL;
73 }