| 1 |
/* C only: C++ exceptions double the fopen leak warnings. |
| 2 |
Therefore this test has been duplicated as |
| 3 |
c-c++-common/analyzer/file-pr58237-noexcept.c */ |
| 4 |
|
| 5 |
typedef struct FILE FILE; |
| 6 |
|
| 7 |
FILE* fopen (const char*, const char*); |
| 8 |
int fclose (FILE*); |
| 9 |
char *fgets (char *, int, FILE *); |
| 10 |
|
| 11 |
#include "analyzer-decls.h" |
| 12 |
|
| 13 |
void f0(const char *str) |
| 14 |
{ |
| 15 |
FILE * fp = fopen(str, "r"); /* { dg-message "opened here" } */ |
| 16 |
// ideally warning should be located at the end of the function |
| 17 |
char buf[10]; |
| 18 |
fgets(buf, 10, fp); |
| 19 |
} /* { dg-warning "leak of FILE 'fp'" } */ |
| 20 |
|
| 21 |
void f1(const char *str) |
| 22 |
{ |
| 23 |
FILE * fp = fopen(str, "r"); /* { dg-message "opened here" } */ |
| 24 |
// ideally warning should be located at the end of the function |
| 25 |
char buf[10]; |
| 26 |
|
| 27 |
while (fgets(buf, 10, fp) != NULL) |
| 28 |
{ |
| 29 |
/* Do something with buf */ |
| 30 |
} |
| 31 |
} /* { dg-warning "leak of FILE 'fp'" } */ |
| 32 |
|
| 33 |
void f2(const char *str, int flag) |
| 34 |
{ |
| 35 |
FILE * fp = fopen(str, "r"); /* { dg-message "opened here" } */ |
| 36 |
// ideally warning should be located at the end of the function |
| 37 |
char buf[10]; |
| 38 |
|
| 39 |
while (fgets(buf, 10, fp) != NULL) |
| 40 |
{ |
| 41 |
/* Do something with buf */ |
| 42 |
} |
| 43 |
if (flag) /* { dg-message "when 'flag == 0'" } */ |
| 44 |
fclose(fp); |
| 45 |
} /* { dg-warning "leak of FILE 'fp'" } */ |
| 46 |
|
| 47 |
extern void called_by_f3( FILE * fp); |
| 48 |
|
| 49 |
void f3(const char *str) |
| 50 |
{ |
| 51 |
FILE * fp = fopen(str, "r"); |
| 52 |
char buf[10]; |
| 53 |
|
| 54 |
while (fgets(buf, 10, fp) != NULL) |
| 55 |
{ |
| 56 |
/* Do something with buf */ |
| 57 |
} |
| 58 |
/* Not sure if fclose executed by called_by_f3 or not. Say nothing */ |
| 59 |
called_by_f3(fp); |
| 60 |
} |
| 61 |
|
| 62 |
void f4(const char *str) |
| 63 |
{ |
| 64 |
FILE * fp = fopen(str, "r"); |
| 65 |
char buf[10]; |
| 66 |
|
| 67 |
while (fgets(buf, 10, fp) != NULL) |
| 68 |
{ |
| 69 |
/* Do something with buf */ |
| 70 |
} |
| 71 |
/* Nothing to say here. */ |
| 72 |
fclose(fp); |
| 73 |
} |
| 74 |
|
| 75 |
int main(int argc, const char * argv[]) |
| 76 |
{ |
| 77 |
FILE * fp = fopen(argv[0], "r"); |
| 78 |
char buf[10]; |
| 79 |
|
| 80 |
while (fgets(buf, 10, fp) != NULL) |
| 81 |
{ |
| 82 |
/* Do something with buf */ |
| 83 |
} |
| 84 |
/* Nothing to say here, because we are in main. */ |
| 85 |
} |