Dominic Chen | 184c624 | 2017-03-03 18:02:02 +0000 | [diff] [blame] | 1 | // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.API -verify %s |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 2 | |
| 3 | #ifndef O_RDONLY |
| 4 | #define O_RDONLY 0 |
| 5 | #endif |
| 6 | |
| 7 | #ifndef NULL |
| 8 | #define NULL ((void*) 0) |
| 9 | #endif |
| 10 | |
| 11 | int open(const char *, int, ...); |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 12 | int openat(int, const char *, int, ...); |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 13 | int close(int fildes); |
| 14 | |
| 15 | void open_1(const char *path) { |
| 16 | int fd; |
| 17 | fd = open(path, O_RDONLY); // no-warning |
| 18 | if (fd > -1) |
| 19 | close(fd); |
| 20 | } |
| 21 | |
| 22 | void open_2(const char *path) { |
| 23 | int fd; |
| 24 | int mode = 0x0; |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 25 | fd = open(path, O_RDONLY, mode, NULL); // expected-warning{{Call to 'open' with more than 3 arguments}} |
| 26 | if (fd > -1) |
| 27 | close(fd); |
| 28 | } |
| 29 | |
| 30 | void openat_2(int base_fd, const char *path) { |
| 31 | int fd; |
| 32 | int mode = 0x0; |
| 33 | fd = openat(base_fd, path, O_RDONLY, mode, NULL); // expected-warning{{Call to 'openat' with more than 4 arguments}} |
Jordan Rose | cd4db5c | 2014-08-20 16:58:03 +0000 | [diff] [blame] | 34 | if (fd > -1) |
| 35 | close(fd); |
| 36 | } |
Jordan Rose | ba129af | 2014-08-20 16:58:09 +0000 | [diff] [blame] | 37 | |
| 38 | void open_3(const char *path) { |
| 39 | int fd; |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 40 | fd = open(path, O_RDONLY, NULL); // expected-warning{{The 3rd argument to 'open' is not an integer}} |
Jordan Rose | ba129af | 2014-08-20 16:58:09 +0000 | [diff] [blame] | 41 | if (fd > -1) |
| 42 | close(fd); |
| 43 | } |
| 44 | |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 45 | void openat_3(int base_fd, const char *path) { |
| 46 | int fd; |
| 47 | fd = openat(base_fd, path, O_RDONLY, NULL); // expected-warning{{The 4th argument to 'openat' is not an integer}} |
| 48 | if (fd > -1) |
| 49 | close(fd); |
| 50 | } |
| 51 | |
| 52 | |
Jordan Rose | ba129af | 2014-08-20 16:58:09 +0000 | [diff] [blame] | 53 | void open_4(const char *path) { |
| 54 | int fd; |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 55 | fd = open(path, O_RDONLY, ""); // expected-warning{{The 3rd argument to 'open' is not an integer}} |
Jordan Rose | ba129af | 2014-08-20 16:58:09 +0000 | [diff] [blame] | 56 | if (fd > -1) |
| 57 | close(fd); |
| 58 | } |
| 59 | |
| 60 | void open_5(const char *path) { |
| 61 | int fd; |
| 62 | struct { |
| 63 | int val; |
| 64 | } st = {0}; |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 65 | fd = open(path, O_RDONLY, st); // expected-warning{{The 3rd argument to 'open' is not an integer}} |
Jordan Rose | ba129af | 2014-08-20 16:58:09 +0000 | [diff] [blame] | 66 | if (fd > -1) |
| 67 | close(fd); |
| 68 | } |
| 69 | |
| 70 | void open_6(const char *path) { |
| 71 | int fd; |
| 72 | struct { |
| 73 | int val; |
| 74 | } st = {0}; |
| 75 | fd = open(path, O_RDONLY, st.val); // no-warning |
| 76 | if (fd > -1) |
| 77 | close(fd); |
| 78 | } |
| 79 | |
| 80 | void open_7(const char *path) { |
| 81 | int fd; |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 82 | fd = open(path, O_RDONLY, &open); // expected-warning{{The 3rd argument to 'open' is not an integer}} |
Jordan Rose | ba129af | 2014-08-20 16:58:09 +0000 | [diff] [blame] | 83 | if (fd > -1) |
| 84 | close(fd); |
| 85 | } |
| 86 | |
| 87 | void open_8(const char *path) { |
| 88 | int fd; |
Devin Coughlin | 7481014 | 2016-12-16 23:31:56 +0000 | [diff] [blame] | 89 | fd = open(path, O_RDONLY, 0.0f); // expected-warning{{The 3rd argument to 'open' is not an integer}} |
Jordan Rose | ba129af | 2014-08-20 16:58:09 +0000 | [diff] [blame] | 90 | if (fd > -1) |
| 91 | close(fd); |
| 92 | } |