facebook / facebook/infer

False positive: RESOURCE_LEAK lang=C++ simple reproducer with socket and pollfd in method

Open
#951 3 comments 0 reactions 0 assignees View on GitHub
c++ false-positive
Dominant language
OCaml
Stars
15.7k
Forks
2.1k
Avg merge
19h 36m
Merged PRs (30d)
13

Description

Hello and thank you for infer,
I ran `infer` on a code base and think it is reporting a false positive, I have narrowed down the code into a simple reproducer which I include below. Thanks.

OS and versions:

```
> uname -a
Linux 4.4.0-87-generic #110-Ubuntu SMP x86_64 x86_64 x86_64 GNU/Linux

> infer --version
Infer version v0.15.0
Copyright 2009 - present Facebook. All Rights Reserved.

> clang++ --version
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
```

Output using the `breaks` `Makefile` target which defines `BREAKS`, note that when `BREAKS` is not defined and the 2 lines from `wait_for_connect()` are inlined - no issues are found.

```
> make clean ; infer run -- make breaks
rm -f my_socket
rm -f my_socket_breaks
Capturing in make/cc mode...
clang++ -DBREAKS=1 -o my_socket_breaks main.cc my_socket.cc
Found 2 source files to analyze in .../infer-out
Starting analysis...

legend:
"F" analyzing a file
"." analyzing a procedure

FF......
Found 1 issue

my_socket.cc:19: error: RESOURCE_LEAK
resource acquired by call to `socket()` at line 17, column 10 is not released after line 19, column 3.
17. m_fd = ::socket(AF_INET, SOCK_STREAM, 0);
18. #if defined BREAKS
19. > wait_for_connect();
20. #else
21. struct pollfd fds[1];

Summary of the reports

RESOURCE_LEAK: 1
```

Source:

```
> cat my_socket.h
namespace MY {

class Socket {
public:
Socket();
~Socket(){};
void connect();

protected:
int m_fd;
private:
void wait_for_connect();
};

}
```

```
>cat my_socket.cc
#include
#include
#include
#include
#include "my_socket.h"

using MY::Socket;

Socket::Socket() : m_fd(-1) {}

void Socket::wait_for_connect() {
struct pollfd fds[1];
fds[0].fd = m_fd;
}

void Socket::connect() {
m_fd = ::socket(AF_INET, SOCK_STREAM, 0);

#if defined BREAKS
wait_for_connect();
#else
struct pollfd fds[1];
fds[0].fd = m_fd;
#endif

::close(m_fd);
}
```

```
> cat Makefile
my_socket: my_socket.cc
clang++ -o my_socket main.cc my_socket.cc

breaks: my_socket.cc
clang++ -DBREAKS=1 -o my_socket_breaks main.cc my_socket.cc

clean:
rm -f my_socket
rm -f my_socket_breaks
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.