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 via email to