xref: /llvm-project/clang/test/Analysis/unix-api.c (revision 184c6242faca0be0188611d45b41235e68ef282a)
1*184c6242SDominic Chen // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.API -verify %s
2cd4db5c6SJordan Rose 
3cd4db5c6SJordan Rose #ifndef O_RDONLY
4cd4db5c6SJordan Rose #define O_RDONLY 0
5cd4db5c6SJordan Rose #endif
6cd4db5c6SJordan Rose 
7cd4db5c6SJordan Rose #ifndef NULL
8cd4db5c6SJordan Rose #define NULL ((void*) 0)
9cd4db5c6SJordan Rose #endif
10cd4db5c6SJordan Rose 
11cd4db5c6SJordan Rose int open(const char *, int, ...);
1274810145SDevin Coughlin int openat(int, const char *, int, ...);
13cd4db5c6SJordan Rose int close(int fildes);
14cd4db5c6SJordan Rose 
open_1(const char * path)15cd4db5c6SJordan Rose void open_1(const char *path) {
16cd4db5c6SJordan Rose   int fd;
17cd4db5c6SJordan Rose   fd = open(path, O_RDONLY); // no-warning
18cd4db5c6SJordan Rose   if (fd > -1)
19cd4db5c6SJordan Rose     close(fd);
20cd4db5c6SJordan Rose }
21cd4db5c6SJordan Rose 
open_2(const char * path)22cd4db5c6SJordan Rose void open_2(const char *path) {
23cd4db5c6SJordan Rose   int fd;
24cd4db5c6SJordan Rose   int mode = 0x0;
2574810145SDevin Coughlin   fd = open(path, O_RDONLY, mode, NULL); // expected-warning{{Call to 'open' with more than 3 arguments}}
2674810145SDevin Coughlin   if (fd > -1)
2774810145SDevin Coughlin     close(fd);
2874810145SDevin Coughlin }
2974810145SDevin Coughlin 
openat_2(int base_fd,const char * path)3074810145SDevin Coughlin void openat_2(int base_fd, const char *path) {
3174810145SDevin Coughlin   int fd;
3274810145SDevin Coughlin   int mode = 0x0;
3374810145SDevin Coughlin   fd = openat(base_fd, path, O_RDONLY, mode, NULL); // expected-warning{{Call to 'openat' with more than 4 arguments}}
34cd4db5c6SJordan Rose   if (fd > -1)
35cd4db5c6SJordan Rose     close(fd);
36cd4db5c6SJordan Rose }
37ba129af6SJordan Rose 
open_3(const char * path)38ba129af6SJordan Rose void open_3(const char *path) {
39ba129af6SJordan Rose   int fd;
4074810145SDevin Coughlin   fd = open(path, O_RDONLY, NULL); // expected-warning{{The 3rd argument to 'open' is not an integer}}
41ba129af6SJordan Rose   if (fd > -1)
42ba129af6SJordan Rose     close(fd);
43ba129af6SJordan Rose }
44ba129af6SJordan Rose 
openat_3(int base_fd,const char * path)4574810145SDevin Coughlin void openat_3(int base_fd, const char *path) {
4674810145SDevin Coughlin   int fd;
4774810145SDevin Coughlin   fd = openat(base_fd, path, O_RDONLY, NULL); // expected-warning{{The 4th argument to 'openat' is not an integer}}
4874810145SDevin Coughlin   if (fd > -1)
4974810145SDevin Coughlin     close(fd);
5074810145SDevin Coughlin }
5174810145SDevin Coughlin 
5274810145SDevin Coughlin 
open_4(const char * path)53ba129af6SJordan Rose void open_4(const char *path) {
54ba129af6SJordan Rose   int fd;
5574810145SDevin Coughlin   fd = open(path, O_RDONLY, ""); // expected-warning{{The 3rd argument to 'open' is not an integer}}
56ba129af6SJordan Rose   if (fd > -1)
57ba129af6SJordan Rose     close(fd);
58ba129af6SJordan Rose }
59ba129af6SJordan Rose 
open_5(const char * path)60ba129af6SJordan Rose void open_5(const char *path) {
61ba129af6SJordan Rose   int fd;
62ba129af6SJordan Rose   struct {
63ba129af6SJordan Rose     int val;
64ba129af6SJordan Rose   } st = {0};
6574810145SDevin Coughlin   fd = open(path, O_RDONLY, st); // expected-warning{{The 3rd argument to 'open' is not an integer}}
66ba129af6SJordan Rose   if (fd > -1)
67ba129af6SJordan Rose     close(fd);
68ba129af6SJordan Rose }
69ba129af6SJordan Rose 
open_6(const char * path)70ba129af6SJordan Rose void open_6(const char *path) {
71ba129af6SJordan Rose   int fd;
72ba129af6SJordan Rose   struct {
73ba129af6SJordan Rose     int val;
74ba129af6SJordan Rose   } st = {0};
75ba129af6SJordan Rose   fd = open(path, O_RDONLY, st.val); // no-warning
76ba129af6SJordan Rose   if (fd > -1)
77ba129af6SJordan Rose     close(fd);
78ba129af6SJordan Rose }
79ba129af6SJordan Rose 
open_7(const char * path)80ba129af6SJordan Rose void open_7(const char *path) {
81ba129af6SJordan Rose   int fd;
8274810145SDevin Coughlin   fd = open(path, O_RDONLY, &open); // expected-warning{{The 3rd argument to 'open' is not an integer}}
83ba129af6SJordan Rose   if (fd > -1)
84ba129af6SJordan Rose     close(fd);
85ba129af6SJordan Rose }
86ba129af6SJordan Rose 
open_8(const char * path)87ba129af6SJordan Rose void open_8(const char *path) {
88ba129af6SJordan Rose   int fd;
8974810145SDevin Coughlin   fd = open(path, O_RDONLY, 0.0f); // expected-warning{{The 3rd argument to 'open' is not an integer}}
90ba129af6SJordan Rose   if (fd > -1)
91ba129af6SJordan Rose     close(fd);
92ba129af6SJordan Rose }
93