blob: 329a7823f2e570cbf32d8ca605f220dce7013f31 [file] [log] [blame]
Ted Kremenekcdc3a892012-08-24 20:39:55 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.unix.Stream -analyzer-store region -verify %s
Zhongxing Xuc1960952010-06-16 05:38:05 +00002
3typedef __typeof__(sizeof(int)) size_t;
4typedef struct _IO_FILE FILE;
Zhongxing Xu23d90f92010-06-18 02:47:46 +00005#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);
Zhongxing Xu47dc37f2010-07-22 14:01:01 +00009extern FILE *tmpfile(void);
Zhongxing Xu9843ba92010-07-19 01:52:29 +000010extern int fclose(FILE *fp);
Zhongxing Xu23d90f92010-06-18 02:47:46 +000011extern 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);
Zhongxing Xuc1960952010-06-16 05:38:05 +000015
16void f1(void) {
17 FILE *p = fopen("foo", "r");
18 char buf[1024];
Ted Kremenekc4bac8e2012-08-16 17:45:23 +000019 fread(buf, 1, 1, p); // expected-warning {{Stream pointer might be NULL}}
Zhongxing Xu766c2012010-07-23 14:14:59 +000020 fclose(p);
Zhongxing Xuc1960952010-06-16 05:38:05 +000021}
Zhongxing Xu23d90f92010-06-18 02:47:46 +000022
23void f2(void) {
24 FILE *p = fopen("foo", "r");
Ted Kremenekc4bac8e2012-08-16 17:45:23 +000025 fseek(p, 1, SEEK_SET); // expected-warning {{Stream pointer might be NULL}}
Zhongxing Xu766c2012010-07-23 14:14:59 +000026 fclose(p);
Zhongxing Xu23d90f92010-06-18 02:47:46 +000027}
28
29void f3(void) {
30 FILE *p = fopen("foo", "r");
Ted Kremenekc4bac8e2012-08-16 17:45:23 +000031 ftell(p); // expected-warning {{Stream pointer might be NULL}}
Zhongxing Xu766c2012010-07-23 14:14:59 +000032 fclose(p);
Zhongxing Xu23d90f92010-06-18 02:47:46 +000033}
34
35void f4(void) {
36 FILE *p = fopen("foo", "r");
Ted Kremenekc4bac8e2012-08-16 17:45:23 +000037 rewind(p); // expected-warning {{Stream pointer might be NULL}}
Zhongxing Xu766c2012010-07-23 14:14:59 +000038 fclose(p);
Zhongxing Xu23d90f92010-06-18 02:47:46 +000039}
40
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +000041void f5(void) {
42 FILE *p = fopen("foo", "r");
43 if (!p)
44 return;
45 fseek(p, 1, SEEK_SET); // no-warning
Ted Kremenekc4bac8e2012-08-16 17:45:23 +000046 fseek(p, 1, 3); // expected-warning {{The whence argument to fseek() should be SEEK_SET, SEEK_END, or SEEK_CUR}}
Zhongxing Xu766c2012010-07-23 14:14:59 +000047 fclose(p);
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +000048}
Zhongxing Xu9843ba92010-07-19 01:52:29 +000049
50void f6(void) {
51 FILE *p = fopen("foo", "r");
52 fclose(p);
Ted Kremenekc4bac8e2012-08-16 17:45:23 +000053 fclose(p); // expected-warning {{Try to close a file Descriptor already closed. Cause undefined behaviour}}
Zhongxing Xu9843ba92010-07-19 01:52:29 +000054}
Zhongxing Xu47dc37f2010-07-22 14:01:01 +000055
56void f7(void) {
57 FILE *p = tmpfile();
Ted Kremenekc4bac8e2012-08-16 17:45:23 +000058 ftell(p); // expected-warning {{Stream pointer might be NULL}}
Zhongxing Xu766c2012010-07-23 14:14:59 +000059 fclose(p);
60}
61
62void f8(int c) {
63 FILE *p = fopen("foo.c", "r");
64 if(c)
Ted Kremenekc4bac8e2012-08-16 17:45:23 +000065 return; // expected-warning {{Opened File never closed. Potential Resource leak}}
Zhongxing Xu766c2012010-07-23 14:14:59 +000066 fclose(p);
67}
68
69FILE *f9(void) {
70 FILE *p = fopen("foo.c", "r");
71 if (p)
72 return p; // no-warning
73 else
74 return 0;
Zhongxing Xu47dc37f2010-07-22 14:01:01 +000075}
Zhongxing Xua87b1eb2010-08-06 00:04:40 +000076
77void pr7831(FILE *fp) {
78 fclose(fp); // no-warning
79}
Ted Kremenek02b49bb2010-09-07 20:45:26 +000080
81// PR 8081 - null pointer crash when 'whence' is not an integer constant
82void pr8081(FILE *stream, long offset, int whence) {
83 fseek(stream, offset, whence);
84}
85