From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 616F2382E908 for ; Wed, 25 May 2022 15:09:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 616F2382E908 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-590-2gASWNzGPK2ZsLkyW3BfCg-1; Wed, 25 May 2022 11:09:11 -0400 X-MC-Unique: 2gASWNzGPK2ZsLkyW3BfCg-1 Received: by mail-qt1-f198.google.com with SMTP id m19-20020a05622a055300b002f940281f18so4463418qtx.22 for ; Wed, 25 May 2022 08:09:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=DnodvSjIGzFKCLXaQwlEqwpCVxcjDGRmYH15J1kzBKA=; b=F6ZyjIZ0iecnrzIYDDRl6UqiTvB5QfgL7LEwU6tSGslO8IeGsCa4HZ7GHBp77BbcJV Jsyf28pvH6NcmVc68OXUdkH3suoxzVwYIJvgy5Pqhwv/GrU6M0GnqvmQX7sECDCyzHB2 fqGjcfvYRcX/x6Dc+aBzBXx69tgFpyjktlD4ORGRtzBeGdZxqDSe683+9ttVfF2xggSr 8csBpzRbGSzvBaG+oQMJqIp5UdSGEVf/2ydZtOIzbd8/pCp0lI4gOf24oxojcYewTFFN 9oU1HWesnIxot9yXp1uZeIEUfiPXREXf2E3YAindey8qASeTTC/g1lruXGNKbOE3qOsd nalA== X-Gm-Message-State: AOAM533QgD7PO5a79EhrqUJWHn+z825wnN5rKEKEeo5mgOAbl+uc8A8Y V7GRqanmZn9PaYeHbzoDCDm7ho5YTE0kOQzWyMU+ZMxDA9YMa9B/9a664m9t7YBzFiOrsCaZn9V D3pT9XPPZ4wpNSe7L2c3GNmDwhHA1xpd8WzfVd61kwcgBSY9Uq3mqjtNiqyHn7Nk8eA== X-Received: by 2002:a05:620a:29cf:b0:6a5:78d7:aea3 with SMTP id s15-20020a05620a29cf00b006a578d7aea3mr5661096qkp.422.1653491349520; Wed, 25 May 2022 08:09:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyDZQgxNlAu2RoshvTFQ8XX3CgZuySwC9e5A80N0Ax4gpUlRjmJwWnGs3u3ZeIxGuA/rOEXZw== X-Received: by 2002:a05:620a:29cf:b0:6a5:78d7:aea3 with SMTP id s15-20020a05620a29cf00b006a578d7aea3mr5661062qkp.422.1653491349099; Wed, 25 May 2022 08:09:09 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id b11-20020ac8540b000000b002f39b99f6c5sm1389814qtq.95.2022.05.25.08.09.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 May 2022 08:09:07 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: constexpr returning deallocated ptr Date: Wed, 25 May 2022 11:09:06 -0400 Message-Id: <20220525150906.1115777-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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-List-Received-Date: Wed, 25 May 2022 15:09:14 -0000 In constexpr-new3.C, the f7 function returns a deleted pointer, which we were happily caching because the new and delete are balanced. Don't. Tested x86_64-pc-linux-gnu, applying to trunk. gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_call_expression): Check for heap vars in the result. --- gcc/cp/constexpr.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 1a70fda1dc5..45208478c3f 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -1356,6 +1356,7 @@ static tree cxx_eval_constant_expression (const constexpr_ctx *, tree, value_cat, bool *, bool *, tree * = NULL); static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree, bool * = NULL); +static tree find_heap_var_refs (tree *, int *, void *); /* Attempt to evaluate T which represents a call to a builtin function. We assume here that all builtin functions evaluate to scalar types @@ -2965,6 +2966,10 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, cacheable = false; break; } + /* Also don't cache a call that returns a deallocated pointer. */ + if (cacheable && (cp_walk_tree_without_duplicates + (&result, find_heap_var_refs, NULL))) + cacheable = false; } /* Rewrite all occurrences of the function's RESULT_DECL with the base-commit: 1b661f3f5e712c951e774b3b91fffe4dac734cc7 prerequisite-patch-id: cc6e608c68f4eb133f6a153f83dfe4f033544cbd -- 2.27.0