Ted Kremenek | cdc3a89 | 2012-08-24 20:39:55 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=alpha.unix.Stream -analyzer-store region -verify %s |
Zhongxing Xu | c196095 | 2010-06-16 05:38:05 +0000 | [diff] [blame] | 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 | 47dc37f | 2010-07-22 14:01:01 +0000 | [diff] [blame] | 9 | extern FILE *tmpfile(void); |
Zhongxing Xu | 9843ba9 | 2010-07-19 01:52:29 +0000 | [diff] [blame] | 10 | extern int fclose(FILE *fp); |
Zhongxing Xu | 23d90f9 | 2010-06-18 02:47:46 +0000 | [diff] [blame] | 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); |
Zhongxing Xu | c196095 | 2010-06-16 05:38:05 +0000 | [diff] [blame] | 15 | |
| 16 | void f1(void) { |
| 17 | FILE *p = fopen("foo", "r"); |
| 18 | char buf[1024]; |
Ted Kremenek | c4bac8e | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 19 | fread(buf, 1, 1, p); // expected-warning {{Stream pointer might be NULL}} |
Zhongxing Xu | 766c201 | 2010-07-23 14:14:59 +0000 | [diff] [blame] | 20 | fclose(p); |
Zhongxing Xu | c196095 | 2010-06-16 05:38:05 +0000 | [diff] [blame] | 21 | } |
Zhongxing Xu | 23d90f9 | 2010-06-18 02:47:46 +0000 | [diff] [blame] | 22 | |
| 23 | void f2(void) { |
| 24 | FILE *p = fopen("foo", "r"); |
Ted Kremenek | c4bac8e | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 25 | fseek(p, 1, SEEK_SET); // expected-warning {{Stream pointer might be NULL}} |
Zhongxing Xu | 766c201 | 2010-07-23 14:14:59 +0000 | [diff] [blame] | 26 | fclose(p); |
Zhongxing Xu | 23d90f9 | 2010-06-18 02:47:46 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void f3(void) { |
| 30 | FILE *p = fopen("foo", "r"); |
Ted Kremenek | c4bac8e | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 31 | ftell(p); // expected-warning {{Stream pointer might be NULL}} |
Zhongxing Xu | 766c201 | 2010-07-23 14:14:59 +0000 | [diff] [blame] | 32 | fclose(p); |
Zhongxing Xu | 23d90f9 | 2010-06-18 02:47:46 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void f4(void) { |
| 36 | FILE *p = fopen("foo", "r"); |
Ted Kremenek | c4bac8e | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 37 | rewind(p); // expected-warning {{Stream pointer might be NULL}} |
Zhongxing Xu | 766c201 | 2010-07-23 14:14:59 +0000 | [diff] [blame] | 38 | fclose(p); |
Zhongxing Xu | 23d90f9 | 2010-06-18 02:47:46 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Zhongxing Xu | 0c2e8c8 | 2010-06-24 13:36:41 +0000 | [diff] [blame] | 41 | void f5(void) { |
| 42 | FILE *p = fopen("foo", "r"); |
| 43 | if (!p) |
| 44 | return; |
| 45 | fseek(p, 1, SEEK_SET); // no-warning |
Ted Kremenek | c4bac8e | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 46 | fseek(p, 1, 3); // expected-warning {{The whence argument to fseek() should be SEEK_SET, SEEK_END, or SEEK_CUR}} |
Zhongxing Xu | 766c201 | 2010-07-23 14:14:59 +0000 | [diff] [blame] | 47 | fclose(p); |
Zhongxing Xu | 0c2e8c8 | 2010-06-24 13:36:41 +0000 | [diff] [blame] | 48 | } |
Zhongxing Xu | 9843ba9 | 2010-07-19 01:52:29 +0000 | [diff] [blame] | 49 | |
| 50 | void f6(void) { |
| 51 | FILE *p = fopen("foo", "r"); |
| 52 | fclose(p); |
Ted Kremenek | c4bac8e | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 53 | 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] | 54 | } |
Zhongxing Xu | 47dc37f | 2010-07-22 14:01:01 +0000 | [diff] [blame] | 55 | |
| 56 | void f7(void) { |
| 57 | FILE *p = tmpfile(); |
Ted Kremenek | c4bac8e | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 58 | ftell(p); // expected-warning {{Stream pointer might be NULL}} |
Zhongxing Xu | 766c201 | 2010-07-23 14:14:59 +0000 | [diff] [blame] | 59 | fclose(p); |
| 60 | } |
| 61 | |
| 62 | void f8(int c) { |
| 63 | FILE *p = fopen("foo.c", "r"); |
| 64 | if(c) |
Ted Kremenek | c4bac8e | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 65 | return; // expected-warning {{Opened File never closed. Potential Resource leak}} |
Zhongxing Xu | 766c201 | 2010-07-23 14:14:59 +0000 | [diff] [blame] | 66 | fclose(p); |
| 67 | } |
| 68 | |
| 69 | FILE *f9(void) { |
| 70 | FILE *p = fopen("foo.c", "r"); |
| 71 | if (p) |
| 72 | return p; // no-warning |
| 73 | else |
| 74 | return 0; |
Zhongxing Xu | 47dc37f | 2010-07-22 14:01:01 +0000 | [diff] [blame] | 75 | } |
Zhongxing Xu | a87b1eb | 2010-08-06 00:04:40 +0000 | [diff] [blame] | 76 | |
| 77 | void pr7831(FILE *fp) { |
| 78 | fclose(fp); // no-warning |
| 79 | } |
Ted Kremenek | 02b49bb | 2010-09-07 20:45:26 +0000 | [diff] [blame] | 80 | |
| 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); |
| 84 | } |
| 85 | |