stream.c revision 47dc37f1efa6942366dd61c4acb0c874049dd1e0
1// RUN: %clang_cc1 -analyze -analyzer-check-objc-mem -analyzer-experimental-checks -analyzer-store region -verify %s
2
3typedef __typeof__(sizeof(int)) size_t;
4typedef 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.  */
8extern FILE *fopen(const char *path, const char *mode);
9extern FILE *tmpfile(void);
10extern int fclose(FILE *fp);
11extern size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
12extern int fseek (FILE *__stream, long int __off, int __whence);
13extern long int ftell (FILE *__stream);
14extern void rewind (FILE *__stream);
15
16void f1(void) {
17  FILE *p = fopen("foo", "r");
18  char buf[1024];
19  fread(buf, 1, 1, p); // expected-warning {{Stream pointer might be NULL.}}
20}
21
22void f2(void) {
23  FILE *p = fopen("foo", "r");
24  fseek(p, 1, SEEK_SET); // expected-warning {{Stream pointer might be NULL.}}
25}
26
27void f3(void) {
28  FILE *p = fopen("foo", "r");
29  ftell(p); // expected-warning {{Stream pointer might be NULL.}}
30}
31
32void f4(void) {
33  FILE *p = fopen("foo", "r");
34  rewind(p); // expected-warning {{Stream pointer might be NULL.}}
35}
36
37void f5(void) {
38  FILE *p = fopen("foo", "r");
39  if (!p)
40    return;
41  fseek(p, 1, SEEK_SET); // no-warning
42  fseek(p, 1, 3); // expected-warning {{The whence argument to fseek() should be SEEK_SET, SEEK_END, or SEEK_CUR.}}
43}
44
45void f6(void) {
46  FILE *p = fopen("foo", "r");
47  fclose(p);
48  fclose(p); // expected-warning {{Try to close a file Descriptor already closed. Cause undefined behaviour.}}
49}
50
51void f7(void) {
52  FILE *p = tmpfile();
53  ftell(p); // expected-warning {{Stream pointer might be NULL.}}
54}
55