63 lines · cpp
1// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.API -verify %s2extern "C" {3#ifndef O_RDONLY4#define O_RDONLY 05#endif6 7#ifndef NULL8#define NULL ((void*) 0)9#endif10 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-warning23 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-warning38 39 using namespace MyNameSpace;40 41 open("Hi", 2, 3, 4); // no-warning42 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-warning59 60 MyClass mc;61 mc.open(1, 2, 3, 4); // no-warning62}63