Dominic Chen | 184c624 | 2017-03-03 18:02:02 +0000 | [diff] [blame] | 1 | // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.API -verify %s |
Devin Coughlin | aa0fd76 | 2016-12-17 01:08:17 +0000 | [diff] [blame] | 2 | extern "C" { |
| 3 | #ifndef O_RDONLY |
| 4 | #define O_RDONLY 0 |
| 5 | #endif |
| 6 | |
| 7 | #ifndef NULL |
| 8 | #define NULL ((void*) 0) |
| 9 | #endif |
| 10 | |
| 11 | int open(const char *, int, ...); |
| 12 | int close(int fildes); |
| 13 | |
| 14 | } // extern "C" |
| 15 | |
| 16 | namespace MyNameSpace { |
| 17 | int open(const char *a, int b, int c, int d); |
| 18 | } |
| 19 | |
| 20 | void 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 | |
| 27 | void 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() |
| 36 | void 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 | |
| 50 | class MyClass { |
| 51 | public: |
| 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 | |
| 57 | void 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 | } |