analyzer: implement five new warnings for misuse of POSIX file descriptor APIs [PR106...
authorImmad Mir <mirimmad17@gmail.com>
Sat, 2 Jul 2022 16:39:37 +0000 (22:09 +0530)
committerImmad Mir <mirimmad17@gmail.com>
Sat, 2 Jul 2022 16:44:26 +0000 (22:14 +0530)
commit97baacba963c06e3d0e33cde04e7e687671e60e7
treed6ffcc9ecb70cc37ca2f55e11834ff8b942f1edd
parented974488991256c50f151ccfb271e198072dfc4d
analyzer: implement five new warnings for misuse of POSIX file descriptor APIs [PR106003].

This patch adds a new state machine to the analyzer for checking usage of POSIX file descriptor
APIs with five new warnings.

It adds:
- check for FD leaks (CWE 775).
- check for double "close" of a FD (CWE-1341).
- check for read/write of a closed file descriptor.
- check whether a file descriptor was used without being checked for validity.
- check for read/write of a descriptor opened for just writing/reading.

gcc/ChangeLog:
PR analyzer/106003
* Makefile.in (ANALYZER_OBJS): Add sm-fd.o.
* doc/invoke.texi:  Add -Wanalyzer-fd-double-close, -Wanalyzer-fd-leak,
-Wanalyzer-fd-access-mode-mismatch, -Wanalyzer-fd-use-without-check,
-Wanalyzer-fd-use-after-close.

gcc/analyzer/ChangeLog:
PR analyzer/106003
* analyzer.opt (Wanalyzer-fd-leak): New option.
(Wanalyzer-fd-access-mode-mismatch): New option.
(Wanalyzer-fd-use-without-check): New option.
(Wanalyzer-fd-double-close): New option.
(Wanalyzer-fd-use-after-close): New option.
* sm.h (make_fd_state_machine): New decl.
* sm.cc (make_checkers): Call make_fd_state_machine.
* sm-fd.cc: New file.

gcc/testsuite/ChangeLog:
PR analyzer/106003
* gcc.dg/analyzer/fd-1.c: New test.
* gcc.dg/analyzer/fd-2.c: New test.
* gcc.dg/analyzer/fd-3.c: New test.
* gcc.dg/analyzer/fd-4.c: New test.
gcc/Makefile.in
gcc/analyzer/analyzer.opt
gcc/analyzer/sm-fd.cc [new file with mode: 0644]
gcc/analyzer/sm.cc
gcc/analyzer/sm.h
gcc/doc/invoke.texi
gcc/testsuite/gcc.dg/analyzer/fd-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/analyzer/fd-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/analyzer/fd-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/analyzer/fd-4.c [new file with mode: 0644]