From patchwork Sun Apr 2 15:05:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernhard Reutner-Fischer X-Patchwork-Id: 78255 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1785013vqo; Sun, 2 Apr 2023 08:06:59 -0700 (PDT) X-Google-Smtp-Source: AKy350b/CDgKK/pzEWYiO1sBJqLV/NKCtA9Itmv3G/sH5qnK8jmrcCUt98hcLeNw8WNcLXcSnB46 X-Received: by 2002:a17:906:16c3:b0:8b1:75a5:4a92 with SMTP id t3-20020a17090616c300b008b175a54a92mr32456631ejd.72.1680448019031; Sun, 02 Apr 2023 08:06:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680448019; cv=none; d=google.com; s=arc-20160816; b=jV2G/8yaH9BLslCyqzt2adM2x80x9QFdhAwFS7M9wwpNSBJAJwPEfYxDxJW4oMqWd4 9mqzq2AfypXYc0hnD/RxzshJ6YB94r6Nc9swlrcU15n85rUb7/LZ7VSP7WIKkatVR+LS 1exSfoxTRetzk3L3GJ8S7FXwxQ4EarprCLOYd/h5ryEiENKip7wMVy1FMABQ10NvGWE5 zk6ZS1bwVduV4UhFnMoHnYUD9/xC4bojk3y2qj5INVWDq7XCS5EXQx8R+zMFaT8NaPsf D+USaQnrxkZx0oGLxrdSBCohyrYXxSxsR8cmGCHomtPhgoCIA7r8VQObZGcl7X4VhVwo gkIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=4ddGE+cYu1aKp+XvaWm+vBDEUggW6gNWux7egmyAEF8=; b=qWuI5PqkM+VvQKYweFdBjcxdjoiH+nT7UucIA6c8Dg6YBEeWIgHGn0lumpLpHieLq0 seFiC+OWD+wn9Sm9iKQXOU/lbcrGrryVa352GmmWiOCXqQ391B/kYilKxbPQbd+N2KBk CKOvgQhQf+6SzHi8OU4TnF/cstSF38WUM8Df4Losx4GUaS2tNO2PS4Dm9qsFTlwwKOhQ WA06MtNsDL/PFiHpdUni4nhBi1eK6YiR0ksU/6ajRX84B5baQSr3aT7HQeoCYJnxZzHe PNZK6up3SRcdYkmNtNq1gDf7jfbJ1xwJY07LMolxd8U3mqho8CX+6TrkeQv57JJACZhP TyZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=F4x7pQOf; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id r17-20020a170906551100b0093c96889d25si5837861ejp.407.2023.04.02.08.06.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Apr 2023 08:06:59 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=F4x7pQOf; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BF856384F00E for ; Sun, 2 Apr 2023 15:06:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BF856384F00E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680447986; bh=4ddGE+cYu1aKp+XvaWm+vBDEUggW6gNWux7egmyAEF8=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=F4x7pQOfDhJ+RdFmSlvof02St/ucMkVRZQaSWnxR2DAIVwvenpTTofSRyx+vQMCU3 pfFBnU5nM9TlOUqBixxf+d1HpuJoNv8vdA624fO0TEQZOt2cNa1pkcyRrd0q0ohQro SSRVtqq2z937CRUzW06wQKnUve3MLKEYN/zP4lNs= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by sourceware.org (Postfix) with ESMTPS id 90AA73858C50; Sun, 2 Apr 2023 15:05:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 90AA73858C50 Received: by mail-ed1-x531.google.com with SMTP id h8so107880167ede.8; Sun, 02 Apr 2023 08:05:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680447940; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4ddGE+cYu1aKp+XvaWm+vBDEUggW6gNWux7egmyAEF8=; b=ERyKAru0QILGYILn2g/li6oF8dJJTqY2dMxNSUAiOMjokfGQiJg3dzie5kq5sTYkLL Wbk4893cMsdEufs4M8NNuWVuFRWpqHBfk2exin5zY1opZ9MhoajSoUHwx38exljEn440 7klAygdvusI037nYX8aWTDE1kQLEnTkiUbHlO1X7rIzMFFtkuvSsfwpTe9Oc9iS89cc2 0xxi9jvZvNaRW6P1H7Y37TPfDy3ijSa6p3m8HtNQZWLpbP3NPyixnAvpsh+yQl2mCWKo gr80ZP6Ka1V5wnOHMB6oRSBKCPeQwjFwk4/mtom0ZH3/i71D1VsjKa3qNgH70Kj85BhL SmYg== X-Gm-Message-State: AAQBX9drPPfzF4mw6l3yNdUm5iVW/xQhvDsgN7n/QgWG08Pa3vtA3jDU SevPj7TsYfSs2E1n2L+FDHaZY/D567c= X-Received: by 2002:a17:906:9618:b0:884:3174:119d with SMTP id s24-20020a170906961800b008843174119dmr34742793ejx.14.1680447939742; Sun, 02 Apr 2023 08:05:39 -0700 (PDT) Received: from nbbrfq ([2001:871:227:1e16:8ec:e883:b72a:18fd]) by smtp.gmail.com with ESMTPSA id u13-20020a1709063b8d00b0093e261cc8bcsm3326752ejf.58.2023.04.02.08.05.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Apr 2023 08:05:38 -0700 (PDT) Received: from b by nbbrfq with local (Exim 4.96) (envelope-from ) id 1pizGz-000Ad8-1f; Sun, 02 Apr 2023 17:05:37 +0200 To: gcc-patches@gcc.gnu.org Cc: Bernhard Reutner-Fischer , fortran@gcc.gnu.org Subject: [PATCH 3/3] Fortran: Fix mpz and mpfr memory leaks Date: Sun, 2 Apr 2023 17:05:15 +0200 Message-Id: <20230402150515.40826-4-rep.dot.nop@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Bernhard Reutner-Fischer via Gcc-patches From: Bernhard Reutner-Fischer Reply-To: Bernhard Reutner-Fischer Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762077462213828581?= X-GMAIL-MSGID: =?utf-8?q?1762077462213828581?= 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"); } 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"); }