From: David Malcolm <dmalcolm@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: David Malcolm <dmalcolm@redhat.com>
Subject: [committed] analyzer: handle (NULL == &VAR) [PR107345]
Date: Mon, 24 Oct 2022 16:53:48 -0400 [thread overview]
Message-ID: <20221024205348.1760258-1-dmalcolm@redhat.com> (raw)
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 <dmalcolm@redhat.com>
---
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 <stdio.h>
+
+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
reply other threads:[~2022-10-24 20:53 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221024205348.1760258-1-dmalcolm@redhat.com \
--to=dmalcolm@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).