From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ciao.gmane.io (ciao.gmane.io [116.202.254.214]) by sourceware.org (Postfix) with ESMTPS id A3EFF3858C20 for ; Mon, 3 Apr 2023 19:50:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A3EFF3858C20 Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=m.gmane-mx.org Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1pjQCd-0009rv-4Q for gcc-patches@gcc.gnu.org; Mon, 03 Apr 2023 21:50:55 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: gcc-patches@gcc.gnu.org From: Harald Anlauf Subject: Re: [PATCH 3/3] Fortran: Fix mpz and mpfr memory leaks Date: Mon, 3 Apr 2023 21:50:49 +0200 Message-ID: <57162b6a-95e0-35c6-386a-3687908fce05@gmx.de> References: <20230402150515.40826-4-rep.dot.nop@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Content-Language: en-US In-Reply-To: <20230402150515.40826-4-rep.dot.nop@gmail.com> Cc: fortran@gcc.gnu.org X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,GIT_PATCH_0,HEADER_FROM_DIFFERENT_DOMAINS,KAM_DMARC_STATUS,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Message-ID: <20230403195049.yuJ40bErKboOKpmeL19kG3JDcLyXhwKZ92M8CRJk1EY@z> Hi Bernhard, there is neither context nor a related PR with a testcase showing that this patch fixes issues seen there. On 4/2/23 17:05, Bernhard Reutner-Fischer via Gcc-patches wrote: > From: Bernhard Reutner-Fischer > > Cc: fortran@gcc.gnu.org > > gcc/fortran/ChangeLog: > > * array.cc (gfc_ref_dimen_size): Free mpz memory before ICEing. > * expr.cc (find_array_section): Fix mpz memory leak. > * simplify.cc (gfc_simplify_reshape): Fix mpz memory leaks in > error paths. > (gfc_simplify_set_exponent): Fix mpfr memory leak. > --- > gcc/fortran/array.cc | 3 +++ > gcc/fortran/expr.cc | 8 ++++---- > gcc/fortran/simplify.cc | 7 ++++++- > 3 files changed, 13 insertions(+), 5 deletions(-) > > diff --git a/gcc/fortran/array.cc b/gcc/fortran/array.cc > index be5eb8b6a0f..8b1e816a859 100644 > --- a/gcc/fortran/array.cc > +++ b/gcc/fortran/array.cc > @@ -2541,6 +2541,9 @@ gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end) > return t; > > default: > + mpz_clear (lower); > + mpz_clear (stride); > + mpz_clear (upper); > gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type"); > } What is the point of clearing variables before issuing a gfc_internal_error? > diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc > index 7fb33f81788..b4736804eda 100644 > --- a/gcc/fortran/expr.cc > +++ b/gcc/fortran/expr.cc > @@ -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; > } > diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc > index ecf0e3558df..d1f06335e79 100644 > --- a/gcc/fortran/simplify.cc > +++ b/gcc/fortran/simplify.cc > @@ -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; > @@ -7408,7 +7413,7 @@ gfc_simplify_set_exponent (gfc_expr *x, gfc_expr *i) > exp2 = (unsigned long) mpz_get_d (i->value.integer); > mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE); > > - mpfr_clears (absv, log2, pow2, frac, NULL); > + mpfr_clears (exp, absv, log2, pow2, frac, NULL); > > return range_check (result, "SET_EXPONENT"); > }