isl_basic_set_opt: avoid invalid access on error path
[platform/upstream/isl.git] / isl_pw_templ.c
index c2e871c..3993974 100644 (file)
@@ -538,9 +538,9 @@ static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
 
        n = pw1->n * pw2->n;
 #ifdef HAS_TYPE
-       res = FN(PW,alloc_size)(space, pw1->type, n);
+       res = FN(PW,alloc_size)(isl_space_copy(space), pw1->type, n);
 #else
-       res = FN(PW,alloc_size)(space, n);
+       res = FN(PW,alloc_size)(isl_space_copy(space), n);
 #endif
 
        for (i = 0; i < pw1->n; ++i) {
@@ -568,6 +568,7 @@ static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
                }
        }
 
+       isl_space_free(space);
        FN(PW,free)(pw1);
        FN(PW,free)(pw2);
        return res;
@@ -982,12 +983,14 @@ __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
        if (!pw->dim)
                goto error;
        for (i = 0; i < pw->n; ++i) {
-               pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
-               if (!pw->p[i].set)
-                       goto error;
                pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
                if (!pw->p[i].FIELD)
                        goto error;
+               if (type == isl_dim_out)
+                       continue;
+               pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
+               if (!pw->p[i].set)
+                       goto error;
        }
 
        return pw;
@@ -1649,6 +1652,36 @@ error:
        return NULL;
 }
 
+static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
+       __isl_take isl_pw_multi_aff *pma,
+       __isl_give PW *(*fn)(__isl_take PW *pw,
+               __isl_take isl_pw_multi_aff *ma))
+{
+       isl_ctx *ctx;
+       isl_space *pma_space;
+
+       pma_space = isl_pw_multi_aff_get_space(pma);
+       if (!pw || !pma || !pma_space)
+               goto error;
+       if (isl_space_match(pw->dim, isl_dim_param, pma_space, isl_dim_param)) {
+               isl_space_free(pma_space);
+               return fn(pw, pma);
+       }
+       ctx = FN(PW,get_ctx)(pw);
+       if (!isl_space_has_named_params(pw->dim) ||
+           !isl_space_has_named_params(pma_space))
+               isl_die(ctx, isl_error_invalid,
+                       "unaligned unnamed parameters", goto error);
+       pw = FN(PW,align_params)(pw, pma_space);
+       pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
+       return fn(pw, pma);
+error:
+       isl_space_free(pma_space);
+       FN(PW,free)(pw);
+       isl_pw_multi_aff_free(pma);
+       return NULL;
+}
+
 /* Compute the pullback of "pw" by the function represented by "ma".
  * In other words, plug in "ma" in "pw".
  */
@@ -1693,4 +1726,53 @@ __isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
        return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
                                        &FN(PW,pullback_multi_aff_aligned));
 }
+
+/* Compute the pullback of "pw" by the function represented by "pma".
+ * In other words, plug in "pma" in "pw".
+ */
+static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
+       __isl_take isl_pw_multi_aff *pma)
+{
+       int i;
+       PW *res;
+
+       if (!pma)
+               goto error;
+
+       if (pma->n == 0) {
+               isl_pw_multi_aff_free(pma);
+               res = FN(PW,empty)(FN(PW,get_space)(pw));
+               FN(PW,free)(pw);
+               return res;
+       }
+
+       res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
+                                       isl_multi_aff_copy(pma->p[0].maff));
+       res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
+
+       for (i = 1; i < pma->n; ++i) {
+               PW *res_i;
+
+               res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
+                                       isl_multi_aff_copy(pma->p[i].maff));
+               res_i = FN(PW,intersect_domain)(res_i,
+                                       isl_set_copy(pma->p[i].set));
+               res = FN(PW,add_disjoint)(res, res_i);
+       }
+
+       isl_pw_multi_aff_free(pma);
+       FN(PW,free)(pw);
+       return res;
+error:
+       isl_pw_multi_aff_free(pma);
+       FN(PW,free)(pw);
+       return NULL;
+}
+
+__isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
+       __isl_take isl_pw_multi_aff *pma)
+{
+       return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
+                                       &FN(PW,pullback_pw_multi_aff_aligned));
+}
 #endif