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

            Bug ID: 108633
           Summary: -Wanalyzer-fd-type-mismatch erroneously emitted on
                    missing error-checking in qemu's
                    tests/qtest/libqtest.c
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: dmalcolm at gcc dot gnu.org
  Target Milestone: ---

Created attachment 54388
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54388&action=edit
Reduced reproducer

https://godbolt.org/z/cPr7c8n9f

Trunk -fanalyzer emits two warnings:
<source>: In function 'qtest_socket_server':
<source>:75:11: warning: 'listen' on non-stream-socket file descriptor 'sock'
[-Wanalyzer-fd-type-mismatch]
   75 |     ret = listen(sock, 1);
      |           ^~~~~~~~~~~~~~~
  'qtest_socket_server': event 1
    |
    |   65 |     sock = socket(1, SOCK_STREAM, 0);
    |      |            ^~~~~~~~~~~~~~~~~~~~~~~~~
    |      |            |
    |      |            (1) when 'socket' fails
    |
  'qtest_socket_server': event 2
    |
    |    8 |     if (__n1 cmp __n2) ; else                                     
     \
    |      |        ^
    |      |        |
    |      |        (2) following 'false' branch (when '__n1 == __n2')...
<source>:66:5: note: in expansion of macro 'g_assert_cmpint'
    |   66 |     g_assert_cmpint(sock, !=, -1);
    |      |     ^~~~~~~~~~~~~~~
    |
  'qtest_socket_server': event 3
    |
    |    9 |       g_assertion_message_cmpnum ("", __FILE__, __LINE__,
__func__, \
    |      |      
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |       |
    |      |       (3) ...to here
    |   10 |                                   #n1 " " #cmp " " #n2, (long
double) __n1, #cmp, (long double) __n2, 'i'); \
    |      |                                  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:66:5: note: in expansion of macro 'g_assert_cmpint'
    |   66 |     g_assert_cmpint(sock, !=, -1);
    |      |     ^~~~~~~~~~~~~~~
    |
  'qtest_socket_server': event 4
    |
    |   75 |     ret = listen(sock, 1);
    |      |           ^~~~~~~~~~~~~~~
    |      |           |
    |      |           (4) 'listen' expects a socket file descriptor but 'sock'
is not a socket
    |
<source>:75:11: warning: 'listen' on possibly invalid file descriptor 'sock'
[-Wanalyzer-fd-use-without-check]
  'qtest_socket_server': event 1
    |
    |   65 |     sock = socket(1, SOCK_STREAM, 0);
    |      |            ^~~~~~~~~~~~~~~~~~~~~~~~~
    |      |            |
    |      |            (1) when 'socket' fails
    |
  'qtest_socket_server': event 2
    |
    |    8 |     if (__n1 cmp __n2) ; else                                     
     \
    |      |        ^
    |      |        |
    |      |        (2) following 'false' branch (when '__n1 == __n2')...
<source>:66:5: note: in expansion of macro 'g_assert_cmpint'
    |   66 |     g_assert_cmpint(sock, !=, -1);
    |      |     ^~~~~~~~~~~~~~~
    |
  'qtest_socket_server': event 3
    |
    |    9 |       g_assertion_message_cmpnum ("", __FILE__, __LINE__,
__func__, \
    |      |      
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |       |
    |      |       (3) ...to here
    |   10 |                                   #n1 " " #cmp " " #n2, (long
double) __n1, #cmp, (long double) __n2, 'i'); \
    |      |                                  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:66:5: note: in expansion of macro 'g_assert_cmpint'
    |   66 |     g_assert_cmpint(sock, !=, -1);
    |      |     ^~~~~~~~~~~~~~~
    |
  'qtest_socket_server': event 4
    |
    |   75 |     ret = listen(sock, 1);
    |      |           ^~~~~~~~~~~~~~~
    |      |           |
    |      |           (4) 'sock' could be invalid
    |

The g_assert_cmpint doesn't have "noreturn", so it's considering the case where
socket fails, returning -1.

The first diagnostic:
  "warning: 'listen' on non-stream-socket file descriptor 'sock'
[-Wanalyzer-fd-type-mismatch]"
is poorly-worded; I *think* it's complaining about listen on -1, but the
wording is very confusing, as it's clearly been created with SOCK_STREAM.

Reply via email to