xref: /llvm-project/clang/test/Analysis/unix-fns-o_creat.c (revision 37c19f9a35c5adad009ad82c608b9ca11155ec06)
1 // RUN: %clang_analyze_cc1 -verify -analyzer-checker=core,unix.API -analyzer-output=text %s
2 
3 // Verify that the UnixAPIChecker finds the missing mode value regardless
4 // of the particular values of these macros, particularly O_CREAT.
5 #define O_RDONLY  0x2000
6 #define O_WRONLY  0x8000
7 #define O_CREAT   0x0002
8 
9 extern int open(const char *path, int flags, ...);
10 
missing_mode_1(const char * path)11 void missing_mode_1(const char *path) {
12   (void)open(path, O_CREAT); // expected-warning{{Call to 'open' requires a 3rd argument when the 'O_CREAT' flag is set}} \
13                                 expected-note{{Call to 'open' requires a 3rd argument when the 'O_CREAT' flag is set}}
14 }
15 
16 extern int some_flag;
17 
missing_mode_2(const char * path)18 void missing_mode_2(const char *path) {
19   int mode = O_WRONLY;
20   if (some_flag) { // expected-note {{Assuming 'some_flag' is not equal to 0}} \
21                      expected-note {{Taking true branch}}
22     mode |= O_CREAT;
23   }
24   (void)open(path, mode); // expected-warning{{Call to 'open' requires a 3rd argument when the 'O_CREAT' flag is set}} \
25                              expected-note{{Call to 'open' requires a 3rd argument when the 'O_CREAT' flag is set}}
26 }
27 
no_creat(const char * path)28 void no_creat(const char* path) {
29   int mode = O_RDONLY;
30   (void)open(path, mode); // ok
31 }
32 
mode_is_there(const char * path)33 void mode_is_there(const char *path) {
34   int mode = O_WRONLY;
35   if (some_flag) {
36     mode |= O_CREAT;
37   }
38   (void)open(path, mode, 0770); // ok
39 }
40