Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Analysis / std-c-library-functions-POSIX.c
blob84ce0f21e569fb58ddb10917bc408438fd98666d
1 // RUN: %clang_analyze_cc1 %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=unix.StdCLibraryFunctions \
4 // RUN: -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true \
5 // RUN: -analyzer-config unix.StdCLibraryFunctions:DisplayLoadedSummaries=true \
6 // RUN: -analyzer-checker=debug.ExprInspection \
7 // RUN: -analyzer-config eagerly-assume=false \
8 // RUN: -triple i686-unknown-linux -verify
10 // RUN: %clang_analyze_cc1 %s \
11 // RUN: -analyzer-checker=core \
12 // RUN: -analyzer-checker=unix.StdCLibraryFunctions \
13 // RUN: -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true \
14 // RUN: -analyzer-config unix.StdCLibraryFunctions:DisplayLoadedSummaries=true \
15 // RUN: -analyzer-checker=debug.ExprInspection \
16 // RUN: -analyzer-config eagerly-assume=false \
17 // RUN: -triple i686-unknown-linux 2>&1 | FileCheck %s
19 // CHECK: Loaded summary for: FILE *fopen(const char *restrict pathname, const char *restrict mode)
20 // CHECK: Loaded summary for: FILE *tmpfile(void)
21 // CHECK: Loaded summary for: FILE *freopen(const char *restrict pathname, const char *restrict mode, FILE *restrict stream)
22 // CHECK: Loaded summary for: int fclose(FILE *stream)
23 // CHECK: Loaded summary for: int fseek(FILE *stream, long offset, int whence)
24 // CHECK: Loaded summary for: int fileno(FILE *stream)
25 // CHECK: Loaded summary for: long a64l(const char *str64)
26 // CHECK: Loaded summary for: char *l64a(long value)
27 // CHECK: Loaded summary for: int open(const char *path, int oflag, ...)
28 // CHECK: Loaded summary for: int openat(int fd, const char *path, int oflag, ...)
29 // CHECK: Loaded summary for: int access(const char *pathname, int amode)
30 // CHECK: Loaded summary for: int faccessat(int dirfd, const char *pathname, int mode, int flags)
31 // CHECK: Loaded summary for: int dup(int fildes)
32 // CHECK: Loaded summary for: int dup2(int fildes1, int filedes2)
33 // CHECK: Loaded summary for: int fdatasync(int fildes)
34 // CHECK: Loaded summary for: int fnmatch(const char *pattern, const char *string, int flags)
35 // CHECK: Loaded summary for: int fsync(int fildes)
36 // CHECK: Loaded summary for: int truncate(const char *path, off_t length)
37 // CHECK: Loaded summary for: int symlink(const char *oldpath, const char *newpath)
38 // CHECK: Loaded summary for: int symlinkat(const char *oldpath, int newdirfd, const char *newpath)
39 // CHECK: Loaded summary for: int lockf(int fd, int cmd, off_t len)
40 // CHECK: Loaded summary for: int creat(const char *pathname, mode_t mode)
41 // CHECK: Loaded summary for: unsigned int sleep(unsigned int seconds)
42 // CHECK: Loaded summary for: int dirfd(DIR *dirp)
43 // CHECK: Loaded summary for: unsigned int alarm(unsigned int seconds)
44 // CHECK: Loaded summary for: int closedir(DIR *dir)
45 // CHECK: Loaded summary for: char *strdup(const char *s)
46 // CHECK: Loaded summary for: char *strndup(const char *s, size_t n)
47 // CHECK: Loaded summary for: int mkstemp(char *template)
48 // CHECK: Loaded summary for: char *mkdtemp(char *template)
49 // CHECK: Loaded summary for: char *getcwd(char *buf, size_t size)
50 // CHECK: Loaded summary for: int mkdir(const char *pathname, mode_t mode)
51 // CHECK: Loaded summary for: int mkdirat(int dirfd, const char *pathname, mode_t mode)
52 // CHECK: Loaded summary for: int mknod(const char *pathname, mode_t mode, dev_t dev)
53 // CHECK: Loaded summary for: int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev)
54 // CHECK: Loaded summary for: int chmod(const char *path, mode_t mode)
55 // CHECK: Loaded summary for: int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags)
56 // CHECK: Loaded summary for: int fchmod(int fildes, mode_t mode)
57 // CHECK: Loaded summary for: int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags)
58 // CHECK: Loaded summary for: int chown(const char *path, uid_t owner, gid_t group)
59 // CHECK: Loaded summary for: int lchown(const char *path, uid_t owner, gid_t group)
60 // CHECK: Loaded summary for: int fchown(int fildes, uid_t owner, gid_t group)
61 // CHECK: Loaded summary for: int rmdir(const char *pathname)
62 // CHECK: Loaded summary for: int chdir(const char *path)
63 // CHECK: Loaded summary for: int link(const char *oldpath, const char *newpath)
64 // CHECK: Loaded summary for: int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag)
65 // CHECK: Loaded summary for: int unlink(const char *pathname)
66 // CHECK: Loaded summary for: int unlinkat(int fd, const char *path, int flag)
67 // CHECK: Loaded summary for: int fstat(int fd, struct stat *statbuf)
68 // CHECK: Loaded summary for: int stat(const char *restrict path, struct stat *restrict buf)
69 // CHECK: Loaded summary for: int lstat(const char *restrict path, struct stat *restrict buf)
70 // CHECK: Loaded summary for: int fstatat(int fd, const char *restrict path, struct stat *restrict buf, int flag)
71 // CHECK: Loaded summary for: DIR *opendir(const char *name)
72 // CHECK: Loaded summary for: DIR *fdopendir(int fd)
73 // CHECK: Loaded summary for: int isatty(int fildes)
74 // CHECK: Loaded summary for: FILE *popen(const char *command, const char *type)
75 // CHECK: Loaded summary for: int pclose(FILE *stream)
76 // CHECK: Loaded summary for: int close(int fildes)
77 // CHECK: Loaded summary for: long fpathconf(int fildes, int name)
78 // CHECK: Loaded summary for: long pathconf(const char *path, int name)
79 // CHECK: Loaded summary for: FILE *fdopen(int fd, const char *mode)
80 // CHECK: Loaded summary for: void rewinddir(DIR *dir)
81 // CHECK: Loaded summary for: void seekdir(DIR *dirp, long loc)
82 // CHECK: Loaded summary for: int rand_r(unsigned int *seedp)
83 // CHECK: Loaded summary for: int fseeko(FILE *stream, off_t offset, int whence)
84 // CHECK: Loaded summary for: off_t ftello(FILE *stream)
85 // CHECK: Loaded summary for: void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
86 // CHECK: Loaded summary for: void *mmap64(void *addr, size_t length, int prot, int flags, int fd, off64_t offset)
87 // CHECK: Loaded summary for: int pipe(int fildes[2])
88 // CHECK: Loaded summary for: off_t lseek(int fildes, off_t offset, int whence)
89 // CHECK: Loaded summary for: ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize)
90 // CHECK: Loaded summary for: ssize_t readlinkat(int fd, const char *restrict path, char *restrict buf, size_t bufsize)
91 // CHECK: Loaded summary for: int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath)
92 // CHECK: Loaded summary for: char *realpath(const char *restrict file_name, char *restrict resolved_name)
93 // CHECK: Loaded summary for: int execv(const char *path, char *const argv[])
94 // CHECK: Loaded summary for: int execvp(const char *file, char *const argv[])
95 // CHECK: Loaded summary for: int getopt(int argc, char *const argv[], const char *optstring)
96 // CHECK: Loaded summary for: int socket(int domain, int type, int protocol)
97 // CHECK: Loaded summary for: int accept(int socket, __SOCKADDR_ARG address, socklen_t *restrict address_len)
98 // CHECK: Loaded summary for: int bind(int socket, __CONST_SOCKADDR_ARG address, socklen_t address_len)
99 // CHECK: Loaded summary for: int getpeername(int socket, __SOCKADDR_ARG address, socklen_t *restrict address_len)
100 // CHECK: Loaded summary for: int getsockname(int socket, __SOCKADDR_ARG address, socklen_t *restrict address_len)
101 // CHECK: Loaded summary for: int connect(int socket, __CONST_SOCKADDR_ARG address, socklen_t address_len)
102 // CHECK: Loaded summary for: ssize_t recvfrom(int socket, void *restrict buffer, size_t length, int flags, __SOCKADDR_ARG address, socklen_t *restrict address_len)
103 // CHECK: Loaded summary for: ssize_t sendto(int socket, const void *message, size_t length, int flags, __CONST_SOCKADDR_ARG dest_addr, socklen_t dest_len)
104 // CHECK: Loaded summary for: int listen(int sockfd, int backlog)
105 // CHECK: Loaded summary for: ssize_t recv(int sockfd, void *buf, size_t len, int flags)
106 // CHECK: Loaded summary for: ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
107 // CHECK: Loaded summary for: ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags)
108 // CHECK: Loaded summary for: int setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len)
109 // CHECK: Loaded summary for: int getsockopt(int socket, int level, int option_name, void *restrict option_value, socklen_t *restrict option_len)
110 // CHECK: Loaded summary for: ssize_t send(int sockfd, const void *buf, size_t len, int flags)
111 // CHECK: Loaded summary for: int socketpair(int domain, int type, int protocol, int sv[2])
112 // CHECK: Loaded summary for: int shutdown(int socket, int how)
113 // CHECK: Loaded summary for: int getnameinfo(const struct sockaddr *restrict sa, socklen_t salen, char *restrict node, socklen_t nodelen, char *restrict service, socklen_t servicelen, int flags)
114 // CHECK: Loaded summary for: int utime(const char *filename, struct utimbuf *buf)
115 // CHECK: Loaded summary for: int futimens(int fd, const struct timespec times[2])
116 // CHECK: Loaded summary for: int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags)
117 // CHECK: Loaded summary for: int utimes(const char *filename, const struct timeval times[2])
118 // CHECK: Loaded summary for: int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
119 // CHECK: Loaded summary for: struct tm *localtime(const time_t *tp)
120 // CHECK: Loaded summary for: struct tm *localtime_r(const time_t *restrict timer, struct tm *restrict result)
121 // CHECK: Loaded summary for: char *asctime_r(const struct tm *restrict tm, char *restrict buf)
122 // CHECK: Loaded summary for: char *ctime_r(const time_t *timep, char *buf)
123 // CHECK: Loaded summary for: struct tm *gmtime_r(const time_t *restrict timer, struct tm *restrict result)
124 // CHECK: Loaded summary for: struct tm *gmtime(const time_t *tp)
125 // CHECK: Loaded summary for: int clock_gettime(clockid_t clock_id, struct timespec *tp)
126 // CHECK: Loaded summary for: int getitimer(int which, struct itimerval *curr_value)
127 // CHECK: Loaded summary for: int pthread_cond_signal(pthread_cond_t *cond)
128 // CHECK: Loaded summary for: int pthread_cond_broadcast(pthread_cond_t *cond)
129 // CHECK: Loaded summary for: int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg)
130 // CHECK: Loaded summary for: int pthread_attr_destroy(pthread_attr_t *attr)
131 // CHECK: Loaded summary for: int pthread_attr_init(pthread_attr_t *attr)
132 // CHECK: Loaded summary for: int pthread_attr_getstacksize(const pthread_attr_t *restrict attr, size_t *restrict stacksize)
133 // CHECK: Loaded summary for: int pthread_attr_getguardsize(const pthread_attr_t *restrict attr, size_t *restrict guardsize)
134 // CHECK: Loaded summary for: int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
135 // CHECK: Loaded summary for: int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
136 // CHECK: Loaded summary for: int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr)
137 // CHECK: Loaded summary for: int pthread_mutex_destroy(pthread_mutex_t *mutex)
138 // CHECK: Loaded summary for: int pthread_mutex_lock(pthread_mutex_t *mutex)
139 // CHECK: Loaded summary for: int pthread_mutex_trylock(pthread_mutex_t *mutex)
140 // CHECK: Loaded summary for: int pthread_mutex_unlock(pthread_mutex_t *mutex)
142 #include "Inputs/std-c-library-functions-POSIX.h"
144 void clang_analyzer_eval(int);
146 void test_open(void) {
147 open(0, 0); // \
148 // expected-warning{{The 1st argument to 'open' is NULL but should not be NULL}}
151 void test_open_additional_arg(void) {
152 open(0, 0, 0); // \
153 // expected-warning{{The 1st argument to 'open' is NULL but should not be NULL}}
156 void test_recvfrom(int socket, void *restrict buffer, size_t length, int flags,
157 struct sockaddr *restrict address,
158 socklen_t *restrict address_len) {
159 ssize_t Ret = recvfrom(socket, buffer, length, flags, address, address_len);
160 if (Ret == 0)
161 clang_analyzer_eval(length == 0); // expected-warning{{TRUE}}
162 if (Ret > 0)
163 clang_analyzer_eval(length > 0); // expected-warning{{TRUE}}
164 if (Ret == -1)
165 clang_analyzer_eval(length == 0); // expected-warning{{UNKNOWN}}
168 void test_sendto(int socket, const void *message, size_t length, int flags,
169 const struct sockaddr *dest_addr, socklen_t dest_len) {
170 ssize_t Ret = sendto(socket, message, length, flags, dest_addr, dest_len);
171 if (Ret == 0)
172 clang_analyzer_eval(length == 0); // expected-warning{{TRUE}}
173 if (Ret > 0)
174 clang_analyzer_eval(length > 0); // expected-warning{{TRUE}}
175 if (Ret == -1)
176 clang_analyzer_eval(length == 0); // expected-warning{{UNKNOWN}}
179 void test_recv(int sockfd, void *buf, size_t len, int flags) {
180 ssize_t Ret = recv(sockfd, buf, len, flags);
181 if (Ret == 0)
182 clang_analyzer_eval(len == 0); // expected-warning{{TRUE}}
183 if (Ret > 0)
184 clang_analyzer_eval(len > 0); // expected-warning{{TRUE}}
185 if (Ret == -1)
186 clang_analyzer_eval(len == 0); // expected-warning{{UNKNOWN}}
189 void test_send(int sockfd, void *buf, size_t len, int flags) {
190 ssize_t Ret = send(sockfd, buf, len, flags);
191 if (Ret == 0)
192 clang_analyzer_eval(len == 0); // expected-warning{{TRUE}}
193 if (Ret > 0)
194 clang_analyzer_eval(len > 0); // expected-warning{{TRUE}}
195 if (Ret == -1)
196 clang_analyzer_eval(len == 0); // expected-warning{{UNKNOWN}}
199 void test_recvmsg(int sockfd, struct msghdr *msg, int flags) {
200 ssize_t Ret = recvmsg(sockfd, msg, flags);
201 clang_analyzer_eval(Ret != 0); // expected-warning{{TRUE}}
204 void test_sendmsg(int sockfd, const struct msghdr *msg, int flags) {
205 ssize_t Ret = sendmsg(sockfd, msg, flags);
206 clang_analyzer_eval(Ret != 0); // expected-warning{{TRUE}}