xref: /llvm-project/clang/test/Analysis/stream-stdlibraryfunctionargs.c (revision 09f160c6298255f520b379b88161fbd1c365b308)
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Stream,unix.StdCLibraryFunctions,debug.ExprInspection \
2 // RUN:   -analyzer-config unix.Stream:Pedantic=true \
3 // RUN:   -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true -verify=stream,any %s
4 
5 // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Stream,debug.ExprInspection \
6 // RUN:   -analyzer-config unix.Stream:Pedantic=true \
7 // RUN:   -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true -verify=stream,any %s
8 
9 // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.StdCLibraryFunctions,debug.ExprInspection \
10 // RUN:   -analyzer-config unix.Stream:Pedantic=true \
11 // RUN:   -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true -verify=stdfunc,any %s
12 
13 #include "Inputs/system-header-simulator.h"
14 
15 extern void clang_analyzer_eval(int);
16 
17 void *buf;
18 size_t size;
19 size_t n;
20 
test_fopen(void)21 void test_fopen(void) {
22   FILE *fp = fopen("path", "r");
23   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}} any-warning{{FALSE}}
24   fclose(fp); // \
25   // stream-warning{{Stream pointer might be NULL}} \
26   // stdfunc-warning{{should not be NULL}}
27 }
28 
test_fdopen(int fd)29 void test_fdopen(int fd) {
30   FILE *fp = fdopen(fd, "r");
31   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}} any-warning{{FALSE}}
32   fclose(fp); // \
33   // stream-warning{{Stream pointer might be NULL}} \
34   // stdfunc-warning{{should not be NULL}}
35 }
36 
test_tmpfile(void)37 void test_tmpfile(void) {
38   FILE *fp = tmpfile();
39   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}} any-warning{{FALSE}}
40   fclose(fp); // \
41   // stream-warning{{Stream pointer might be NULL}} \
42   // stdfunc-warning{{should not be NULL}}
43 }
44 
test_fclose(void)45 void test_fclose(void) {
46   FILE *fp = tmpfile();
47   fclose(fp); // \
48   // stream-warning{{Stream pointer might be NULL}} \
49   // stdfunc-warning{{should not be NULL}}
50   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}}
51 }
52 
test_freopen(void)53 void test_freopen(void) {
54   FILE *fp = tmpfile();
55   fp = freopen("file", "w", fp); // \
56   // stream-warning{{Stream pointer might be NULL}} \
57   // stdfunc-warning{{should not be NULL}}
58   fclose(fp); // \
59   // stream-warning{{Stream pointer might be NULL}} \
60   // stdfunc-warning{{should not be NULL}}
61   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}}
62 }
63 
test_fread(void)64 void test_fread(void) {
65   FILE *fp = tmpfile();
66   size_t ret = fread(buf, size, n, fp); // \
67   // stream-warning{{Stream pointer might be NULL}} \
68   // stdfunc-warning{{The 4th argument to 'fread' is NULL but should not be NULL}}
69   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}}
70   clang_analyzer_eval(ret <= n); // any-warning{{TRUE}}
71   clang_analyzer_eval(ret == n); // any-warning{{TRUE}} any-warning{{FALSE}}
72 
73   fclose(fp);
74 }
75 
test_fwrite(void)76 void test_fwrite(void) {
77   FILE *fp = tmpfile();
78   size_t ret = fwrite(buf, size, n, fp); // \
79   // stream-warning{{Stream pointer might be NULL}} \
80   // stdfunc-warning{{The 4th argument to 'fwrite' is NULL but should not be NULL}}
81   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}}
82   clang_analyzer_eval(ret <= n); // any-warning{{TRUE}}
83   clang_analyzer_eval(ret == n); // any-warning{{TRUE}} any-warning{{FALSE}}
84 
85   fclose(fp);
86 }
87 
test_fseek(void)88 void test_fseek(void) {
89   FILE *fp = tmpfile();
90   fseek(fp, 0, 0); // \
91   // stream-warning{{Stream pointer might be NULL}} \
92   // stdfunc-warning{{should not be NULL}}
93   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}}
94   fclose(fp);
95 }
96 
test_ftell(void)97 void test_ftell(void) {
98   FILE *fp = tmpfile();
99   ftell(fp); // \
100   // stream-warning{{Stream pointer might be NULL}} \
101   // stdfunc-warning{{should not be NULL}}
102   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}}
103   fclose(fp);
104 }
105 
test_rewind(void)106 void test_rewind(void) {
107   FILE *fp = tmpfile();
108   rewind(fp); // \
109   // stream-warning{{Stream pointer might be NULL}} \
110   // stdfunc-warning{{should not be NULL}}
111   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}}
112   fclose(fp);
113 }
114 
test_fgetpos(void)115 void test_fgetpos(void) {
116   FILE *fp = tmpfile();
117   fpos_t pos;
118   fgetpos(fp, &pos); // \
119   // stream-warning{{Stream pointer might be NULL}} \
120   // stdfunc-warning{{should not be NULL}}
121   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}}
122   fclose(fp);
123 }
124 
test_fsetpos(void)125 void test_fsetpos(void) {
126   FILE *fp = tmpfile();
127   fpos_t pos;
128   fsetpos(fp, &pos); // \
129   // stream-warning{{Stream pointer might be NULL}} \
130   // stdfunc-warning{{should not be NULL}}
131   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}}
132   fclose(fp);
133 }
134 
test_clearerr(void)135 void test_clearerr(void) {
136   FILE *fp = tmpfile();
137   clearerr(fp); // \
138   // stream-warning{{Stream pointer might be NULL}} \
139   // stdfunc-warning{{should not be NULL}}
140   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}}
141   fclose(fp);
142 }
143 
test_feof(void)144 void test_feof(void) {
145   FILE *fp = tmpfile();
146   feof(fp); // \
147   // stream-warning{{Stream pointer might be NULL}} \
148   // stdfunc-warning{{should not be NULL}}
149   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}}
150   fclose(fp);
151 }
152 
test_ferror(void)153 void test_ferror(void) {
154   FILE *fp = tmpfile();
155   ferror(fp); // \
156   // stream-warning{{Stream pointer might be NULL}} \
157   // stdfunc-warning{{should not be NULL}}
158   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}}
159   fclose(fp);
160 }
161 
test_fileno(void)162 void test_fileno(void) {
163   FILE *fp = tmpfile();
164   fileno(fp); // \
165   // stream-warning{{Stream pointer might be NULL}} \
166   // stdfunc-warning{{should not be NULL}}
167   clang_analyzer_eval(fp != NULL); // any-warning{{TRUE}}
168   fclose(fp);
169 }
170