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

            Bug ID: 114677
           Summary: apparent -Wanalyzer-fd-leak  false positive
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: levon at movementarian dot org
  Target Milestone: ---

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

https://godbolt.org/z/PfKKbPhdP reproduced with today's trunk

Also attached

gives

<source>: In function 'try_bind_socket':
<source>:37:8: warning: leak of file descriptor '*sock' [CWE-775]
[-Wanalyzer-fd-leak]
   37 |     if (err != 0) {
      |        ^
  'try_bind_socket': events 1-8
    |
    |   20 |     *sock = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
    |      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |             |
    |      |             (1) when 'socket' succeeds
    |   21 |     if (*sock < 0) {
    |      |        ~     
    |      |        |
    |      |        (2) following 'false' branch...
    |......
    |   25 |     if (setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &(int){1},
sizeof(int)) < 0) {
    |      |        ~                                                  ~
    |      |        |                                                  |
    |      |        |                                                  (3)
...to here
    |      |        (4) following 'false' branch...
    |......
    |   29 |     err = bind(*sock, (struct sockaddr *)&addr, sizeof(addr));
    |      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |           |
    |      |           (5) ...to here
    |   30 | 
    |   31 |     if (err != 0) {
    |      |        ~     
    |      |        |
    |      |        (6) following 'false' branch (when 'err == 0')...
    |......
    |   35 |     err = listen(*sock, backlog);
    |      |           ~~~~~~~~~~~~~~~~~~~~~~
    |      |           |
    |      |           (7) ...to here
    |   36 | 
    |   37 |     if (err != 0) {
    |      |        ~     
    |      |        |
    |      |        (8) '*sock' leaks here
    |


this happens regardless of whether we have the (incorrect) close on line 41 or
not

Reply via email to