1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.unix.SimpleStream -verify %s
3 #include "Inputs/system-header-simulator-for-simple-stream.h"
5 void checkDoubleFClose(int *Data
) {
6 FILE *F
= fopen("myfile.txt", "w");
8 fputs ("fopen example", F
);
13 fclose(F
); // expected-warning {{Closing a previously closed file stream}}
17 int checkLeak(int *Data
) {
18 FILE *F
= fopen("myfile.txt", "w");
20 fputs ("fopen example", F
);
23 if (Data
) // expected-warning {{Opened file is never closed; potential resource leak}}
29 void checkLeakFollowedByAssert(int *Data
) {
30 FILE *F
= fopen("myfile.txt", "w");
32 fputs ("fopen example", F
);
39 void CloseOnlyOnValidFileHandle(void) {
40 FILE *F
= fopen("myfile.txt", "w");
43 int x
= 0; // no warning
46 void leakOnEnfOfPath1(int *Data
) {
47 FILE *F
= fopen("myfile.txt", "w");
48 } // expected-warning {{Opened file is never closed; potential resource leak}}
50 void leakOnEnfOfPath2(int *Data
) {
51 FILE *F
= fopen("myfile.txt", "w");
52 return; // expected-warning {{Opened file is never closed; potential resource leak}}
55 FILE *leakOnEnfOfPath3(int *Data
) {
56 FILE *F
= fopen("myfile.txt", "w");
60 void myfclose(FILE *F
);
61 void SymbolEscapedThroughFunctionCall(void) {
62 FILE *F
= fopen("myfile.txt", "w");
68 void SymbolEscapedThroughAssignmentToGlobal(void) {
69 FILE *F
= fopen("myfile.txt", "w");
74 void SymbolDoesNotEscapeThoughStringAPIs(char *Data
) {
75 FILE *F
= fopen("myfile.txt", "w");
77 return; // expected-warning {{Opened file is never closed; potential resource leak}}
80 void passConstPointer(const FILE * F
);
81 void testPassConstPointer(void) {
82 FILE *F
= fopen("myfile.txt", "w");
84 return; // expected-warning {{Opened file is never closed; potential resource leak}}
87 void testPassToSystemHeaderFunctionIndirectly(void) {
89 fs
.p
= fopen("myfile.txt", "w");
90 fakeSystemHeaderCall(&fs
); // invalidates fs, making fs.p unreachable
93 void testOverwrite(void) {
94 FILE *fp
= fopen("myfile.txt", "w");
96 } // expected-warning {{Opened file is never closed; potential resource leak}}