1 // RUN: %clang_cc1 -analyze -analyzer-check-objc-mem -analyzer-experimental-checks -analyzer-store region -verify %s
3 typedef __typeof__(sizeof(int)) size_t;
4 typedef struct _IO_FILE
FILE;
5 #define SEEK_SET 0 /* Seek from beginning of file. */
6 #define SEEK_CUR 1 /* Seek from current position. */
7 #define SEEK_END 2 /* Seek from end of file. */
8 extern FILE *fopen(const char *path
, const char *mode
);
9 extern FILE *tmpfile(void);
10 extern int fclose(FILE *fp
);
11 extern size_t fread(void *ptr
, size_t size
, size_t nmemb
, FILE *stream
);
12 extern int fseek (FILE *__stream
, long int __off
, int __whence
);
13 extern long int ftell (FILE *__stream
);
14 extern void rewind (FILE *__stream
);
17 FILE *p
= fopen("foo", "r");
19 fread(buf
, 1, 1, p
); // expected-warning {{Stream pointer might be NULL.}}
24 FILE *p
= fopen("foo", "r");
25 fseek(p
, 1, SEEK_SET
); // expected-warning {{Stream pointer might be NULL.}}
30 FILE *p
= fopen("foo", "r");
31 ftell(p
); // expected-warning {{Stream pointer might be NULL.}}
36 FILE *p
= fopen("foo", "r");
37 rewind(p
); // expected-warning {{Stream pointer might be NULL.}}
42 FILE *p
= fopen("foo", "r");
45 fseek(p
, 1, SEEK_SET
); // no-warning
46 fseek(p
, 1, 3); // expected-warning {{The whence argument to fseek() should be SEEK_SET, SEEK_END, or SEEK_CUR.}}
51 FILE *p
= fopen("foo", "r");
53 fclose(p
); // expected-warning {{Try to close a file Descriptor already closed. Cause undefined behaviour.}}
58 ftell(p
); // expected-warning {{Stream pointer might be NULL.}}
63 FILE *p
= fopen("foo.c", "r");
65 return; // expected-warning {{Opened File never closed. Potential Resource leak.}}
70 FILE *p
= fopen("foo.c", "r");
72 return p
; // no-warning
77 void pr7831(FILE *fp
) {
78 fclose(fp
); // no-warning
81 // PR 8081 - null pointer crash when 'whence' is not an integer constant
82 void pr8081(FILE *stream
, long offset
, int whence
) {
83 fseek(stream
, offset
, whence
);