Clang Project

clang_source_code/test/Analysis/unix-api.c
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
11int open(const char *, int, ...);
12int openat(int, const char *, int, ...);
13int 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;
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
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}}
34  if (fd > -1)
35    close(fd);
36}
37
38void 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
45void 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
53void 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
60void 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
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;
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
87void 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