From: Sven Verdoolaege Date: Sun, 4 May 2014 10:10:04 +0000 (+0200) Subject: isl_Polyhedron_Reduced_Basis: avoid double free of barvinok_options X-Git-Tag: barvinok-0.38~57 X-Git-Url: https://repo.or.cz/w/barvinok.git/commitdiff_plain/a488776044362891383dd2ba34f9d0392c1f53c0 isl_Polyhedron_Reduced_Basis: avoid double free of barvinok_options In 6702a6a (avoid use of isl_ctx internals, Wed Mar 16 16:25:11 2011 +0100), isl_Polyhedron_Reduced_Basis was changed to update options->isl->gbr_only_first and the to pass the barvinok_options to isl_ctx_alloc_with_options. This will make the resuling isl_ctx own the barvinok_options, which are then also freed when the isl_ctx is freed, even though it may and in fact is reused in the very same function after ths isl_ctx is freed. Apparently, this change was never tested. In f387b45 (update isl for hiding of isl_options, Fri Nov 11 11:33:25 2011 +0100), the function was further changed to update the gbr_only_first isl option after the isl_ctx is created. There is therefore no longer any need to pass the barvinok_options to isl_ctx_alloc_with_options and we can simply call isl_ctx_alloc instead. Signed-off-by: Sven Verdoolaege --- diff --git a/basis_reduction.c b/basis_reduction.c index 359346e..c1c185f 100644 --- a/basis_reduction.c +++ b/basis_reduction.c @@ -33,12 +33,10 @@ Matrix *isl_Polyhedron_Reduced_Basis(Polyhedron *P, isl_basic_set *bset; isl_mat *basis; Matrix *M; - int isl_gbr_only_first; - ctx = isl_ctx_alloc_with_options(&barvinok_options_args, options); + ctx = isl_ctx_alloc(); assert(ctx); - isl_gbr_only_first = isl_options_get_gbr_only_first(ctx); isl_options_set_gbr_only_first(ctx, options->gbr_only_first); dim = isl_space_set_alloc(ctx, 0, nvar); @@ -60,8 +58,6 @@ Matrix *isl_Polyhedron_Reduced_Basis(Polyhedron *P, isl_ctx_free(ctx); - isl_options_set_gbr_only_first(ctx, isl_gbr_only_first); - return M; } diff --git a/testlib.cc b/testlib.cc index 045772d..6a53e90 100644 --- a/testlib.cc +++ b/testlib.cc @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include "conversion.h" @@ -750,6 +751,35 @@ static int test_laurent(struct barvinok_options *options) return 0; } +/* Check that Polyhedron_Reduced_Basis produces a result + * of the expected dimensions (without crashing). + */ +static int test_basis_reduction(struct barvinok_options *options) +{ + Matrix *M; + Polyhedron *P; + + M = matrix_read_from_str( + "4 4\n" + "1 1 0 0 \n" + "1 0 1 0 \n" + "1 -1 0 1 \n" + "1 0 -1 1 \n"); + P = Constraints2Polyhedron(M, options->MaxRays); + Matrix_Free(M); + + M = Polyhedron_Reduced_Basis(P, options); + + assert(M); + assert(M->NbRows == 2); + assert(M->NbColumns == 2); + + Polyhedron_Free(P); + Matrix_Free(M); + + return 0; +} + int main(int argc, char **argv) { struct barvinok_options *options = barvinok_options_new_with_defaults(); @@ -770,5 +800,6 @@ int main(int argc, char **argv) test_ilp(options); test_hull(options); test_laurent(options); + test_basis_reduction(options); barvinok_options_free(options); }