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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
// RUN: %clang_analyze_cc1 -triple=x86_64-pc-linux-gnu -analyzer-checker=core,unix.Stream,debug.ExprInspection \
// RUN: -analyzer-config unix.Stream:Pedantic=true -verify %s
// RUN: %clang_analyze_cc1 -triple=armv8-none-linux-eabi -analyzer-checker=core,unix.Stream,debug.ExprInspection \
// RUN: -analyzer-config unix.Stream:Pedantic=true -verify %s
// RUN: %clang_analyze_cc1 -triple=aarch64-linux-gnu -analyzer-checker=core,unix.Stream,debug.ExprInspection \
// RUN: -analyzer-config unix.Stream:Pedantic=true -verify %s
// RUN: %clang_analyze_cc1 -triple=hexagon -analyzer-checker=core,unix.Stream,debug.ExprInspection \
// RUN: -analyzer-config unix.Stream:Pedantic=true -verify %s
#include "Inputs/system-header-simulator.h"
#include "Inputs/system-header-simulator-for-malloc.h"
#include "Inputs/system-header-simulator-for-valist.h"
void clang_analyzer_eval(int);
void check_fread(void) {
FILE *fp = tmpfile();
fread(0, 0, 0, fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_fwrite(void) {
FILE *fp = tmpfile();
fwrite(0, 0, 0, fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_fgetc(void) {
FILE *fp = tmpfile();
fgetc(fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_fgets(void) {
FILE *fp = tmpfile();
char buf[256];
fgets(buf, sizeof(buf), fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_fputc(void) {
FILE *fp = tmpfile();
fputc('A', fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_fputs(void) {
FILE *fp = tmpfile();
fputs("ABC", fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_fprintf(void) {
FILE *fp = tmpfile();
fprintf(fp, "ABC"); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_fscanf(void) {
FILE *fp = tmpfile();
fscanf(fp, "ABC"); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_ungetc(void) {
FILE *fp = tmpfile();
ungetc('A', fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_fseek(void) {
FILE *fp = tmpfile();
fseek(fp, 0, 0); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_fseeko(void) {
FILE *fp = tmpfile();
fseeko(fp, 0, 0); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_ftell(void) {
FILE *fp = tmpfile();
ftell(fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_ftello(void) {
FILE *fp = tmpfile();
ftello(fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_rewind(void) {
FILE *fp = tmpfile();
rewind(fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_fgetpos(void) {
FILE *fp = tmpfile();
fpos_t pos;
fgetpos(fp, &pos); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_fsetpos(void) {
FILE *fp = tmpfile();
fpos_t pos;
fsetpos(fp, &pos); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_clearerr(void) {
FILE *fp = tmpfile();
clearerr(fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_feof(void) {
FILE *fp = tmpfile();
feof(fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_ferror(void) {
FILE *fp = tmpfile();
ferror(fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void check_fileno(void) {
FILE *fp = tmpfile();
fileno(fp); // expected-warning {{Stream pointer might be NULL}}
fclose(fp);
}
void f_open(void) {
FILE *p = fopen("foo", "r");
char buf[1024];
fread(buf, 1, 1, p); // expected-warning {{Stream pointer might be NULL}}
fclose(p);
}
void f_dopen(int fd) {
FILE *F = fdopen(fd, "r");
char buf[1024];
fread(buf, 1, 1, F); // expected-warning {{Stream pointer might be NULL}}
fclose(F);
}
void f_vfprintf(int fd, va_list args) {
FILE *F = fdopen(fd, "r");
vfprintf(F, "%d", args); // expected-warning {{Stream pointer might be NULL}}
fclose(F);
}
void f_vfscanf(int fd, va_list args) {
FILE *F = fdopen(fd, "r");
vfscanf(F, "%u", args); // expected-warning {{Stream pointer might be NULL}}
fclose(F);
}
void f_seek(void) {
FILE *p = fopen("foo", "r");
if (!p)
return;
fseek(p, 1, SEEK_SET); // no-warning
fseek(p, 1, 3); // expected-warning {{The whence argument to fseek() should be SEEK_SET, SEEK_END, or SEEK_CUR}}
fclose(p);
}
void f_seeko(void) {
FILE *p = fopen("foo", "r");
if (!p)
return;
fseeko(p, 1, SEEK_SET); // no-warning
fseeko(p, 1, 3); // expected-warning {{The whence argument to fseek() should be SEEK_SET, SEEK_END, or SEEK_CUR}}
fclose(p);
}
void f_double_close(void) {
FILE *p = fopen("foo", "r");
if (!p)
return;
fclose(p);
fclose(p); // expected-warning {{Stream might be already closed}}
}
void f_double_close_alias(void) {
FILE *p1 = fopen("foo", "r");
if (!p1)
return;
FILE *p2 = p1;
fclose(p1);
fclose(p2); // expected-warning {{Stream might be already closed}}
}
void f_use_after_close(void) {
FILE *p = fopen("foo", "r");
if (!p)
return;
fclose(p);
clearerr(p); // expected-warning {{Stream might be already closed}}
}
void f_open_after_close(void) {
FILE *p = fopen("foo", "r");
if (!p)
return;
fclose(p);
p = fopen("foo", "r");
if (!p)
return;
fclose(p);
}
void f_reopen_after_close(void) {
FILE *p = fopen("foo", "r");
if (!p)
return;
fclose(p);
// Allow reopen after close.
p = freopen("foo", "w", p);
if (!p)
return;
fclose(p);
}
void f_leak(int c) {
FILE *p = fopen("foo.c", "r");
if (!p)
return;
if(c)
return; // expected-warning {{Opened stream never closed. Potential resource leak}}
fclose(p);
}
FILE *f_null_checked(void) {
FILE *p = fopen("foo.c", "r");
if (p)
return p; // no-warning
else
return 0;
}
void pr7831(FILE *fp) {
fclose(fp); // no-warning
}
// PR 8081 - null pointer crash when 'whence' is not an integer constant
void pr8081(FILE *stream, long offset, int whence) {
fseek(stream, offset, whence);
}
void check_freopen_1(void) {
FILE *f1 = freopen("foo.c", "r", (FILE *)0); // expected-warning {{Stream pointer might be NULL}}
f1 = freopen(0, "w", (FILE *)0x123456); // Do not report this as error.
}
void check_freopen_2(void) {
FILE *f1 = fopen("foo.c", "r");
if (f1) {
FILE *f2 = freopen(0, "w", f1);
if (f2) {
// Check if f1 and f2 point to the same stream.
fclose(f1);
fclose(f2); // expected-warning {{Stream might be already closed.}}
} else {
// Reopen failed.
// f1 is non-NULL but points to a possibly invalid stream.
rewind(f1); // expected-warning {{Stream might be invalid}}
// f2 is NULL but the previous error stops the checker.
rewind(f2);
}
}
}
void check_freopen_3(void) {
FILE *f1 = fopen("foo.c", "r");
if (f1) {
// Unchecked result of freopen.
// The f1 may be invalid after this call.
freopen(0, "w", f1);
rewind(f1); // expected-warning {{Stream might be invalid}}
fclose(f1);
}
}
extern FILE *GlobalF;
extern void takeFile(FILE *);
void check_escape1(void) {
FILE *F = tmpfile();
if (!F)
return;
fwrite("1", 1, 1, F); // may fail
GlobalF = F;
fwrite("1", 1, 1, F); // no warning
}
void check_escape2(void) {
FILE *F = tmpfile();
if (!F)
return;
fwrite("1", 1, 1, F); // may fail
takeFile(F);
fwrite("1", 1, 1, F); // no warning
}
void check_escape3(void) {
FILE *F = tmpfile();
if (!F)
return;
takeFile(F);
F = freopen(0, "w", F);
if (!F)
return;
fwrite("1", 1, 1, F); // may fail
fwrite("1", 1, 1, F); // no warning
}
void check_escape4(void) {
FILE *F = tmpfile();
if (!F)
return;
fwrite("1", 1, 1, F); // may fail
// no escape at a non-StreamChecker-handled system call
setbuf(F, "0");
fwrite("1", 1, 1, F); // expected-warning {{might be 'indeterminate'}}
fclose(F);
}
int Test;
_Noreturn void handle_error(void);
void check_leak_noreturn_1(void) {
FILE *F1 = tmpfile();
if (!F1)
return;
if (Test == 1) {
handle_error(); // no warning
}
rewind(F1);
} // expected-warning {{Opened stream never closed. Potential resource leak}}
// Check that "location uniqueing" works.
// This results in reporting only one occurence of resource leak for a stream.
void check_leak_noreturn_2(void) {
FILE *F1 = tmpfile();
if (!F1)
return;
if (Test == 1) {
return; // no warning
}
rewind(F1);
} // expected-warning {{Opened stream never closed. Potential resource leak}}
// FIXME: This warning should be placed at the `return` above.
// See https://reviews.llvm.org/D83120 about details.
void fflush_after_fclose(void) {
FILE *F = tmpfile();
int Ret;
fflush(NULL); // no-warning
if (!F)
return;
if ((Ret = fflush(F)) != 0)
clang_analyzer_eval(Ret == EOF); // expected-warning {{TRUE}}
fclose(F);
fflush(F); // expected-warning {{Stream might be already closed}}
}
void fflush_on_open_failed_stream(void) {
FILE *F = tmpfile();
if (!F) {
fflush(F); // no-warning
return;
}
fclose(F);
}
void getline_null_file() {
char *buffer = NULL;
size_t n = 0;
getline(&buffer, &n, NULL); // expected-warning {{Stream pointer might be NULL}}
}
void getdelim_null_file() {
char *buffer = NULL;
size_t n = 0;
getdelim(&buffer, &n, '\n', NULL); // expected-warning {{Stream pointer might be NULL}}
}
void getline_buffer_on_error() {
FILE *file = fopen("file.txt", "r");
if (file == NULL) {
return;
}
char *line = NULL;
size_t len = 0;
if (getline(&line, &len, file) == -1) {
if (line[0] == '\0') {} // expected-warning {{The left operand of '==' is a garbage value}}
} else {
if (line[0] == '\0') {} // no warning
}
free(line);
fclose(file);
}
void getline_ret_value() {
FILE *file = fopen("file.txt", "r");
if (file == NULL) {
return;
}
size_t n = 0;
char *buffer = NULL;
ssize_t r = getline(&buffer, &n, file);
if (r > -1) {
// The return value does *not* include the terminating null byte.
// The buffer must be large enough to include it.
clang_analyzer_eval(n > r); // expected-warning{{TRUE}}
clang_analyzer_eval(buffer != NULL); // expected-warning{{TRUE}}
}
fclose(file);
free(buffer);
}
void getline_buffer_size_negative() {
FILE *file = fopen("file.txt", "r");
if (file == NULL) {
return;
}
size_t n = -1;
clang_analyzer_eval((ssize_t)n >= 0); // expected-warning{{FALSE}}
char *buffer = NULL;
ssize_t r = getline(&buffer, &n, file);
if (r > -1) {
clang_analyzer_eval((ssize_t)n > r); // expected-warning{{TRUE}}
clang_analyzer_eval(buffer != NULL); // expected-warning{{TRUE}}
}
free(buffer);
fclose(file);
}
void gh_93408_regression(void *buffer) {
FILE *f = fopen("/tmp/foo.txt", "r");
fread(buffer, 1, 1, f); // expected-warning {{Stream pointer might be NULL}} no-crash
fclose(f);
}
typedef void VOID;
void gh_93408_regression_typedef(VOID *buffer) {
FILE *f = fopen("/tmp/foo.txt", "r");
fread(buffer, 1, 1, f); // expected-warning {{Stream pointer might be NULL}} no-crash
fclose(f);
}
struct FAM {
int data;
int tail[];
};
struct FAM0 {
int data;
int tail[0];
};
void gh_93408_regression_FAM(struct FAM *p) {
FILE *f = fopen("/tmp/foo.txt", "r");
fread(p->tail, 1, 1, f); // expected-warning {{Stream pointer might be NULL}}
fclose(f);
}
void gh_93408_regression_FAM0(struct FAM0 *p) {
FILE *f = fopen("/tmp/foo.txt", "r");
fread(p->tail, 1, 1, f); // expected-warning {{Stream pointer might be NULL}}
fclose(f);
}
struct ZeroSized {
int data[0];
};
void gh_93408_regression_ZeroSized(struct ZeroSized *buffer) {
FILE *f = fopen("/tmp/foo.txt", "r");
fread(buffer, 1, 1, f); // expected-warning {{Stream pointer might be NULL}} no-crash
fclose(f);
}
|