blob: 64ff3c0fccf426799cb8e4fc37aea39f376600b9 [file] [log] [blame]
Dominic Chen184c6242017-03-03 18:02:02 +00001// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.API -verify %s
Jordan Rosecd4db5c2014-08-20 16:58:03 +00002
3#ifndef O_RDONLY
4#define O_RDONLY 0
5#endif
6
7#ifndef NULL
8#define NULL ((void*) 0)
9#endif
10
11int open(const char *, int, ...);
Devin Coughlin74810142016-12-16 23:31:56 +000012int openat(int, const char *, int, ...);
Jordan Rosecd4db5c2014-08-20 16:58:03 +000013int close(int fildes);
14
15void 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
22void open_2(const char *path) {
23 int fd;
24 int mode = 0x0;
Devin Coughlin74810142016-12-16 23:31:56 +000025 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
30void 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 Rosecd4db5c2014-08-20 16:58:03 +000034 if (fd > -1)
35 close(fd);
36}
Jordan Roseba129af2014-08-20 16:58:09 +000037
38void open_3(const char *path) {
39 int fd;
Devin Coughlin74810142016-12-16 23:31:56 +000040 fd = open(path, O_RDONLY, NULL); // expected-warning{{The 3rd argument to 'open' is not an integer}}
Jordan Roseba129af2014-08-20 16:58:09 +000041 if (fd > -1)
42 close(fd);
43}
44
Devin Coughlin74810142016-12-16 23:31:56 +000045void 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 Roseba129af2014-08-20 16:58:09 +000053void open_4(const char *path) {
54 int fd;
Devin Coughlin74810142016-12-16 23:31:56 +000055 fd = open(path, O_RDONLY, ""); // expected-warning{{The 3rd argument to 'open' is not an integer}}
Jordan Roseba129af2014-08-20 16:58:09 +000056 if (fd > -1)
57 close(fd);
58}
59
60void open_5(const char *path) {
61 int fd;
62 struct {
63 int val;
64 } st = {0};
Devin Coughlin74810142016-12-16 23:31:56 +000065 fd = open(path, O_RDONLY, st); // expected-warning{{The 3rd argument to 'open' is not an integer}}
Jordan Roseba129af2014-08-20 16:58:09 +000066 if (fd > -1)
67 close(fd);
68}
69
70void 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
80void open_7(const char *path) {
81 int fd;
Devin Coughlin74810142016-12-16 23:31:56 +000082 fd = open(path, O_RDONLY, &open); // expected-warning{{The 3rd argument to 'open' is not an integer}}
Jordan Roseba129af2014-08-20 16:58:09 +000083 if (fd > -1)
84 close(fd);
85}
86
87void open_8(const char *path) {
88 int fd;
Devin Coughlin74810142016-12-16 23:31:56 +000089 fd = open(path, O_RDONLY, 0.0f); // expected-warning{{The 3rd argument to 'open' is not an integer}}
Jordan Roseba129af2014-08-20 16:58:09 +000090 if (fd > -1)
91 close(fd);
92}