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.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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666644832; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=sQZeIlEq/gS6yiizJCVJZc2A3YlFyEvgM13z3ol59Xw=; b=ernZTcabhfmESQ9FeqCelMUHJSRh8+QW06QJ7qZI5637n/ck3EarZayQSYNXZ8u/ApGRjh co/NMwaoyfgGzuO6FsxM9r42rJ1mexu9rwU7IkQfcJQ5FSSsZR6NPClMMRjJ4jevHftFuq dcYOtY3t2JJcpBpLt7Oh6nDPDsavCeM= 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) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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; +} -- 2.26.3