From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2209) id 8E20D3857BBF; Mon, 24 Oct 2022 20:49:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8E20D3857BBF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666644553; bh=QzFo+ktisgQd4X/zxYIhFkRsHZy1g/SpxgBtwInx9/k=; h=From:To:Subject:Date:From; b=KOgx2hrRCULxOuRp6vr80fKgSEgbv/K3vjqk0VA7HrLzxRtjO3SjZq1673FQudjfp ZyFLkq4LpXZovVFAudY91ZvFqVv4CKR6NsmweKlmvKIotz721b5I04V+Zcb0VkR+5p 5UlzOmNZIs6kZEhW0Uuce1oOxqpCaSZ8D+LDbUUI= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: David Malcolm To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3468] analyzer: handle (NULL == &VAR) [PR107345] X-Act-Checkin: gcc X-Git-Author: David Malcolm X-Git-Refname: refs/heads/master X-Git-Oldrev: 53881c47e4b3574e2cb2046a6cb154c87a9836b6 X-Git-Newrev: 18faaeb3af42f3d7dc609b9b91df77d0d59b16f6 Message-Id: <20221024204913.8E20D3857BBF@sourceware.org> Date: Mon, 24 Oct 2022 20:49:13 +0000 (GMT) List-Id: https://gcc.gnu.org/g:18faaeb3af42f3d7dc609b9b91df77d0d59b16f6 commit r13-3468-g18faaeb3af42f3d7dc609b9b91df77d0d59b16f6 Author: David Malcolm Date: Mon Oct 24 16:38:23 2022 -0400 analyzer: handle (NULL == &VAR) [PR107345] 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 Diff: --- gcc/analyzer/region-model.cc | 15 ++++++++++++--- gcc/testsuite/gcc.dg/analyzer/pr107345.c | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) 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; +}