facebook / facebook/infer

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

Ouverte
#951 3 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
c++ false-positive
Langage dominant
OCaml
Étoiles
15.7k
Forks
2.1k
Merge moyen
19 h 36 min
PR mergées (30 j)
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
```

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.