Zhongxing Xu | c196095 | 2010-06-16 05:38:05 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-check-objc-mem -analyzer-experimental-checks -analyzer-store region -verify %s |
| 2 | |
| 3 | typedef __typeof__(sizeof(int)) size_t; |
| 4 | typedef struct _IO_FILE FILE; |
Zhongxing Xu | 23d90f9 | 2010-06-18 02:47:46 +0000 | [diff] [blame] | 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); |
Zhongxing Xu | 9843ba9 | 2010-07-19 01:52:29 +0000 | [diff] [blame] | 9 | extern int fclose(FILE *fp); |
Zhongxing Xu | 23d90f9 | 2010-06-18 02:47:46 +0000 | [diff] [blame] | 10 | extern size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); |
| 11 | extern int fseek (FILE *__stream, long int __off, int __whence); |
| 12 | extern long int ftell (FILE *__stream); |
| 13 | extern void rewind (FILE *__stream); |
Zhongxing Xu | c196095 | 2010-06-16 05:38:05 +0000 | [diff] [blame] | 14 | |
| 15 | void f1(void) { |
| 16 | FILE *p = fopen("foo", "r"); |
| 17 | char buf[1024]; |
| 18 | fread(buf, 1, 1, p); // expected-warning {{Stream pointer might be NULL.}} |
| 19 | } |
Zhongxing Xu | 23d90f9 | 2010-06-18 02:47:46 +0000 | [diff] [blame] | 20 | |
| 21 | void f2(void) { |
| 22 | FILE *p = fopen("foo", "r"); |
| 23 | fseek(p, 1, SEEK_SET); // expected-warning {{Stream pointer might be NULL.}} |
| 24 | } |
| 25 | |
| 26 | void f3(void) { |
| 27 | FILE *p = fopen("foo", "r"); |
| 28 | ftell(p); // expected-warning {{Stream pointer might be NULL.}} |
| 29 | } |
| 30 | |
| 31 | void f4(void) { |
| 32 | FILE *p = fopen("foo", "r"); |
| 33 | rewind(p); // expected-warning {{Stream pointer might be NULL.}} |
| 34 | } |
| 35 | |
Zhongxing Xu | 0c2e8c8 | 2010-06-24 13:36:41 +0000 | [diff] [blame] | 36 | void f5(void) { |
| 37 | FILE *p = fopen("foo", "r"); |
| 38 | if (!p) |
| 39 | return; |
| 40 | fseek(p, 1, SEEK_SET); // no-warning |
| 41 | fseek(p, 1, 3); // expected-warning {{The whence argument to fseek() should be SEEK_SET, SEEK_END, or SEEK_CUR.}} |
| 42 | } |
Zhongxing Xu | 9843ba9 | 2010-07-19 01:52:29 +0000 | [diff] [blame] | 43 | |
| 44 | void f6(void) { |
| 45 | FILE *p = fopen("foo", "r"); |
| 46 | fclose(p); |
Zhongxing Xu | c6a36ff | 2010-07-19 02:06:14 +0000 | [diff] [blame^] | 47 | fclose(p); // expected-warning {{Try to close a file Descriptor already closed. Cause undefined behaviour.}} |
Zhongxing Xu | 9843ba9 | 2010-07-19 01:52:29 +0000 | [diff] [blame] | 48 | } |