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