public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug analyzer/106325] -Wanalyzer-null-dereference false positive due to analyzer not making assumptions for  `__attribute__((nonnull))`
Date: Wed, 29 Mar 2023 18:18:51 +0000	[thread overview]
Message-ID: <bug-106325-4-2npInbnBxa@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-106325-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106325

--- Comment #9 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-12 branch has been updated by David Malcolm
<dmalcolm@gcc.gnu.org>:

https://gcc.gnu.org/g:02fbda165b74179469d9eae436fed613aa6a6ebb

commit r12-9362-g02fbda165b74179469d9eae436fed613aa6a6ebb
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Wed Mar 29 14:16:48 2023 -0400

    analyzer: use __attribute__((nonnull)) at top level of analysis [PR106325]

    PR analyzer/106325 reports false postives from
    -Wanalyzer-null-dereference on code like this:

    __attribute__((nonnull))
    void foo_a (Foo *p)
    {
      foo_b (p);

      switch (p->type)
        {
          /* ... */
        }
    }

    where foo_b (p) has a:

      g_return_if_fail (p);

    that expands to:

      if (!p)
        {
          return;
        }

    The analyzer "sees" the comparison against NULL in foo_b, and splits the
    analysis into the NULL and not-NULL cases; later, back in foo_a,  at
      switch (p->type)
    it complains that p is NULL.

    Previously we were only using __attribute__((nonnull)) as something to
    complain about when it was violated; we weren't using it as a source of
    knowledge.

    This patch fixes things by making the analyzer respect
    __attribute__((nonnull)) at the top-level of the analysis: any such
    params are now assumed to be non-NULL, so that the analyzer assumes the
    g_return_if_fail inside foo_b doesn't fail when called from foo_a

    Doing so fixes the false positives.

    Backported from r13-4520-gdcfc7ac94dbcf6.

    gcc/analyzer/ChangeLog:
            PR analyzer/106325
            * region-model-manager.cc
            (region_model_manager::get_or_create_null_ptr): New.
            * region-model.cc (region_model::on_top_level_param): Add
            "nonnull" param and make use of it.
            (region_model::push_frame): When handling a top-level entrypoint
            to the analysis, determine which params __attribute__((nonnull))
            applies to, and pass to on_top_level_param.
            * region-model.h (region_model_manager::get_or_create_null_ptr):
            New decl.
            (region_model::on_top_level_param): Add "nonnull" param.

    gcc/testsuite/ChangeLog:
            PR analyzer/106325
            * gcc.dg/analyzer/attr-nonnull-pr106325.c: New test.
            * gcc.dg/analyzer/attribute-nonnull.c (test_6): New.
            (test_7): New.

    Signed-off-by: David Malcolm <dmalcolm@redhat.com>

  parent reply	other threads:[~2023-03-29 18:18 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-16 13:11 [Bug analyzer/106325] New: `analyzer-null-dereference` gets thrown on a parameter on a function marked with `__attribute__((nonnull))` alex at zrythm dot org
2022-07-16 13:14 ` [Bug analyzer/106325] " alex at zrythm dot org
2022-07-16 13:25 ` alex at zrythm dot org
2022-07-16 15:36 ` dmalcolm at gcc dot gnu.org
2022-12-05 22:58 ` [Bug analyzer/106325] -Wanalyzer-null-dereference false positive due to analyzer not making assumptions for `__attribute__((nonnull))` dmalcolm at gcc dot gnu.org
2022-12-05 23:00 ` dmalcolm at gcc dot gnu.org
2022-12-05 23:08 ` dmalcolm at gcc dot gnu.org
2022-12-06 18:28 ` cvs-commit at gcc dot gnu.org
2022-12-06 18:56 ` dmalcolm at gcc dot gnu.org
2023-03-29 18:18 ` cvs-commit at gcc dot gnu.org [this message]
2023-03-29 19:29 ` dmalcolm at gcc dot gnu.org

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=bug-106325-4-2npInbnBxa@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).