<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58992>58992</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            clang-tidy appears to be unaware that accept4(2) writes its second argument
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          dgibson
      </td>
    </tr>
</table>

<pre>
    clang-tidy gives error about uninitialized data on socket addresses which have, in fact, been initialized by the `accept()` or `accept4()` system calls.  This is awkward to work around, because it requires somehow initializing the socket address before the `accept()` call.  Depending on address type socket addresses can be quite large, so this is potentially costly.

For example running clang tidy on this program:

```
#include <assert.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <sys/socket.h>

int main(void)
{
        int fd, rc;
        struct sockaddr_in sa;
        socklen_t sl = sizeof(sa);

        fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        assert(fd >= 0);

        rc = listen(fd, 0);
        assert(rc == 0);

        rc = accept4(fd, &sa, &sl, SOCK_CLOEXEC);
        assert(rc == 0);

        printf("sin_family=%d\n", sa.sin_family);

        return 0;
}
```

gives this error:

```
$ clang-tidy -checks=*,-llvmlibc-restrict-system-libc-headers,-readability-identifier-length,-readability-isolate-declaration,-cppcoreguidelines-init-variables clang-tidy-accept.c --  -D_GNU_SOURCE=1
3213 warnings generated.
/home/dwg/tmp/clang-tidy-accept.c:21:2: warning: 2nd function call argument is an uninitialized value [clang-analyzer-core.CallAndMessage]
        printf("sin_family=%d\n", sa.sin_family);
        ^                         ~~~~~~~~~~~~~
/home/dwg/tmp/clang-tidy-accept.c:13:9: note: Assuming 'fd' is >= 0
        assert(fd >= 0);
               ^
/usr/include/assert.h:106:11: note: expanded from macro 'assert'
      if (expr)                                                         \
          ^~~~
/home/dwg/tmp/clang-tidy-accept.c:13:2: note: Taking true branch
        assert(fd >= 0);
        ^
/usr/include/assert.h:106:7: note: expanded from macro 'assert'
      if (expr)                                                         \
      ^
/home/dwg/tmp/clang-tidy-accept.c:16:9: note: Assuming 'rc' is equal to 0
        assert(rc == 0);
               ^
/usr/include/assert.h:106:11: note: expanded from macro 'assert'
      if (expr)                                                         \
          ^~~~
/home/dwg/tmp/clang-tidy-accept.c:16:2: note: Taking true branch
        assert(rc == 0);
        ^
/usr/include/assert.h:106:7: note: expanded from macro 'assert'
      if (expr)                                                         \
      ^
/home/dwg/tmp/clang-tidy-accept.c:19:9: note: Assuming 'rc' is equal to 0
        assert(rc == 0);
               ^
/usr/include/assert.h:106:11: note: expanded from macro 'assert'
      if (expr)                                                         \
          ^~~~
/home/dwg/tmp/clang-tidy-accept.c:19:2: note: Taking true branch
        assert(rc == 0);
        ^
/usr/include/assert.h:106:7: note: expanded from macro 'assert'
      if (expr)                                                         \
      ^
/home/dwg/tmp/clang-tidy-accept.c:21:2: note: 2nd function call argument is an uninitialized value
        printf("sin_family=%d\n", sa.sin_family);
        ^                         ~~~~~~~~~~~~~
Suppressed 3212 warnings (3212 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWN1z2jgQ_2vMi8aMkfnyAw8UyE3nrk0mITN9Y2RJtnURkk-SofSvv5UMgaRwSZN76EzLGPlDq92fVj-td51rtptQSVQZO8F2qBQbbhE3RhtEct041CihhBNEim-cIUYcQVohq-kDd4gwZri1MGRbCVqhimx4hGdIKFQQ6vxlzrlCpyryHXIVR9EwIZTyGoTGEc7gFoHNx6f942O7s46vESVS2i5Cy0pYBAfZPmyJYchptNXmARGjG8Vak5Q0liPhkOH_NAIgAuA1r_T2iESoMuB4OhMYW2jDLyH0GADCnNdcMa8BXHEY6Xb1c21glxIFOhGgcBxJYsrgH6vBQjuNWjuuPCS5Q1RbJ3fdKJlHybRtr8Ap_CtZ15Ij0yjljYb1QmG9wH5QVB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