brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.0 KiB · b28cc30 Raw
281 lines · c
1// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Stream,unix.Errno,unix.StdCLibraryFunctions,debug.ExprInspection \2// RUN:   -analyzer-config unix.Stream:Pedantic=true \3// RUN:   -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true -verify %s4 5#include "Inputs/system-header-simulator.h"6#include "Inputs/errno_func.h"7 8extern void clang_analyzer_eval(int);9extern void clang_analyzer_dump(int);10extern void clang_analyzer_printState();11 12void check_fopen(void) {13  FILE *F = fopen("xxx", "r");14  if (!F) {15    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}16    if (errno) {} // no-warning17    return;18  }19  if (errno) {} // expected-warning{{An undefined value may be read from 'errno' [unix.Errno]}}20}21 22void check_fdopen(int Fd) {23  FILE *F = fdopen(Fd, "r");24  if (!F) {25    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}26    if (errno) {}                    // no-warning27  } else {28    if (errno) {}                    // expected-warning{{An undefined value may be read from 'errno' [unix.Errno]}}29  }30}31 32void check_tmpfile(void) {33  FILE *F = tmpfile();34  if (!F) {35    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}36    if (errno) {} // no-warning37    return;38  }39  if (errno) {} // expected-warning{{An undefined value may be read from 'errno' [unix.Errno]}}40}41 42void check_freopen(void) {43  FILE *F = tmpfile();44  if (!F)45    return;46  F = freopen("xxx", "w", F);47  if (!F) {48    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}49    if (errno) {} // no-warning50    return;51  }52  if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}53}54 55void check_fclose(void) {56  FILE *F = tmpfile();57  if (!F)58    return;59  int Ret = fclose(F);60  if (Ret == EOF) {61    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}62    if (errno) {} // no-warning63    return;64  }65  if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}66}67 68void check_fread_size0(void) {69  char Buf[10];70  FILE *F = tmpfile();71  if (!F)72    return;73  fread(Buf, 0, 1, F);74  if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}75}76 77void check_fread_nmemb0(void) {78  char Buf[10];79  FILE *F = tmpfile();80  if (!F)81    return;82  fread(Buf, 1, 0, F);83  if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}84}85 86void check_fread(void) {87  char Buf[10];88  FILE *F = tmpfile();89  if (!F)90    return;91 92  int R = fread(Buf, 1, 10, F);93  if (R < 10) {94    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}95    if (errno) {} // no-warning96    fclose(F);97    return;98  }99  if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}100}101 102void check_fwrite_size0(void) {103  char Buf[] = "0123456789";104  FILE *F = tmpfile();105  if (!F)106    return;107  fwrite(Buf, 0, 1, F);108  if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}109}110 111void check_fwrite_nmemb0(void) {112  char Buf[] = "0123456789";113  FILE *F = tmpfile();114  if (!F)115    return;116  fwrite(Buf, 1, 0, F);117  if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}118}119 120void check_fwrite(void) {121  char Buf[] = "0123456789";122  FILE *F = tmpfile();123  if (!F)124    return;125 126  int R = fwrite(Buf, 1, 10, F);127  if (R < 10) {128    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}129    if (errno) {} // no-warning130    fclose(F);131    return;132  }133  if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}134}135 136void check_fseek(void) {137  FILE *F = tmpfile();138  if (!F)139    return;140  int S = fseek(F, 11, SEEK_SET);141  if (S != 0) {142    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}143    clang_analyzer_eval(S == -1);    // expected-warning{{TRUE}}144    if (errno) {} // no-warning145    fclose(F);146    return;147  }148  if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}149}150 151void check_fseeko(void) {152  FILE *F = tmpfile();153  if (!F)154    return;155  int S = fseeko(F, 11, SEEK_SET);156  if (S == -1) {157    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}158    if (errno) {}                    // no-warning159  } else {160    clang_analyzer_eval(S == 0);     // expected-warning{{TRUE}}161    if (errno) {}                    // expected-warning{{An undefined value may be read from 'errno'}}162  }163  fclose(F);164}165 166void check_no_errno_change(void) {167  FILE *F = tmpfile();168  if (!F)169    return;170  errno = 1;171  clearerr(F);172  if (errno) {} // no-warning173  feof(F);174  if (errno) {} // no-warning175  ferror(F);176  if (errno) {} // no-warning177  fileno(F);178  if (errno) {} // no-warning179  clang_analyzer_eval(errno == 1); // expected-warning{{TRUE}}180  fclose(F);181}182 183void check_fgetpos(void) {184  FILE *F = tmpfile();185  if (!F)186    return;187  errno = 0;188  fpos_t Pos;189  int Ret = fgetpos(F, &Pos);190  if (Ret)191    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}192  else193    clang_analyzer_eval(errno == 0); // expected-warning{{TRUE}}194  if (errno) {} // no-warning195  fclose(F);196}197 198void check_fsetpos(void) {199  FILE *F = tmpfile();200  if (!F)201    return;202  errno = 0;203  fpos_t Pos;204  int Ret = fsetpos(F, &Pos);205  if (Ret)206    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}207  else208    clang_analyzer_eval(errno == 0); // expected-warning{{TRUE}}209  if (errno) {} // no-warning210  fclose(F);211}212 213void check_ftell(void) {214  FILE *F = tmpfile();215  if (!F)216    return;217  errno = 0;218  long Ret = ftell(F);219  if (Ret == -1) {220    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}221  } else {222    clang_analyzer_eval(errno == 0); // expected-warning{{TRUE}}223    clang_analyzer_eval(Ret >= 0); // expected-warning{{TRUE}}224  }225  if (errno) {} // no-warning226  fclose(F);227}228 229void check_ftello(void) {230  FILE *F = tmpfile();231  if (!F)232    return;233  off_t Ret = ftello(F);234  if (Ret >= 0) {235    if (errno) {}                    // expected-warning{{An undefined value may be read from 'errno'}}236  } else {237    clang_analyzer_eval(Ret == -1);  // expected-warning{{TRUE}}238    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}239    if (errno) {}                    // no-warning240  }241  fclose(F);242}243 244void check_rewind(void) {245  FILE *F = tmpfile();246  if (!F)247    return;248  errno = 0;249  rewind(F);250  clang_analyzer_eval(errno == 0);251  // expected-warning@-1{{FALSE}}252  // expected-warning@-2{{TRUE}}253  fclose(F);254}255 256void check_fflush_opened_file(void) {257  FILE *F = tmpfile();258  if (!F)259    return;260  int N = fflush(F);261  if (N == EOF) {262    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}263    if (errno) {}                    // no-warning264  } else {265    clang_analyzer_eval(N == 0);     // expected-warning{{TRUE}}266    if (errno) {}                    // expected-warning{{An undefined value may be read from 'errno'}}267  }268  fclose(F);269}270 271void check_fflush_all(void) {272  int N = fflush(NULL);273  if (N == 0) {274    if (errno) {}                    // expected-warning{{An undefined value may be read from 'errno'}}275  } else {276    clang_analyzer_eval(N == EOF);   // expected-warning{{TRUE}}277    clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}278    if (errno) {}                    // no-warning279  }280}281