[Analyzer][StreamChecker] Add note tags for file opening.
Summary:
Bug reports of resource leak are now improved.
If there are multiple resource leak paths for the same stream,
only one wil be reported.
Reviewers: Szelethus, xazax.hun, baloghadamsoftware, NoQ
Reviewed By: Szelethus, NoQ
Subscribers: NoQ, rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, ASDenysPetrov, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81407
2020-06-22 15:04:05 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.unix.Stream -verify %s
|
2010-06-16 13:38:05 +08:00
|
|
|
|
[Analyzer][StreamChecker] Introduction of stream error handling.
Summary:
Store the error flags (EOF or error) of a stream.
Support the functions feof, ferror, clearerr.
Added a test checker for setting the error flags.
Reviewers: Szelethus, NoQ, Charusso, baloghadamsoftware, xazax.hun
Reviewed By: Szelethus
Subscribers: steakhal, ASDenysPetrov, rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75682
2020-04-08 14:52:24 +08:00
|
|
|
#include "Inputs/system-header-simulator.h"
|
2010-06-16 13:38:05 +08:00
|
|
|
|
[clang][analyzer] Using CallDescription in StreamChecker.
Summary:
Recognization of function names is done now with the CallDescription
class instead of using IdentifierInfo. This means function name and
argument count is compared too.
A new check for filtering not global-C-functions was added.
Test was updated.
Reviewers: Szelethus, NoQ, baloghadamsoftware, Charusso
Reviewed By: Szelethus, NoQ, Charusso
Subscribers: rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, donat.nagy, Charusso, dkrupp, Szelethus, gamesh411, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D67706
2019-10-31 19:09:44 +08:00
|
|
|
void check_fread() {
|
|
|
|
FILE *fp = tmpfile();
|
|
|
|
fread(0, 0, 0, fp); // expected-warning {{Stream pointer might be NULL}}
|
|
|
|
fclose(fp);
|
2010-06-16 13:38:05 +08:00
|
|
|
}
|
2010-06-18 10:47:46 +08:00
|
|
|
|
[clang][analyzer] Using CallDescription in StreamChecker.
Summary:
Recognization of function names is done now with the CallDescription
class instead of using IdentifierInfo. This means function name and
argument count is compared too.
A new check for filtering not global-C-functions was added.
Test was updated.
Reviewers: Szelethus, NoQ, baloghadamsoftware, Charusso
Reviewed By: Szelethus, NoQ, Charusso
Subscribers: rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, donat.nagy, Charusso, dkrupp, Szelethus, gamesh411, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D67706
2019-10-31 19:09:44 +08:00
|
|
|
void check_fwrite() {
|
|
|
|
FILE *fp = tmpfile();
|
|
|
|
fwrite(0, 0, 0, fp); // expected-warning {{Stream pointer might be NULL}}
|
|
|
|
fclose(fp);
|
2010-06-18 10:47:46 +08:00
|
|
|
}
|
|
|
|
|
[clang][analyzer] Using CallDescription in StreamChecker.
Summary:
Recognization of function names is done now with the CallDescription
class instead of using IdentifierInfo. This means function name and
argument count is compared too.
A new check for filtering not global-C-functions was added.
Test was updated.
Reviewers: Szelethus, NoQ, baloghadamsoftware, Charusso
Reviewed By: Szelethus, NoQ, Charusso
Subscribers: rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, donat.nagy, Charusso, dkrupp, Szelethus, gamesh411, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D67706
2019-10-31 19:09:44 +08:00
|
|
|
void check_fseek() {
|
|
|
|
FILE *fp = tmpfile();
|
|
|
|
fseek(fp, 0, 0); // expected-warning {{Stream pointer might be NULL}}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_ftell() {
|
|
|
|
FILE *fp = tmpfile();
|
|
|
|
ftell(fp); // expected-warning {{Stream pointer might be NULL}}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_rewind() {
|
|
|
|
FILE *fp = tmpfile();
|
|
|
|
rewind(fp); // expected-warning {{Stream pointer might be NULL}}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_fgetpos() {
|
|
|
|
FILE *fp = tmpfile();
|
|
|
|
fpos_t pos;
|
|
|
|
fgetpos(fp, &pos); // expected-warning {{Stream pointer might be NULL}}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_fsetpos() {
|
|
|
|
FILE *fp = tmpfile();
|
|
|
|
fpos_t pos;
|
|
|
|
fsetpos(fp, &pos); // expected-warning {{Stream pointer might be NULL}}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_clearerr() {
|
|
|
|
FILE *fp = tmpfile();
|
|
|
|
clearerr(fp); // expected-warning {{Stream pointer might be NULL}}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_feof() {
|
|
|
|
FILE *fp = tmpfile();
|
|
|
|
feof(fp); // expected-warning {{Stream pointer might be NULL}}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_ferror() {
|
|
|
|
FILE *fp = tmpfile();
|
|
|
|
ferror(fp); // expected-warning {{Stream pointer might be NULL}}
|
|
|
|
fclose(fp);
|
2010-06-18 10:47:46 +08:00
|
|
|
}
|
|
|
|
|
[clang][analyzer] Using CallDescription in StreamChecker.
Summary:
Recognization of function names is done now with the CallDescription
class instead of using IdentifierInfo. This means function name and
argument count is compared too.
A new check for filtering not global-C-functions was added.
Test was updated.
Reviewers: Szelethus, NoQ, baloghadamsoftware, Charusso
Reviewed By: Szelethus, NoQ, Charusso
Subscribers: rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, donat.nagy, Charusso, dkrupp, Szelethus, gamesh411, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D67706
2019-10-31 19:09:44 +08:00
|
|
|
void check_fileno() {
|
|
|
|
FILE *fp = tmpfile();
|
|
|
|
fileno(fp); // expected-warning {{Stream pointer might be NULL}}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void f_open(void) {
|
2010-06-18 10:47:46 +08:00
|
|
|
FILE *p = fopen("foo", "r");
|
[clang][analyzer] Using CallDescription in StreamChecker.
Summary:
Recognization of function names is done now with the CallDescription
class instead of using IdentifierInfo. This means function name and
argument count is compared too.
A new check for filtering not global-C-functions was added.
Test was updated.
Reviewers: Szelethus, NoQ, baloghadamsoftware, Charusso
Reviewed By: Szelethus, NoQ, Charusso
Subscribers: rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, donat.nagy, Charusso, dkrupp, Szelethus, gamesh411, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D67706
2019-10-31 19:09:44 +08:00
|
|
|
char buf[1024];
|
|
|
|
fread(buf, 1, 1, p); // expected-warning {{Stream pointer might be NULL}}
|
2010-07-23 22:14:59 +08:00
|
|
|
fclose(p);
|
2010-06-18 10:47:46 +08:00
|
|
|
}
|
|
|
|
|
[clang][analyzer] Using CallDescription in StreamChecker.
Summary:
Recognization of function names is done now with the CallDescription
class instead of using IdentifierInfo. This means function name and
argument count is compared too.
A new check for filtering not global-C-functions was added.
Test was updated.
Reviewers: Szelethus, NoQ, baloghadamsoftware, Charusso
Reviewed By: Szelethus, NoQ, Charusso
Subscribers: rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, donat.nagy, Charusso, dkrupp, Szelethus, gamesh411, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D67706
2019-10-31 19:09:44 +08:00
|
|
|
void f_seek(void) {
|
2010-06-24 21:36:41 +08:00
|
|
|
FILE *p = fopen("foo", "r");
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
fseek(p, 1, SEEK_SET); // no-warning
|
Allow multiple PathDiagnosticConsumers to be used with a BugReporter at the same time.
This fixes several issues:
- removes egregious hack where PlistDiagnosticConsumer would forward to HTMLDiagnosticConsumer,
but diagnostics wouldn't be generated consistently in the same way if PlistDiagnosticConsumer
was used by itself.
- emitting diagnostics to the terminal (using clang's diagnostic machinery) is no longer a special
case, just another PathDiagnosticConsumer. This also magically resolved some duplicate warnings,
as we now use PathDiagnosticConsumer's diagnostic pruning, which has scope for the entire translation
unit, not just the scope of a BugReporter (which is limited to a particular ExprEngine).
As an interesting side-effect, diagnostics emitted to the terminal also have their trailing "." stripped,
just like with diagnostics emitted to plists and HTML. This required some tests to be updated, but now
the tests have higher fidelity with what users will see.
There are some inefficiencies in this patch. We currently generate the report graph (from the ExplodedGraph)
once per PathDiagnosticConsumer, which is a bit wasteful, but that could be pulled up higher in the
logic stack. There is some intended duplication, however, as we now generate different PathDiagnostics (for the same issue)
for different PathDiagnosticConsumers. This is necessary to produce the diagnostics that a particular
consumer expects.
llvm-svn: 162028
2012-08-17 01:45:23 +08:00
|
|
|
fseek(p, 1, 3); // expected-warning {{The whence argument to fseek() should be SEEK_SET, SEEK_END, or SEEK_CUR}}
|
2010-07-23 22:14:59 +08:00
|
|
|
fclose(p);
|
2010-06-24 21:36:41 +08:00
|
|
|
}
|
2010-07-19 09:52:29 +08:00
|
|
|
|
[clang][analyzer] Using CallDescription in StreamChecker.
Summary:
Recognization of function names is done now with the CallDescription
class instead of using IdentifierInfo. This means function name and
argument count is compared too.
A new check for filtering not global-C-functions was added.
Test was updated.
Reviewers: Szelethus, NoQ, baloghadamsoftware, Charusso
Reviewed By: Szelethus, NoQ, Charusso
Subscribers: rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, donat.nagy, Charusso, dkrupp, Szelethus, gamesh411, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D67706
2019-10-31 19:09:44 +08:00
|
|
|
void f_double_close(void) {
|
2010-07-19 09:52:29 +08:00
|
|
|
FILE *p = fopen("foo", "r");
|
[Analyzer][StreamChecker] Check for opened stream before operations.
Summary:
According to documentations, after an `fclose` call any other stream
operations cause undefined behaviour, regardless if the close failed
or not.
This change adds the check for the opened state before all other
(applicable) operations.
Reviewers: Szelethus
Reviewed By: Szelethus
Subscribers: xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75614
2020-03-09 17:15:31 +08:00
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
fclose(p);
|
|
|
|
fclose(p); // expected-warning {{Stream might be already closed}}
|
2010-07-19 09:52:29 +08:00
|
|
|
}
|
2010-07-22 22:01:01 +08:00
|
|
|
|
2019-12-05 00:15:03 +08:00
|
|
|
void f_double_close_alias(void) {
|
|
|
|
FILE *p1 = fopen("foo", "r");
|
[Analyzer][StreamChecker] Check for opened stream before operations.
Summary:
According to documentations, after an `fclose` call any other stream
operations cause undefined behaviour, regardless if the close failed
or not.
This change adds the check for the opened state before all other
(applicable) operations.
Reviewers: Szelethus
Reviewed By: Szelethus
Subscribers: xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75614
2020-03-09 17:15:31 +08:00
|
|
|
if (!p1)
|
|
|
|
return;
|
2019-12-05 00:15:03 +08:00
|
|
|
FILE *p2 = p1;
|
|
|
|
fclose(p1);
|
[Analyzer][StreamChecker] Check for opened stream before operations.
Summary:
According to documentations, after an `fclose` call any other stream
operations cause undefined behaviour, regardless if the close failed
or not.
This change adds the check for the opened state before all other
(applicable) operations.
Reviewers: Szelethus
Reviewed By: Szelethus
Subscribers: xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75614
2020-03-09 17:15:31 +08:00
|
|
|
fclose(p2); // expected-warning {{Stream might be already closed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void f_use_after_close(void) {
|
|
|
|
FILE *p = fopen("foo", "r");
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
fclose(p);
|
|
|
|
clearerr(p); // expected-warning {{Stream might be already closed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void f_open_after_close(void) {
|
|
|
|
FILE *p = fopen("foo", "r");
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
fclose(p);
|
|
|
|
p = fopen("foo", "r");
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
fclose(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void f_reopen_after_close(void) {
|
|
|
|
FILE *p = fopen("foo", "r");
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
fclose(p);
|
|
|
|
// Allow reopen after close.
|
|
|
|
p = freopen("foo", "w", p);
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
fclose(p);
|
2019-12-05 00:15:03 +08:00
|
|
|
}
|
|
|
|
|
[clang][analyzer] Using CallDescription in StreamChecker.
Summary:
Recognization of function names is done now with the CallDescription
class instead of using IdentifierInfo. This means function name and
argument count is compared too.
A new check for filtering not global-C-functions was added.
Test was updated.
Reviewers: Szelethus, NoQ, baloghadamsoftware, Charusso
Reviewed By: Szelethus, NoQ, Charusso
Subscribers: rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, donat.nagy, Charusso, dkrupp, Szelethus, gamesh411, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D67706
2019-10-31 19:09:44 +08:00
|
|
|
void f_leak(int c) {
|
2010-07-23 22:14:59 +08:00
|
|
|
FILE *p = fopen("foo.c", "r");
|
[Analyzer][StreamChecker] Check for opened stream before operations.
Summary:
According to documentations, after an `fclose` call any other stream
operations cause undefined behaviour, regardless if the close failed
or not.
This change adds the check for the opened state before all other
(applicable) operations.
Reviewers: Szelethus
Reviewed By: Szelethus
Subscribers: xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75614
2020-03-09 17:15:31 +08:00
|
|
|
if (!p)
|
|
|
|
return;
|
2010-07-23 22:14:59 +08:00
|
|
|
if(c)
|
[Analyzer][StreamChecker] Add note tags for file opening.
Summary:
Bug reports of resource leak are now improved.
If there are multiple resource leak paths for the same stream,
only one wil be reported.
Reviewers: Szelethus, xazax.hun, baloghadamsoftware, NoQ
Reviewed By: Szelethus, NoQ
Subscribers: NoQ, rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, ASDenysPetrov, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81407
2020-06-22 15:04:05 +08:00
|
|
|
return; // expected-warning {{Opened stream never closed. Potential resource leak}}
|
2010-07-23 22:14:59 +08:00
|
|
|
fclose(p);
|
|
|
|
}
|
|
|
|
|
[clang][analyzer] Using CallDescription in StreamChecker.
Summary:
Recognization of function names is done now with the CallDescription
class instead of using IdentifierInfo. This means function name and
argument count is compared too.
A new check for filtering not global-C-functions was added.
Test was updated.
Reviewers: Szelethus, NoQ, baloghadamsoftware, Charusso
Reviewed By: Szelethus, NoQ, Charusso
Subscribers: rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, donat.nagy, Charusso, dkrupp, Szelethus, gamesh411, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D67706
2019-10-31 19:09:44 +08:00
|
|
|
FILE *f_null_checked(void) {
|
2010-07-23 22:14:59 +08:00
|
|
|
FILE *p = fopen("foo.c", "r");
|
|
|
|
if (p)
|
|
|
|
return p; // no-warning
|
|
|
|
else
|
|
|
|
return 0;
|
2010-07-22 22:01:01 +08:00
|
|
|
}
|
2010-08-06 08:04:40 +08:00
|
|
|
|
|
|
|
void pr7831(FILE *fp) {
|
|
|
|
fclose(fp); // no-warning
|
|
|
|
}
|
2010-09-08 04:45:26 +08:00
|
|
|
|
|
|
|
// PR 8081 - null pointer crash when 'whence' is not an integer constant
|
|
|
|
void pr8081(FILE *stream, long offset, int whence) {
|
|
|
|
fseek(stream, offset, whence);
|
|
|
|
}
|
2019-12-05 00:15:03 +08:00
|
|
|
|
|
|
|
void check_freopen_1() {
|
|
|
|
FILE *f1 = freopen("foo.c", "r", (FILE *)0); // expected-warning {{Stream pointer might be NULL}}
|
|
|
|
f1 = freopen(0, "w", (FILE *)0x123456); // Do not report this as error.
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_freopen_2() {
|
|
|
|
FILE *f1 = fopen("foo.c", "r");
|
|
|
|
if (f1) {
|
|
|
|
FILE *f2 = freopen(0, "w", f1);
|
|
|
|
if (f2) {
|
|
|
|
// Check if f1 and f2 point to the same stream.
|
|
|
|
fclose(f1);
|
[Analyzer][StreamChecker] Check for opened stream before operations.
Summary:
According to documentations, after an `fclose` call any other stream
operations cause undefined behaviour, regardless if the close failed
or not.
This change adds the check for the opened state before all other
(applicable) operations.
Reviewers: Szelethus
Reviewed By: Szelethus
Subscribers: xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75614
2020-03-09 17:15:31 +08:00
|
|
|
fclose(f2); // expected-warning {{Stream might be already closed.}}
|
2019-12-05 00:15:03 +08:00
|
|
|
} else {
|
|
|
|
// Reopen failed.
|
[Analyzer][StreamChecker] Check for opened stream before operations.
Summary:
According to documentations, after an `fclose` call any other stream
operations cause undefined behaviour, regardless if the close failed
or not.
This change adds the check for the opened state before all other
(applicable) operations.
Reviewers: Szelethus
Reviewed By: Szelethus
Subscribers: xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75614
2020-03-09 17:15:31 +08:00
|
|
|
// f1 is non-NULL but points to a possibly invalid stream.
|
|
|
|
rewind(f1); // expected-warning {{Stream might be invalid}}
|
|
|
|
// f2 is NULL but the previous error stops the checker.
|
|
|
|
rewind(f2);
|
2019-12-05 00:15:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_freopen_3() {
|
|
|
|
FILE *f1 = fopen("foo.c", "r");
|
|
|
|
if (f1) {
|
|
|
|
// Unchecked result of freopen.
|
[Analyzer][StreamChecker] Check for opened stream before operations.
Summary:
According to documentations, after an `fclose` call any other stream
operations cause undefined behaviour, regardless if the close failed
or not.
This change adds the check for the opened state before all other
(applicable) operations.
Reviewers: Szelethus
Reviewed By: Szelethus
Subscribers: xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75614
2020-03-09 17:15:31 +08:00
|
|
|
// The f1 may be invalid after this call.
|
2019-12-05 00:15:03 +08:00
|
|
|
freopen(0, "w", f1);
|
[Analyzer][StreamChecker] Check for opened stream before operations.
Summary:
According to documentations, after an `fclose` call any other stream
operations cause undefined behaviour, regardless if the close failed
or not.
This change adds the check for the opened state before all other
(applicable) operations.
Reviewers: Szelethus
Reviewed By: Szelethus
Subscribers: xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75614
2020-03-09 17:15:31 +08:00
|
|
|
rewind(f1); // expected-warning {{Stream might be invalid}}
|
2019-12-05 00:15:03 +08:00
|
|
|
fclose(f1);
|
|
|
|
}
|
[Analyzer][StreamChecker] Add check for pointer escape.
Summary:
After an escaped FILE* stream handle it is not possible to make
reliable checks on it because any function call can have effect
on it.
Reviewers: Szelethus, baloghadamsoftware, martong, NoQ
Reviewed By: NoQ
Subscribers: NoQ, rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, ASDenysPetrov, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D80699
2020-06-15 20:59:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern FILE *GlobalF;
|
|
|
|
extern void takeFile(FILE *);
|
|
|
|
|
|
|
|
void check_escape1() {
|
|
|
|
FILE *F = tmpfile();
|
|
|
|
if (!F)
|
|
|
|
return;
|
|
|
|
fwrite("1", 1, 1, F); // may fail
|
|
|
|
GlobalF = F;
|
|
|
|
fwrite("1", 1, 1, F); // no warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_escape2() {
|
|
|
|
FILE *F = tmpfile();
|
|
|
|
if (!F)
|
|
|
|
return;
|
|
|
|
fwrite("1", 1, 1, F); // may fail
|
|
|
|
takeFile(F);
|
|
|
|
fwrite("1", 1, 1, F); // no warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_escape3() {
|
|
|
|
FILE *F = tmpfile();
|
|
|
|
if (!F)
|
|
|
|
return;
|
|
|
|
takeFile(F);
|
|
|
|
F = freopen(0, "w", F);
|
|
|
|
if (!F)
|
|
|
|
return;
|
|
|
|
fwrite("1", 1, 1, F); // may fail
|
|
|
|
fwrite("1", 1, 1, F); // no warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_escape4() {
|
|
|
|
FILE *F = tmpfile();
|
|
|
|
if (!F)
|
|
|
|
return;
|
|
|
|
fwrite("1", 1, 1, F); // may fail
|
|
|
|
|
|
|
|
// no escape at (non-StreamChecker-handled) system call
|
|
|
|
// FIXME: all such calls should be handled by the checker
|
|
|
|
fprintf(F, "0");
|
|
|
|
|
|
|
|
fwrite("1", 1, 1, F); // expected-warning {{might be 'indeterminate'}}
|
|
|
|
fclose(F);
|
|
|
|
}
|
[Analyzer][StreamChecker] Add note tags for file opening.
Summary:
Bug reports of resource leak are now improved.
If there are multiple resource leak paths for the same stream,
only one wil be reported.
Reviewers: Szelethus, xazax.hun, baloghadamsoftware, NoQ
Reviewed By: Szelethus, NoQ
Subscribers: NoQ, rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, ASDenysPetrov, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81407
2020-06-22 15:04:05 +08:00
|
|
|
|
|
|
|
int Test;
|
|
|
|
_Noreturn void handle_error();
|
|
|
|
|
|
|
|
void check_leak_noreturn_1() {
|
|
|
|
FILE *F1 = tmpfile();
|
|
|
|
if (!F1)
|
|
|
|
return;
|
|
|
|
if (Test == 1) {
|
|
|
|
handle_error(); // no warning
|
|
|
|
}
|
|
|
|
rewind(F1);
|
|
|
|
} // expected-warning {{Opened stream never closed. Potential resource leak}}
|
|
|
|
|
|
|
|
// Check that "location uniqueing" works.
|
|
|
|
// This results in reporting only one occurence of resource leak for a stream.
|
|
|
|
void check_leak_noreturn_2() {
|
|
|
|
FILE *F1 = tmpfile();
|
|
|
|
if (!F1)
|
|
|
|
return;
|
|
|
|
if (Test == 1) {
|
2020-07-23 16:53:16 +08:00
|
|
|
return; // no warning
|
[Analyzer][StreamChecker] Add note tags for file opening.
Summary:
Bug reports of resource leak are now improved.
If there are multiple resource leak paths for the same stream,
only one wil be reported.
Reviewers: Szelethus, xazax.hun, baloghadamsoftware, NoQ
Reviewed By: Szelethus, NoQ
Subscribers: NoQ, rnkovacs, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, gamesh411, Charusso, martong, ASDenysPetrov, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81407
2020-06-22 15:04:05 +08:00
|
|
|
}
|
|
|
|
rewind(F1);
|
2020-07-23 16:53:16 +08:00
|
|
|
} // expected-warning {{Opened stream never closed. Potential resource leak}}
|
|
|
|
// FIXME: This warning should be placed at the `return` above.
|
|
|
|
// See https://reviews.llvm.org/D83120 about details.
|