From patchwork Mon Oct 24 20:53:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 10277 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp647718wru; Mon, 24 Oct 2022 13:55:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM60Chj0t2r16sRHu8p73NMl90TkQInCmqNYYHyXTbNqVdplFJE+n5jkLYyH6QgKK2ag8XhF X-Received: by 2002:a05:6402:2994:b0:453:4c5c:d31c with SMTP id eq20-20020a056402299400b004534c5cd31cmr33035038edb.412.1666644913980; Mon, 24 Oct 2022 13:55:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666644913; cv=none; d=google.com; s=arc-20160816; b=fmhibPMEsKNmEV0SXvhZc+CdqHk6acoDGSPRQzaTOdloDqSSb5STAVpgMXUaZBxCft 7Zb/3QAzvxB87rIW6XkPeksyu0x0eMhVmlgyK2WqBnschp5hrIM7n1w3gPxhXAoS/gdR h+FtbALo1yELPC0jJ9DxJrBeA5b0NAYyGdtp4qht/5m6IrRjnBXF9G14/6k4k24BDrGB 3QgFdDeuRFDhA8XduDznovHyi0Y0CpoioH5crfSOtFHxd56bhWXUZcu2Wq6Xt+vhw5yC l/f4fP9xydliQMqAQEK2uqyB7HRUs2Wl26/ZltunHWNpFBU3C6Y3nXnQ7CAcREYBNJK3 Ujag== 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:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=sQZeIlEq/gS6yiizJCVJZc2A3YlFyEvgM13z3ol59Xw=; b=mKuDn8FF/IrBHoxO+uRjYDJxNPMbDBZj1hmVVJYLkaC2EYptNWptjDMv7kQPkmVamT B/sBiy2bm8p2kGfe7vBYphA1cwibsnwmH7g38sFFjdrNL5ezqQrTvd5X9CU+v2q2VB1p zCWQR0aX+yYVVaW+agXVxf+h+zPogauNxhWwTfj7bvuHSFuJNRSe5GIP0IG1pAIPAO66 4Ooo2CGpueJWnTYF1KQmf1/8ngvOkKNFZnFXQ9w3Sx3cBeG7IOCV0R9a9Vk83jZ1HgAb +1lwt4GM7OAvYQVuJs17QiSxX88TGvOecBXiW9ndoRDcJyjJSybo24PvIWjPg4l6vAQ0 sgUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=S2FKK9uA; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id hw8-20020a170907a0c800b007a8beb3aa4csi774372ejc.872.2022.10.24.13.55.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Oct 2022 13:55:13 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=S2FKK9uA; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 AE7723858012 for ; Mon, 24 Oct 2022 20:55:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AE7723858012 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666644911; bh=sQZeIlEq/gS6yiizJCVJZc2A3YlFyEvgM13z3ol59Xw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=S2FKK9uAzFXwfFMWbDow45K4zN2yWnbxvm4GXFqGD1zJslZb6sb3GFhfJTj0uQ5PR m73Tv+oSRUEMksmC7qsYTcgKPF26U6XA90Cgu+lLFon/jBXc/JIovowCXcl2foYDVk 2J6+m8VsuH+Y+Wy2MypF2KdVslDn8k0mDvt9omc4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id D1DE73858000 for ; Mon, 24 Oct 2022 20:53:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D1DE73858000 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-77-VRvYH-I5OBacQ-gyA_Juow-1; Mon, 24 Oct 2022 16:53:51 -0400 X-MC-Unique: VRvYH-I5OBacQ-gyA_Juow-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 002F985A5A6 for ; Mon, 24 Oct 2022 20:53:51 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.17.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id BBFBB492B0A; Mon, 24 Oct 2022 20:53:50 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [committed] analyzer: handle (NULL == &VAR) [PR107345] Date: Mon, 24 Oct 2022 16:53:48 -0400 Message-Id: <20221024205348.1760258-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.6 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, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm 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?1747603857470315347?= X-GMAIL-MSGID: =?utf-8?q?1747603857470315347?= Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r13-3468-g18faaeb3af42f3. gcc/analyzer/ChangeLog: PR analyzer/107345 * region-model.cc (region_model::eval_condition_without_cm): Ensure that constants are on the right-hand side before checking for them. gcc/testsuite/ChangeLog: PR analyzer/107345 * gcc.dg/analyzer/pr107345.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/region-model.cc | 15 ++++++++++++--- gcc/testsuite/gcc.dg/analyzer/pr107345.c | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/pr107345.c diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index 608fcd58fab..7c44fc9e253 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -4212,10 +4212,19 @@ region_model::eval_condition_without_cm (const svalue *lhs, /* Otherwise, only known through constraints. */ } - /* If we have a pair of constants, compare them. */ if (const constant_svalue *cst_lhs = lhs->dyn_cast_constant_svalue ()) - if (const constant_svalue *cst_rhs = rhs->dyn_cast_constant_svalue ()) - return constant_svalue::eval_condition (cst_lhs, op, cst_rhs); + { + /* If we have a pair of constants, compare them. */ + if (const constant_svalue *cst_rhs = rhs->dyn_cast_constant_svalue ()) + return constant_svalue::eval_condition (cst_lhs, op, cst_rhs); + else + { + /* When we have one constant, put it on the RHS. */ + std::swap (lhs, rhs); + op = swap_tree_comparison (op); + } + } + gcc_assert (lhs->get_kind () != SK_CONSTANT); /* Handle comparison against zero. */ if (const constant_svalue *cst_rhs = rhs->dyn_cast_constant_svalue ()) diff --git a/gcc/testsuite/gcc.dg/analyzer/pr107345.c b/gcc/testsuite/gcc.dg/analyzer/pr107345.c new file mode 100644 index 00000000000..540596d1182 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/pr107345.c @@ -0,0 +1,17 @@ +/* Ensure the analyzer treats (NULL == &e) as being false for this case, + where the logic is sufficiently complicated to not be optimized away. */ + +#include + +int main() { + int e = 10086; + int *f = &e; + int g = 0; + int *h[2][1]; + h[1][0] = f; + if (g == (h[1][0])) { /* { dg-warning "comparison between pointer and integer" } */ + unsigned int *i = 0; + } + printf("NPD_FLAG: %d\n ", *f); + return 0; +}