2017-03-04 02:02:02 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.unix.Stream -analyzer-store region -verify %s
|
2010-06-16 13:38:05 +08:00
|
|
|
|
|
|
|
typedef __typeof__(sizeof(int)) size_t;
|
|
|
|
typedef struct _IO_FILE FILE;
|
2010-06-18 10:47:46 +08:00
|
|
|
#define SEEK_SET 0 /* Seek from beginning of file. */
|
|
|
|
#define SEEK_CUR 1 /* Seek from current position. */
|
|
|
|
#define SEEK_END 2 /* Seek from end of file. */
|
|
|
|
extern FILE *fopen(const char *path, const char *mode);
|
2010-07-22 22:01:01 +08:00
|
|
|
extern FILE *tmpfile(void);
|
2010-07-19 09:52:29 +08:00
|
|
|
extern int fclose(FILE *fp);
|
2010-06-18 10:47:46 +08:00
|
|
|
extern size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
|
|
|
|
extern int fseek (FILE *__stream, long int __off, int __whence);
|
|
|
|
extern long int ftell (FILE *__stream);
|
|
|
|
extern void rewind (FILE *__stream);
|
2010-06-16 13:38:05 +08:00
|
|
|
|
|
|
|
void f1(void) {
|
|
|
|
FILE *p = fopen("foo", "r");
|
|
|
|
char buf[1024];
|
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
|
|
|
fread(buf, 1, 1, p); // expected-warning {{Stream pointer might be NULL}}
|
2010-07-23 22:14:59 +08:00
|
|
|
fclose(p);
|
2010-06-16 13:38:05 +08:00
|
|
|
}
|
2010-06-18 10:47:46 +08:00
|
|
|
|
|
|
|
void f2(void) {
|
|
|
|
FILE *p = fopen("foo", "r");
|
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, SEEK_SET); // 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
|
|
|
}
|
|
|
|
|
|
|
|
void f3(void) {
|
|
|
|
FILE *p = fopen("foo", "r");
|
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
|
|
|
ftell(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
|
|
|
}
|
|
|
|
|
|
|
|
void f4(void) {
|
|
|
|
FILE *p = fopen("foo", "r");
|
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
|
|
|
rewind(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
|
|
|
}
|
|
|
|
|
2010-06-24 21:36:41 +08:00
|
|
|
void f5(void) {
|
|
|
|
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
|
|
|
|
|
|
|
void f6(void) {
|
|
|
|
FILE *p = fopen("foo", "r");
|
|
|
|
fclose(p);
|
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
|
|
|
fclose(p); // expected-warning {{Try to close a file Descriptor already closed. Cause undefined behaviour}}
|
2010-07-19 09:52:29 +08:00
|
|
|
}
|
2010-07-22 22:01:01 +08:00
|
|
|
|
|
|
|
void f7(void) {
|
|
|
|
FILE *p = tmpfile();
|
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
|
|
|
ftell(p); // expected-warning {{Stream pointer might be NULL}}
|
2010-07-23 22:14:59 +08:00
|
|
|
fclose(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void f8(int c) {
|
|
|
|
FILE *p = fopen("foo.c", "r");
|
|
|
|
if(c)
|
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
|
|
|
return; // expected-warning {{Opened File never closed. Potential Resource leak}}
|
2010-07-23 22:14:59 +08:00
|
|
|
fclose(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *f9(void) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|