]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: Fix mpz and mpfr memory leaks [PR fortran/68800]
authorBernhard Reutner-Fischer <aldot@gcc.gnu.org>
Sat, 6 May 2023 22:02:21 +0000 (00:02 +0200)
committerBernhard Reutner-Fischer <aldot@gcc.gnu.org>
Mon, 8 May 2023 05:55:28 +0000 (07:55 +0200)
gcc/fortran/ChangeLog:

PR fortran/68800
* expr.cc (find_array_section): Fix mpz memory leak.
* simplify.cc (gfc_simplify_reshape): Fix mpz memory leaks in
error paths.

gcc/fortran/expr.cc
gcc/fortran/simplify.cc

index b19e3647aa018851a813ff412dc642c63a75e823..d91722e6ac6f28e0cfa3eaec7cca868123436a33 100644 (file)
@@ -1539,6 +1539,7 @@ find_array_section (gfc_expr *expr, gfc_ref *ref)
   mpz_init_set_ui (delta_mpz, one);
   mpz_init_set_ui (nelts, one);
   mpz_init (tmp_mpz);
+  mpz_init (ptr);
 
   /* Do the initialization now, so that we can cleanup without
      keeping track of where we were.  */
@@ -1682,7 +1683,6 @@ find_array_section (gfc_expr *expr, gfc_ref *ref)
       mpz_mul (delta_mpz, delta_mpz, tmp_mpz);
     }
 
-  mpz_init (ptr);
   cons = gfc_constructor_first (base);
 
   /* Now clock through the array reference, calculating the index in
@@ -1735,7 +1735,8 @@ find_array_section (gfc_expr *expr, gfc_ref *ref)
                     "at %L requires an increase of the allowed %d "
                     "upper limit.  See %<-fmax-array-constructor%> "
                     "option", &expr->where, flag_max_array_constructor);
-         return false;
+         t = false;
+         goto cleanup;
        }
 
       cons = gfc_constructor_lookup (base, limit);
@@ -1750,8 +1751,6 @@ find_array_section (gfc_expr *expr, gfc_ref *ref)
                                   gfc_copy_expr (cons->expr), NULL);
     }
 
-  mpz_clear (ptr);
-
 cleanup:
 
   mpz_clear (delta_mpz);
@@ -1765,6 +1764,7 @@ cleanup:
       mpz_clear (ctr[d]);
       mpz_clear (stride[d]);
     }
+  mpz_clear (ptr);
   gfc_constructor_free (base);
   return t;
 }
index 7b0642126a9ea73b42fa4ebdabe1f54f4f06ac22..a7b4784d73aaf247b25ed8f65244c5c9355af144 100644 (file)
@@ -6866,6 +6866,7 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
          gfc_error ("The SHAPE array for the RESHAPE intrinsic at %L has a "
                     "negative value %d for dimension %d",
                     &shape_exp->where, shape[rank], rank+1);
+         mpz_clear (index);
          return &gfc_bad_expr;
        }
 
@@ -6889,6 +6890,7 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
        {
          gfc_error ("Shapes of ORDER at %L and SHAPE at %L are different",
                     &order_exp->where, &shape_exp->where);
+         mpz_clear (index);
          return &gfc_bad_expr;
        }
 
@@ -6902,6 +6904,7 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
        {
          gfc_error ("Sizes of ORDER at %L and SHAPE at %L are different",
                     &order_exp->where, &shape_exp->where);
+         mpz_clear (index);
          return &gfc_bad_expr;
        }
 
@@ -6918,6 +6921,7 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
                         "in the range [1, ..., %d] for the RESHAPE intrinsic "
                         "near %L", order[i], &order_exp->where, rank,
                         &shape_exp->where);
+             mpz_clear (index);
              return &gfc_bad_expr;
            }
 
@@ -6926,6 +6930,7 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
            {
              gfc_error ("ORDER at %L is not a permutation of the size of "
                         "SHAPE at %L", &order_exp->where, &shape_exp->where);
+             mpz_clear (index);
              return &gfc_bad_expr;
            }
          x[order[i]] = 1;