1 // RUN: %clang_analyze_cc1 %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=apiModeling.StdCLibraryFunctions \
4 // RUN: -analyzer-checker=debug.ExprInspection \
5 // RUN: -analyzer-config eagerly-assume=false \
6 // RUN: -triple i686-unknown-linux \
9 // RUN: %clang_analyze_cc1 %s \
10 // RUN: -analyzer-checker=core \
11 // RUN: -analyzer-checker=apiModeling.StdCLibraryFunctions \
12 // RUN: -analyzer-checker=debug.ExprInspection \
13 // RUN: -analyzer-config eagerly-assume=false \
14 // RUN: -triple x86_64-unknown-linux \
17 // RUN: %clang_analyze_cc1 %s \
18 // RUN: -analyzer-checker=core \
19 // RUN: -analyzer-checker=apiModeling.StdCLibraryFunctions \
20 // RUN: -analyzer-checker=debug.ExprInspection \
21 // RUN: -analyzer-config eagerly-assume=false \
22 // RUN: -triple armv7-a15-linux \
25 // RUN: %clang_analyze_cc1 %s \
26 // RUN: -analyzer-checker=core \
27 // RUN: -analyzer-checker=apiModeling.StdCLibraryFunctions \
28 // RUN: -analyzer-checker=debug.ExprInspection \
29 // RUN: -analyzer-config eagerly-assume=false \
30 // RUN: -triple thumbv7-a15-linux \
33 // RUN: %clang_analyze_cc1 %s \
34 // RUN: -analyzer-checker=core \
35 // RUN: -analyzer-checker=apiModeling.StdCLibraryFunctions \
36 // RUN: -analyzer-config apiModeling.StdCLibraryFunctions:DisplayLoadedSummaries=true \
37 // RUN: -analyzer-checker=debug.ExprInspection \
38 // RUN: -analyzer-config eagerly-assume=false \
39 // RUN: -triple i686-unknown-linux 2>&1 | FileCheck %s
41 // CHECK: Loaded summary for: int isalnum(int)
42 // CHECK-NEXT: Loaded summary for: int isalpha(int)
43 // CHECK-NEXT: Loaded summary for: int isascii(int)
44 // CHECK-NEXT: Loaded summary for: int isblank(int)
45 // CHECK-NEXT: Loaded summary for: int isdigit(int)
46 // CHECK-NEXT: Loaded summary for: int isgraph(int)
47 // CHECK-NEXT: Loaded summary for: int islower(int)
48 // CHECK-NEXT: Loaded summary for: int isprint(int)
49 // CHECK-NEXT: Loaded summary for: int ispunct(int)
50 // CHECK-NEXT: Loaded summary for: int isspace(int)
51 // CHECK-NEXT: Loaded summary for: int isupper(int)
52 // CHECK-NEXT: Loaded summary for: int isxdigit(int)
53 // CHECK-NEXT: Loaded summary for: int getc(FILE *)
54 // CHECK-NEXT: Loaded summary for: int fgetc(FILE *)
55 // CHECK-NEXT: Loaded summary for: int getchar(void)
56 // CHECK-NEXT: Loaded summary for: unsigned int fread(void *restrict, size_t, size_t, FILE *restrict)
57 // CHECK-NEXT: Loaded summary for: unsigned int fwrite(const void *restrict, size_t, size_t, FILE *restrict)
58 // CHECK-NEXT: Loaded summary for: ssize_t read(int, void *, size_t)
59 // CHECK-NEXT: Loaded summary for: ssize_t write(int, const void *, size_t)
60 // CHECK-NEXT: Loaded summary for: ssize_t getline(char **restrict, size_t *restrict, FILE *restrict)
61 // CHECK-NEXT: Loaded summary for: ssize_t getdelim(char **restrict, size_t *restrict, int, FILE *restrict)
64 void clang_analyzer_eval(int);
68 typedef struct FILE FILE;
72 void test_getc(FILE *fp
) {
74 while ((x
= getc(fp
)) != EOF
) {
75 clang_analyzer_eval(x
> 255); // expected-warning{{FALSE}}
76 clang_analyzer_eval(x
>= 0); // expected-warning{{TRUE}}
81 void test_fgets(FILE *fp
) {
82 clang_analyzer_eval(fgetc(fp
) < 256); // expected-warning{{TRUE}}
83 clang_analyzer_eval(fgetc(fp
) >= 0); // expected-warning{{UNKNOWN}}
87 typedef typeof(sizeof(int)) size_t;
88 typedef signed long ssize_t
;
89 ssize_t
read(int, void *, size_t);
90 ssize_t
write(int, const void *, size_t);
91 void test_read_write(int fd
, char *buf
) {
93 ssize_t x
= write(fd
, buf
, 10);
94 clang_analyzer_eval(glob
); // expected-warning{{UNKNOWN}}
96 clang_analyzer_eval(x
<= 10); // expected-warning{{TRUE}}
97 ssize_t y
= read(fd
, &glob
, sizeof(glob
));
99 clang_analyzer_eval(y
<= sizeof(glob
)); // expected-warning{{TRUE}}
101 // -1 overflows on promotion!
102 clang_analyzer_eval(y
<= sizeof(glob
)); // expected-warning{{FALSE}}
105 clang_analyzer_eval(x
== -1); // expected-warning{{TRUE}}
109 size_t fread(void *restrict
, size_t, size_t, FILE *restrict
);
110 size_t fwrite(const void *restrict
, size_t, size_t, FILE *restrict
);
111 void test_fread_fwrite(FILE *fp
, int *buf
) {
113 size_t x
= fwrite(buf
, sizeof(int), 10, fp
);
114 clang_analyzer_eval(x
<= 10); // expected-warning{{TRUE}}
116 size_t y
= fread(buf
, sizeof(int), 10, fp
);
117 clang_analyzer_eval(y
<= 10); // expected-warning{{TRUE}}
119 size_t z
= fwrite(buf
, sizeof(int), y
, fp
);
120 clang_analyzer_eval(z
<= y
); // expected-warning{{TRUE}}
123 void test_fread_uninitialized(void) {
128 (void)fread(ptr
, sz
, nmem
, fp
); // expected-warning {{1st function call argument is an uninitialized value}}
131 ssize_t
getline(char **restrict
, size_t *restrict
, FILE *restrict
);
132 ssize_t
getdelim(char **restrict
, size_t *restrict
, int, FILE *restrict
);
133 void test_getline(FILE *fp
) {
137 while ((len
= getline(&line
, &n
, fp
)) != -1) {
138 clang_analyzer_eval(len
== 0); // expected-warning{{FALSE}}
143 void test_isascii(int x
) {
144 clang_analyzer_eval(isascii(123)); // expected-warning{{TRUE}}
145 clang_analyzer_eval(isascii(-1)); // expected-warning{{FALSE}}
147 clang_analyzer_eval(x
< 128); // expected-warning{{TRUE}}
148 clang_analyzer_eval(x
>= 0); // expected-warning{{TRUE}}
151 clang_analyzer_eval(x
>= 128); // expected-warning{{TRUE}}
153 clang_analyzer_eval(x
< 0); // expected-warning{{TRUE}}
157 clang_analyzer_eval(glob
); // expected-warning{{TRUE}}
161 void test_islower(int x
) {
162 clang_analyzer_eval(islower('x')); // expected-warning{{TRUE}}
163 clang_analyzer_eval(islower('X')); // expected-warning{{FALSE}}
165 clang_analyzer_eval(x
< 'a'); // expected-warning{{FALSE}}
169 void test_getchar(void) {
173 clang_analyzer_eval(x
< 0); // expected-warning{{FALSE}}
174 clang_analyzer_eval(x
< 256); // expected-warning{{TRUE}}
178 void test_isalpha(void) {
179 clang_analyzer_eval(isalpha(']')); // expected-warning{{FALSE}}
180 clang_analyzer_eval(isalpha('Q')); // expected-warning{{TRUE}}
181 clang_analyzer_eval(isalpha(128)); // expected-warning{{UNKNOWN}}
185 void test_alnum(void) {
186 clang_analyzer_eval(isalnum('1')); // expected-warning{{TRUE}}
187 clang_analyzer_eval(isalnum(')')); // expected-warning{{FALSE}}
191 void test_isblank(void) {
192 clang_analyzer_eval(isblank('\t')); // expected-warning{{TRUE}}
193 clang_analyzer_eval(isblank(' ')); // expected-warning{{TRUE}}
194 clang_analyzer_eval(isblank('\n')); // expected-warning{{FALSE}}
198 void test_ispunct(int x
) {
199 clang_analyzer_eval(ispunct(' ')); // expected-warning{{FALSE}}
200 clang_analyzer_eval(ispunct(-1)); // expected-warning{{FALSE}}
201 clang_analyzer_eval(ispunct('#')); // expected-warning{{TRUE}}
202 clang_analyzer_eval(ispunct('_')); // expected-warning{{TRUE}}
204 clang_analyzer_eval(x
< 127); // expected-warning{{TRUE}}
208 void test_isupper(int x
) {
210 clang_analyzer_eval(x
< 'A'); // expected-warning{{FALSE}}
215 void test_isgraph_isprint(int x
) {
218 clang_analyzer_eval(isprint(x
)); // expected-warning{{TRUE}}
222 void test_mixed_branches(int x
) {
224 clang_analyzer_eval(isgraph(x
)); // expected-warning{{TRUE}}
225 clang_analyzer_eval(isblank(x
)); // expected-warning{{FALSE}}
226 } else if (isascii(x
)) {
227 // isalnum() bifurcates here.
228 clang_analyzer_eval(isalnum(x
)); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
229 clang_analyzer_eval(isprint(x
)); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
234 void test_isspace(int x
) {
239 clang_analyzer_eval(isspace(x
)); // expected-warning{{TRUE}}
243 void test_isxdigit(int x
) {
244 if (isxdigit(x
) && isupper(x
)) {
245 clang_analyzer_eval(x
>= 'A'); // expected-warning{{TRUE}}
246 clang_analyzer_eval(x
<= 'F'); // expected-warning{{TRUE}}
250 void test_call_by_pointer(void) {
251 typedef int (*func
)(int);
253 clang_analyzer_eval(f('A')); // expected-warning{{TRUE}}
255 clang_analyzer_eval(f('A')); // expected-warning{{FALSE}}
258 char *getenv(const char *name
);
259 void test_getenv(void) {
260 // getenv() bifurcates here.
261 clang_analyzer_eval(getenv("FOO") == 0);
262 // expected-warning@-1 {{TRUE}}
263 // expected-warning@-2 {{FALSE}}