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

            Bug ID: 129095
           Summary: Improve precision for analyzer event messages from
                    succeed_or_fail_call_info subclasses
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: dmalcolm at gcc dot gnu.org
            Blocks: 128964
  Target Milestone: ---

https://github.com/CASTLE-Benchmark/CASTLE-Benchmark/issues/5 notes an
unexpected but IMHO true positive on CASTLE-617-7.c from -fanalyzer due to the
benchmark checking the return value of 'socket' against 0, rather than -1.

The code is:
 
https://raw.githubusercontent.com/CASTLE-Benchmark/CASTLE-Benchmark/refs/heads/main/datasets/CASTLE-C250/CASTLE-617-7.c

https://godbolt.org/z/jonfoM7KE

Currently we emit 

<source>: In function 'main':
<source>:50:9: warning: 'bind' on possibly invalid file descriptor 'server_fd'
[-Wanalyzer-fd-use-without-check]
   50 |     if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) <
0) {
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  'main': event 1
   40 |     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                      |
      |                      (1) when 'socket' fails
  'main': event 2
   40 |     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
      |        ^
      |        |
      |        (2) following 'false' branch (when 'server_fd != 0')... ─>─┐
      |                                                                   │
  'main': events 3-4
      |                                                                   │
      |┌──────────────────────────────────────────────────────────────────┘
   46 |│    address.sin_family = AF_INET;
      |│                       ^
      |│                       |
      |└──────────────────────>(3) ...to here
......
   50 |     if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) <
0) {
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |         |
      |         (4) ⚠️  'server_fd' could be invalid

The wording of the events could be improved here:

* At the 'socket' call:
    (1) when 'socket' fails
  would be better as
    (1) when 'socket' fails (returning -1)
  specifying the return value, or even change the "when" to "if":
    (1) if 'socket' fails (returning -1)
* At the conditional:
    (2) following 'false' branch (when 'server_fd != 0')...
  would be better as
    (2) following 'false' branch (since 'server_fd != 0')...
  since on this path we know the value of server_fd and it's implied by event
(1).
  Possibly even:
    (2) 'server_fd' == '-1' from (1)" or somesuch
    (3) following 'false' branch (since 'server_fd != 0')...

* At the 'bind' call on this path:
    (4) ⚠️  'server_fd' could be invalid
  could be:
    (4) ⚠️  'server_fd' is invalid (-1)
  or somesuch

In sm-fd.cc, kf_socket bifurcates, and fd_state_machine::on_socket has the
logic that sets the return value to -1 on failure, >= 0 on success.

Re event (1), this could be generalized to all known functions that bifurcate
where we know something about the return value, so all such subclasses of
succeed_or_fail_call_info could (optionally) provide info on the return value,
for use by succeed_or_fail_call_info::print_desc (or simply override it for
such cases).
Potentially we could also add URLification to 'socket' in that message to point
at e.g.
  https://www.man7.org/linux/man-pages/man2/socket.2.html#RETURN_VALUE

In event (4), looks like this comes from fd_use_without_check::emit; we can
probably make the wording more precise for the "known invalid" case, rather
than just "could be invalid"


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128964
[Bug 128964] GCC -fanalyzer bugs and RFEs related to CASTLE benchmark

Reply via email to