aboutsummaryrefslogtreecommitdiff
path: root/clang/test/Analysis/std-c-library-functions.c
blob: b5f663493a6765a8ca52ef422a6e6b63de194ed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// RUN: %clang_analyze_cc1 %s \
// RUN:   -analyzer-checker=core \
// RUN:   -analyzer-checker=unix.StdCLibraryFunctions \
// RUN:   -analyzer-checker=debug.ExprInspection \
// RUN:   -analyzer-config eagerly-assume=false \
// RUN:   -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=false \
// RUN:   -triple i686-unknown-linux \
// RUN:   -verify

// RUN: %clang_analyze_cc1 %s \
// RUN:   -analyzer-checker=core \
// RUN:   -analyzer-checker=unix.StdCLibraryFunctions \
// RUN:   -analyzer-checker=debug.ExprInspection \
// RUN:   -analyzer-config eagerly-assume=false \
// RUN:   -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=false \
// RUN:   -triple x86_64-unknown-linux \
// RUN:   -verify

// RUN: %clang_analyze_cc1 %s \
// RUN:   -analyzer-checker=core \
// RUN:   -analyzer-checker=unix.StdCLibraryFunctions \
// RUN:   -analyzer-checker=debug.ExprInspection \
// RUN:   -analyzer-config eagerly-assume=false \
// RUN:   -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=false \
// RUN:   -triple armv7-a15-linux \
// RUN:   -verify

// RUN: %clang_analyze_cc1 %s \
// RUN:   -analyzer-checker=core \
// RUN:   -analyzer-checker=unix.StdCLibraryFunctions \
// RUN:   -analyzer-checker=debug.ExprInspection \
// RUN:   -analyzer-config eagerly-assume=false \
// RUN:   -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=false \
// RUN:   -triple thumbv7-a15-linux \
// RUN:   -verify

// RUN: %clang_analyze_cc1 %s \
// RUN:   -analyzer-checker=core \
// RUN:   -analyzer-checker=unix.StdCLibraryFunctions \
// RUN:   -analyzer-config unix.StdCLibraryFunctions:DisplayLoadedSummaries=true \
// RUN:   -analyzer-checker=debug.ExprInspection \
// RUN:   -analyzer-config eagerly-assume=false \
// RUN:   -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=false \
// RUN:   -triple i686-unknown-linux 2>&1 | FileCheck %s

//      CHECK: Loaded summary for: int isalnum(int)
// CHECK-NEXT: Loaded summary for: int isalpha(int)
// CHECK-NEXT: Loaded summary for: int isascii(int)
// CHECK-NEXT: Loaded summary for: int isblank(int)
// CHECK-NEXT: Loaded summary for: int isdigit(int)
// CHECK-NEXT: Loaded summary for: int isgraph(int)
// CHECK-NEXT: Loaded summary for: int islower(int)
// CHECK-NEXT: Loaded summary for: int isprint(int)
// CHECK-NEXT: Loaded summary for: int ispunct(int)
// CHECK-NEXT: Loaded summary for: int isspace(int)
// CHECK-NEXT: Loaded summary for: int isupper(int)
// CHECK-NEXT: Loaded summary for: int isxdigit(int)
// CHECK-NEXT: Loaded summary for: int toupper(int)
// CHECK-NEXT: Loaded summary for: int tolower(int)
// CHECK-NEXT: Loaded summary for: int toascii(int)
// CHECK-NEXT: Loaded summary for: int getchar(void)
// CHECK-NEXT: Loaded summary for: __size_t fread(void *restrict, size_t, size_t, FILE *restrict)
// CHECK-NEXT: Loaded summary for: __size_t fwrite(const void *restrict, size_t, size_t, FILE *restrict)
// CHECK-NEXT: Loaded summary for: ssize_t read(int, void *, size_t)
// CHECK-NEXT: Loaded summary for: ssize_t write(int, const void *, size_t)
// CHECK-NEXT: Loaded summary for: ssize_t getline(char **restrict, size_t *restrict, FILE *restrict)
// CHECK-NEXT: Loaded summary for: ssize_t getdelim(char **restrict, size_t *restrict, int, FILE *restrict)
// CHECK-NEXT: Loaded summary for: char *getenv(const char *)
// CHECK-NEXT: Loaded summary for: int getc(FILE *)
// CHECK-NEXT: Loaded summary for: int fgetc(FILE *)

#include "Inputs/std-c-library-functions.h"

void clang_analyzer_eval(int);

int glob;

void test_getc(FILE *fp) {
  int x;
  while ((x = getc(fp)) != EOF) {
    clang_analyzer_eval(x > 255); // expected-warning{{FALSE}}
    clang_analyzer_eval(x >= 0); // expected-warning{{TRUE}}
  }
}

void test_fgets(FILE *fp) {
  clang_analyzer_eval(fgetc(fp) < 256); // expected-warning{{TRUE}}
  clang_analyzer_eval(fgetc(fp) >= 0); // expected-warning{{UNKNOWN}}
}

void test_read_write(int fd, char *buf) {
  glob = 1;
  ssize_t x = write(fd, buf, 10);
  clang_analyzer_eval(glob); // expected-warning{{UNKNOWN}}
  if (x >= 0) {
    clang_analyzer_eval(x <= 10); // expected-warning{{TRUE}}
    ssize_t y = read(fd, &glob, sizeof(glob));
    if (y >= 0) {
      clang_analyzer_eval(y <= sizeof(glob)); // expected-warning{{TRUE}}
    } else {
      // -1 overflows on promotion!
      clang_analyzer_eval(y <= sizeof(glob)); // expected-warning{{FALSE}}
    }
  } else {
    clang_analyzer_eval(x == -1); // expected-warning{{TRUE}}
  }
}

void test_fread_fwrite(FILE *fp, int *buf) {

  size_t x = fwrite(buf, sizeof(int), 10, fp);
  clang_analyzer_eval(x <= 10); // expected-warning{{TRUE}}

  size_t y = fread(buf, sizeof(int), 10, fp);
  clang_analyzer_eval(y <= 10); // expected-warning{{TRUE}}

  size_t z = fwrite(buf, sizeof(int), y, fp);
  clang_analyzer_eval(z <= y); // expected-warning{{TRUE}}
}

void test_fread_uninitialized(void) {
  void *ptr;
  size_t sz;
  size_t nmem;
  FILE *fp;
  (void)fread(ptr, sz, nmem, fp); // expected-warning {{1st function call argument is an uninitialized value}}
}

void test_getline(FILE *fp) {
  char *line = 0;
  size_t n = 0;
  ssize_t len;
  while ((len = getline(&line, &n, fp)) != -1) {
    clang_analyzer_eval(len == 0); // expected-warning{{FALSE}}
  }
}

void test_isascii(int x) {
  clang_analyzer_eval(isascii(123)); // expected-warning{{TRUE}}
  clang_analyzer_eval(isascii(-1)); // expected-warning{{FALSE}}
  if (isascii(x)) {
    clang_analyzer_eval(x < 128); // expected-warning{{TRUE}}
    clang_analyzer_eval(x >= 0); // expected-warning{{TRUE}}
  } else {
    if (x > 42)
      clang_analyzer_eval(x >= 128); // expected-warning{{TRUE}}
    else
      clang_analyzer_eval(x < 0); // expected-warning{{TRUE}}
  }
  glob = 1;
  isascii('a');
  clang_analyzer_eval(glob); // expected-warning{{TRUE}}
}

void test_islower(int x) {
  clang_analyzer_eval(islower('x')); // expected-warning{{TRUE}}
  clang_analyzer_eval(islower('X')); // expected-warning{{FALSE}}
  if (islower(x))
    clang_analyzer_eval(x < 'a'); // expected-warning{{FALSE}}
}

void test_getchar(void) {
  int x = getchar();
  if (x == EOF)
    return;
  clang_analyzer_eval(x < 0); // expected-warning{{FALSE}}
  clang_analyzer_eval(x < 256); // expected-warning{{TRUE}}
}

void test_isalpha(void) {
  clang_analyzer_eval(isalpha(']')); // expected-warning{{FALSE}}
  clang_analyzer_eval(isalpha('Q')); // expected-warning{{TRUE}}
  clang_analyzer_eval(isalpha(128)); // expected-warning{{UNKNOWN}}
}

void test_alnum(void) {
  clang_analyzer_eval(isalnum('1')); // expected-warning{{TRUE}}
  clang_analyzer_eval(isalnum(')')); // expected-warning{{FALSE}}
}

void test_isblank(void) {
  clang_analyzer_eval(isblank('\t')); // expected-warning{{TRUE}}
  clang_analyzer_eval(isblank(' ')); // expected-warning{{TRUE}}
  clang_analyzer_eval(isblank('\n')); // expected-warning{{FALSE}}
}

void test_ispunct(int x) {
  clang_analyzer_eval(ispunct(' ')); // expected-warning{{FALSE}}
  clang_analyzer_eval(ispunct(-1)); // expected-warning{{FALSE}}
  clang_analyzer_eval(ispunct('#')); // expected-warning{{TRUE}}
  clang_analyzer_eval(ispunct('_')); // expected-warning{{TRUE}}
  if (ispunct(x))
    clang_analyzer_eval(x < 127); // expected-warning{{TRUE}}
}

void test_isupper(int x) {
  if (isupper(x))
    clang_analyzer_eval(x < 'A'); // expected-warning{{FALSE}}
}

void test_isgraph_isprint(int x) {
  char y = x;
  if (isgraph(y))
    clang_analyzer_eval(isprint(x)); // expected-warning{{TRUE}}
}

void test_mixed_branches(int x) {
  if (isdigit(x)) {
    clang_analyzer_eval(isgraph(x)); // expected-warning{{TRUE}}
    clang_analyzer_eval(isblank(x)); // expected-warning{{FALSE}}
  } else if (isascii(x)) {
    // isalnum() bifurcates here.
    clang_analyzer_eval(isalnum(x)); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
    clang_analyzer_eval(isprint(x)); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
  }
}

void test_isspace(int x) {
  if (!isascii(x))
    return;
  char y = x;
  if (y == ' ')
    clang_analyzer_eval(isspace(x)); // expected-warning{{TRUE}}
}

void test_isxdigit(int x) {
  if (isxdigit(x) && isupper(x)) {
    clang_analyzer_eval(x >= 'A'); // expected-warning{{TRUE}}
    clang_analyzer_eval(x <= 'F'); // expected-warning{{TRUE}}
  }
}

void test_call_by_pointer(void) {
  typedef int (*func)(int);
  func f = isascii;
  clang_analyzer_eval(f('A')); // expected-warning{{TRUE}}
  f = ispunct;
  clang_analyzer_eval(f('A')); // expected-warning{{FALSE}}
}

void test_getenv(void) {
  // getenv() bifurcates here.
  clang_analyzer_eval(getenv("FOO") == 0);
  // expected-warning@-1 {{TRUE}}
  // expected-warning@-2 {{FALSE}}
}