1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.unix.SimpleStream -verify %s
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuc #include "Inputs/system-header-simulator-for-simple-stream.h"
4*f4a2713aSLionel Sambuc
checkDoubleFClose(int * Data)5*f4a2713aSLionel Sambuc void checkDoubleFClose(int *Data) {
6*f4a2713aSLionel Sambuc FILE *F = fopen("myfile.txt", "w");
7*f4a2713aSLionel Sambuc if (F != 0) {
8*f4a2713aSLionel Sambuc fputs ("fopen example", F);
9*f4a2713aSLionel Sambuc if (!Data)
10*f4a2713aSLionel Sambuc fclose(F);
11*f4a2713aSLionel Sambuc else
12*f4a2713aSLionel Sambuc fputc(*Data, F);
13*f4a2713aSLionel Sambuc fclose(F); // expected-warning {{Closing a previously closed file stream}}
14*f4a2713aSLionel Sambuc }
15*f4a2713aSLionel Sambuc }
16*f4a2713aSLionel Sambuc
checkLeak(int * Data)17*f4a2713aSLionel Sambuc int checkLeak(int *Data) {
18*f4a2713aSLionel Sambuc FILE *F = fopen("myfile.txt", "w");
19*f4a2713aSLionel Sambuc if (F != 0) {
20*f4a2713aSLionel Sambuc fputs ("fopen example", F);
21*f4a2713aSLionel Sambuc }
22*f4a2713aSLionel Sambuc
23*f4a2713aSLionel Sambuc if (Data) // expected-warning {{Opened file is never closed; potential resource leak}}
24*f4a2713aSLionel Sambuc return *Data;
25*f4a2713aSLionel Sambuc else
26*f4a2713aSLionel Sambuc return 0;
27*f4a2713aSLionel Sambuc }
28*f4a2713aSLionel Sambuc
checkLeakFollowedByAssert(int * Data)29*f4a2713aSLionel Sambuc void checkLeakFollowedByAssert(int *Data) {
30*f4a2713aSLionel Sambuc FILE *F = fopen("myfile.txt", "w");
31*f4a2713aSLionel Sambuc if (F != 0) {
32*f4a2713aSLionel Sambuc fputs ("fopen example", F);
33*f4a2713aSLionel Sambuc if (!Data)
34*f4a2713aSLionel Sambuc exit(0);
35*f4a2713aSLionel Sambuc fclose(F);
36*f4a2713aSLionel Sambuc }
37*f4a2713aSLionel Sambuc }
38*f4a2713aSLionel Sambuc
CloseOnlyOnValidFileHandle()39*f4a2713aSLionel Sambuc void CloseOnlyOnValidFileHandle() {
40*f4a2713aSLionel Sambuc FILE *F = fopen("myfile.txt", "w");
41*f4a2713aSLionel Sambuc if (F)
42*f4a2713aSLionel Sambuc fclose(F);
43*f4a2713aSLionel Sambuc int x = 0; // no warning
44*f4a2713aSLionel Sambuc }
45*f4a2713aSLionel Sambuc
leakOnEnfOfPath1(int * Data)46*f4a2713aSLionel Sambuc void leakOnEnfOfPath1(int *Data) {
47*f4a2713aSLionel Sambuc FILE *F = fopen("myfile.txt", "w");
48*f4a2713aSLionel Sambuc } // expected-warning {{Opened file is never closed; potential resource leak}}
49*f4a2713aSLionel Sambuc
leakOnEnfOfPath2(int * Data)50*f4a2713aSLionel Sambuc void leakOnEnfOfPath2(int *Data) {
51*f4a2713aSLionel Sambuc FILE *F = fopen("myfile.txt", "w");
52*f4a2713aSLionel Sambuc return; // expected-warning {{Opened file is never closed; potential resource leak}}
53*f4a2713aSLionel Sambuc }
54*f4a2713aSLionel Sambuc
leakOnEnfOfPath3(int * Data)55*f4a2713aSLionel Sambuc FILE *leakOnEnfOfPath3(int *Data) {
56*f4a2713aSLionel Sambuc FILE *F = fopen("myfile.txt", "w");
57*f4a2713aSLionel Sambuc return F;
58*f4a2713aSLionel Sambuc }
59*f4a2713aSLionel Sambuc
60*f4a2713aSLionel Sambuc void myfclose(FILE *F);
SymbolEscapedThroughFunctionCall()61*f4a2713aSLionel Sambuc void SymbolEscapedThroughFunctionCall() {
62*f4a2713aSLionel Sambuc FILE *F = fopen("myfile.txt", "w");
63*f4a2713aSLionel Sambuc myfclose(F);
64*f4a2713aSLionel Sambuc return; // no warning
65*f4a2713aSLionel Sambuc }
66*f4a2713aSLionel Sambuc
67*f4a2713aSLionel Sambuc FILE *GlobalF;
SymbolEscapedThroughAssignmentToGloabl()68*f4a2713aSLionel Sambuc void SymbolEscapedThroughAssignmentToGloabl() {
69*f4a2713aSLionel Sambuc FILE *F = fopen("myfile.txt", "w");
70*f4a2713aSLionel Sambuc GlobalF = F;
71*f4a2713aSLionel Sambuc return; // no warning
72*f4a2713aSLionel Sambuc }
73*f4a2713aSLionel Sambuc
SymbolDoesNotEscapeThoughStringAPIs(char * Data)74*f4a2713aSLionel Sambuc void SymbolDoesNotEscapeThoughStringAPIs(char *Data) {
75*f4a2713aSLionel Sambuc FILE *F = fopen("myfile.txt", "w");
76*f4a2713aSLionel Sambuc fputc(*Data, F);
77*f4a2713aSLionel Sambuc return; // expected-warning {{Opened file is never closed; potential resource leak}}
78*f4a2713aSLionel Sambuc }
79*f4a2713aSLionel Sambuc
80*f4a2713aSLionel Sambuc void passConstPointer(const FILE * F);
testPassConstPointer()81*f4a2713aSLionel Sambuc void testPassConstPointer() {
82*f4a2713aSLionel Sambuc FILE *F = fopen("myfile.txt", "w");
83*f4a2713aSLionel Sambuc passConstPointer(F);
84*f4a2713aSLionel Sambuc return; // expected-warning {{Opened file is never closed; potential resource leak}}
85*f4a2713aSLionel Sambuc }
86*f4a2713aSLionel Sambuc
testPassToSystemHeaderFunctionIndirectly()87*f4a2713aSLionel Sambuc void testPassToSystemHeaderFunctionIndirectly() {
88*f4a2713aSLionel Sambuc FileStruct fs;
89*f4a2713aSLionel Sambuc fs.p = fopen("myfile.txt", "w");
90*f4a2713aSLionel Sambuc fakeSystemHeaderCall(&fs); // invalidates fs, making fs.p unreachable
91*f4a2713aSLionel Sambuc } // no-warning
92