Clang Project

clang_source_code/test/Analysis/unix-api.cpp
1// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.API -verify %s
2extern "C" {
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 close(int fildes);
13
14} // extern "C"
15
16namespace MyNameSpace {
17int open(const char *a, int b, int c, int d);
18}
19
20void unix_open(const char *path) {
21  int fd;
22  fd = open(path, O_RDONLY); // no-warning
23  if (fd > -1)
24    close(fd);
25}
26
27void unix_open_misuse(const char *path) {
28  int fd;
29  int mode = 0x0;
30  fd = open(path, O_RDONLY, mode, NULL); // expected-warning{{Call to 'open' with more than 3 arguments}}
31  if (fd > -1)
32    close(fd);
33}
34
35// Don't treat open() in namespaces as the POSIX open()
36void namespaced_open(const char *path) {
37  MyNameSpace::open("Hi", 2, 3, 4); // no-warning
38
39  using namespace MyNameSpace;
40
41  open("Hi", 2, 3, 4); // no-warning
42
43  int fd;
44  int mode = 0x0;
45  fd = ::open(path, O_RDONLY, mode, NULL); // expected-warning{{Call to 'open' with more than 3 arguments}}
46  if (fd > -1)
47    close(fd);
48}
49
50class MyClass {
51public:
52  static int open(const char *a, int b, int c, int d);
53
54  int open(int a, int, int c, int d);
55};
56
57void class_qualified_open() {
58  MyClass::open("Hi", 2, 3, 4); // no-warning
59
60  MyClass mc;
61  mc.open(1, 2, 3, 4); // no-warning
62}
63