1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.API -verify %s
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, ...);
12 int openat(int, const char *, int, ...);
13 int close(int fildes);
14 
open_1(const char * path)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 
open_2(const char * path)22 void open_2(const char *path) {
23   int fd;
24   int mode = 0x0;
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 
openat_2(int base_fd,const char * path)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}}
34   if (fd > -1)
35     close(fd);
36 }
37 
open_3(const char * path)38 void open_3(const char *path) {
39   int fd;
40   fd = open(path, O_RDONLY, NULL); // expected-warning{{The 3rd argument to 'open' is not an integer}}
41   if (fd > -1)
42     close(fd);
43 }
44 
openat_3(int base_fd,const char * path)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 
open_4(const char * path)53 void open_4(const char *path) {
54   int fd;
55   fd = open(path, O_RDONLY, ""); // expected-warning{{The 3rd argument to 'open' is not an integer}}
56   if (fd > -1)
57     close(fd);
58 }
59 
open_5(const char * path)60 void open_5(const char *path) {
61   int fd;
62   struct {
63     int val;
64   } st = {0};
65   fd = open(path, O_RDONLY, st); // expected-warning{{The 3rd argument to 'open' is not an integer}}
66   if (fd > -1)
67     close(fd);
68 }
69 
open_6(const char * path)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 
open_7(const char * path)80 void open_7(const char *path) {
81   int fd;
82   fd = open(path, O_RDONLY, &open); // expected-warning{{The 3rd argument to 'open' is not an integer}}
83   if (fd > -1)
84     close(fd);
85 }
86 
open_8(const char * path)87 void open_8(const char *path) {
88   int fd;
89   fd = open(path, O_RDONLY, 0.0f); // expected-warning{{The 3rd argument to 'open' is not an integer}}
90   if (fd > -1)
91     close(fd);
92 }
93