public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rjones at redhat dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/99196] New: GCC analyzer doesn't know that error (code != 0, ...) exits the program
Date: Mon, 22 Feb 2021 10:32:06 +0000	[thread overview]
Message-ID: <bug-99196-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 99196
           Summary: GCC analyzer doesn't know that error (code != 0, ...)
                    exits the program
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rjones at redhat dot com
  Target Milestone: ---

https://github.com/libguestfs/libguestfs/blob/f19fd566f6387ce7e4d82409528c9dde374d25e0/daemon/tar.c#L108

tar.c: In function 'read_error_file':
tar.c:113:11: error: use of NULL 'str' where non-null expected [CWE-476]
[-Werror=analyzer-null-argument]
  113 |     len = strlen (str);
      |           ^~~~~~~~~~~~
  'read_error_file': events 1-7
    |
    |  109 |   if (str == NULL) {
    |      |      ^
    |      |      |
    |      |      (1) following 'true' branch (when 'str' is NULL)...
    |  110 |     str = strdup ("(no error)");
    |      |     ~~~   ~~~~~~~~~~~~~~~~~~~~~
    |      |     |     |
    |      |     |     (3) allocated here
    |      |     (2) ...to here
    |  111 |     if (str == NULL)
    |      |        ~
    |      |        |
    |      |        (4) assuming 'str' is NULL
    |      |        (5) following 'true' branch (when 'str' is NULL)...
    |  112 |       error (EXIT_FAILURE, errno, "strdup"); /* XXX */
    |      |       ~~~~~
    |      |       |
    |      |       (6) ...to here
    |  113 |     len = strlen (str);
    |      |           ~~~~~~~~~~~~
    |      |           |
    |      |           (7) argument 1 ('str') NULL where non-null expected
    |
In file included from ../gnulib/lib/string.h:41,
                 from tar.c:23:
/usr/include/string.h:391:15: note: argument 1 of 'strlen' must be non-null
  391 | extern size_t strlen (const char *__s)
      |               ^~~~~~
cc1: all warnings being treated as errors

In the original code if str == NULL, error (EXIT_FAILURE, ...) is
called which exits the program.  Therefore strlen (NULL) cannot
be called so the warning is bogus.

https://www.man7.org/linux/man-pages/man3/error.3.html

       "If status has a nonzero value, then error() calls exit(3) to
       terminate the program using the given value as the exit status."

gcc-11.0.0-0.19.fc35.x86_64

             reply	other threads:[~2021-02-22 10:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-22 10:32 rjones at redhat dot com [this message]
2021-02-22 15:22 ` [Bug analyzer/99196] " msebor at gcc dot gnu.org
2021-02-22 16:53 ` dmalcolm at gcc dot gnu.org
2021-02-22 17:18 ` rjones at redhat dot com
2021-02-22 23:47 ` cvs-commit at gcc dot gnu.org
2021-02-22 23:48 ` 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-99196-4@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).