1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.API -verify %s
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc #ifndef O_RDONLY
4*0a6a1f1dSLionel Sambuc #define O_RDONLY 0
5*0a6a1f1dSLionel Sambuc #endif
6*0a6a1f1dSLionel Sambuc
7*0a6a1f1dSLionel Sambuc #ifndef NULL
8*0a6a1f1dSLionel Sambuc #define NULL ((void*) 0)
9*0a6a1f1dSLionel Sambuc #endif
10*0a6a1f1dSLionel Sambuc
11*0a6a1f1dSLionel Sambuc int open(const char *, int, ...);
12*0a6a1f1dSLionel Sambuc int close(int fildes);
13*0a6a1f1dSLionel Sambuc
open_1(const char * path)14*0a6a1f1dSLionel Sambuc void open_1(const char *path) {
15*0a6a1f1dSLionel Sambuc int fd;
16*0a6a1f1dSLionel Sambuc fd = open(path, O_RDONLY); // no-warning
17*0a6a1f1dSLionel Sambuc if (fd > -1)
18*0a6a1f1dSLionel Sambuc close(fd);
19*0a6a1f1dSLionel Sambuc }
20*0a6a1f1dSLionel Sambuc
open_2(const char * path)21*0a6a1f1dSLionel Sambuc void open_2(const char *path) {
22*0a6a1f1dSLionel Sambuc int fd;
23*0a6a1f1dSLionel Sambuc int mode = 0x0;
24*0a6a1f1dSLionel Sambuc fd = open(path, O_RDONLY, mode, NULL); // expected-warning{{Call to 'open' with more than three arguments}}
25*0a6a1f1dSLionel Sambuc if (fd > -1)
26*0a6a1f1dSLionel Sambuc close(fd);
27*0a6a1f1dSLionel Sambuc }
28*0a6a1f1dSLionel Sambuc
open_3(const char * path)29*0a6a1f1dSLionel Sambuc void open_3(const char *path) {
30*0a6a1f1dSLionel Sambuc int fd;
31*0a6a1f1dSLionel Sambuc fd = open(path, O_RDONLY, NULL); // expected-warning{{Third argument to 'open' is not an integer}}
32*0a6a1f1dSLionel Sambuc if (fd > -1)
33*0a6a1f1dSLionel Sambuc close(fd);
34*0a6a1f1dSLionel Sambuc }
35*0a6a1f1dSLionel Sambuc
open_4(const char * path)36*0a6a1f1dSLionel Sambuc void open_4(const char *path) {
37*0a6a1f1dSLionel Sambuc int fd;
38*0a6a1f1dSLionel Sambuc fd = open(path, O_RDONLY, ""); // expected-warning{{Third argument to 'open' is not an integer}}
39*0a6a1f1dSLionel Sambuc if (fd > -1)
40*0a6a1f1dSLionel Sambuc close(fd);
41*0a6a1f1dSLionel Sambuc }
42*0a6a1f1dSLionel Sambuc
open_5(const char * path)43*0a6a1f1dSLionel Sambuc void open_5(const char *path) {
44*0a6a1f1dSLionel Sambuc int fd;
45*0a6a1f1dSLionel Sambuc struct {
46*0a6a1f1dSLionel Sambuc int val;
47*0a6a1f1dSLionel Sambuc } st = {0};
48*0a6a1f1dSLionel Sambuc fd = open(path, O_RDONLY, st); // expected-warning{{Third argument to 'open' is not an integer}}
49*0a6a1f1dSLionel Sambuc if (fd > -1)
50*0a6a1f1dSLionel Sambuc close(fd);
51*0a6a1f1dSLionel Sambuc }
52*0a6a1f1dSLionel Sambuc
open_6(const char * path)53*0a6a1f1dSLionel Sambuc void open_6(const char *path) {
54*0a6a1f1dSLionel Sambuc int fd;
55*0a6a1f1dSLionel Sambuc struct {
56*0a6a1f1dSLionel Sambuc int val;
57*0a6a1f1dSLionel Sambuc } st = {0};
58*0a6a1f1dSLionel Sambuc fd = open(path, O_RDONLY, st.val); // no-warning
59*0a6a1f1dSLionel Sambuc if (fd > -1)
60*0a6a1f1dSLionel Sambuc close(fd);
61*0a6a1f1dSLionel Sambuc }
62*0a6a1f1dSLionel Sambuc
open_7(const char * path)63*0a6a1f1dSLionel Sambuc void open_7(const char *path) {
64*0a6a1f1dSLionel Sambuc int fd;
65*0a6a1f1dSLionel Sambuc fd = open(path, O_RDONLY, &open); // expected-warning{{Third argument to 'open' is not an integer}}
66*0a6a1f1dSLionel Sambuc if (fd > -1)
67*0a6a1f1dSLionel Sambuc close(fd);
68*0a6a1f1dSLionel Sambuc }
69*0a6a1f1dSLionel Sambuc
open_8(const char * path)70*0a6a1f1dSLionel Sambuc void open_8(const char *path) {
71*0a6a1f1dSLionel Sambuc int fd;
72*0a6a1f1dSLionel Sambuc fd = open(path, O_RDONLY, 0.0f); // expected-warning{{Third argument to 'open' is not an integer}}
73*0a6a1f1dSLionel Sambuc if (fd > -1)
74*0a6a1f1dSLionel Sambuc close(fd);
75*0a6a1f1dSLionel Sambuc }
76