2015-01-30 07:52:32 +01:00
|
|
|
|
|
|
|
// Test library configuration for posix.cfg
|
|
|
|
//
|
|
|
|
// Usage:
|
2015-02-16 22:19:51 +01:00
|
|
|
// $ cppcheck --check-library --library=posix --enable=information --error-exitcode=1 --inline-suppr --suppress=missingIncludeSystem test/cfg/posix.c
|
2015-01-30 07:52:32 +01:00
|
|
|
// =>
|
|
|
|
// No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0
|
|
|
|
//
|
|
|
|
|
2015-03-08 15:21:50 +01:00
|
|
|
#include <stdlib.h>
|
2015-02-15 18:11:09 +01:00
|
|
|
#include <stdio.h> // <- FILE
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/socket.h>
|
2019-01-10 21:14:37 +01:00
|
|
|
#include <dlfcn.h>
|
2015-02-15 18:11:09 +01:00
|
|
|
#include <fcntl.h>
|
2015-03-15 13:28:16 +01:00
|
|
|
// unavailable on some linux systems #include <ndbm.h>
|
2015-02-21 11:08:58 +01:00
|
|
|
#include <netdb.h>
|
2015-02-20 20:50:20 +01:00
|
|
|
#include <regex.h>
|
2015-03-11 18:59:34 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2018-07-21 17:52:12 +02:00
|
|
|
#include <pthread.h>
|
2015-02-15 18:11:09 +01:00
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void bufferAccessOutOfBounds(int fd)
|
|
|
|
{
|
|
|
|
char a[5];
|
|
|
|
read(fd,a,5);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
read(fd,a,6);
|
|
|
|
write(fd,a,5);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
write(fd,a,6);
|
|
|
|
recv(fd,a,5,0);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
recv(fd,a,6,0);
|
|
|
|
recvfrom(fd,a,5,0,0x0,0x0);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
recvfrom(fd,a,6,0,0x0,0x0);
|
|
|
|
send(fd,a,5,0);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
send(fd,a,6,0);
|
|
|
|
sendto(fd,a,5,0,0x0,0x0);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
sendto(fd,a,6,0,0x0,0x0);
|
|
|
|
// cppcheck-suppress constStatement
|
|
|
|
0;
|
2018-05-09 18:20:45 +02:00
|
|
|
readlink("path", a, 5);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
readlink("path", a, 6);
|
|
|
|
readlinkat(1, "path", a, 5);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
readlinkat(1, "path", a, 6);
|
2018-07-18 09:40:06 +02:00
|
|
|
// This is valid
|
|
|
|
gethostname(a, 5);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
gethostname(a, 6);
|
2015-02-10 17:29:36 +01:00
|
|
|
}
|
|
|
|
|
2018-07-21 17:52:12 +02:00
|
|
|
void nullPointer(char *p, int fd, pthread_mutex_t mutex)
|
2015-08-14 01:36:44 +02:00
|
|
|
{
|
2015-03-13 18:29:46 +01:00
|
|
|
// cppcheck-suppress ignoredReturnValue
|
2015-08-14 01:36:44 +02:00
|
|
|
isatty(0);
|
|
|
|
mkdir(p, 0);
|
|
|
|
getcwd(0, 0);
|
2015-01-30 07:52:32 +01:00
|
|
|
// cppcheck-suppress nullPointer
|
2015-11-22 17:52:47 +01:00
|
|
|
// cppcheck-suppress readdirCalled
|
2015-08-14 01:36:44 +02:00
|
|
|
readdir(0);
|
2015-03-08 14:43:25 +01:00
|
|
|
// cppcheck-suppress nullPointer
|
2015-11-22 17:52:47 +01:00
|
|
|
// cppcheck-suppress utimeCalled
|
2015-03-08 14:43:25 +01:00
|
|
|
utime(NULL, NULL);
|
2017-04-29 17:57:16 +02:00
|
|
|
// not implemented yet: cppcheck-suppress nullPointer
|
2017-04-26 14:25:14 +02:00
|
|
|
read(fd,NULL,42);
|
2017-04-29 17:57:16 +02:00
|
|
|
read(fd,NULL,0);
|
|
|
|
// not implemented yet: cppcheck-suppress nullPointer
|
2017-04-26 14:25:14 +02:00
|
|
|
write(fd,NULL,42);
|
2017-04-29 17:57:16 +02:00
|
|
|
write(fd,NULL,0);
|
2018-01-18 13:56:36 +01:00
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
open(NULL, 0);
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
open(NULL, 0, 0);
|
2018-01-22 14:54:14 +01:00
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
int ret = access(NULL, 0);
|
2018-01-29 11:19:31 +01:00
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
fdopen(fd, NULL);
|
2018-01-31 10:58:30 +01:00
|
|
|
// cppcheck-suppress strtokCalled
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strtok(p, NULL);
|
2018-07-21 17:52:12 +02:00
|
|
|
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
pthread_mutex_init(NULL, NULL);
|
|
|
|
// Second argument can be NULL
|
|
|
|
pthread_mutex_init(&mutex, NULL);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
pthread_mutex_destroy(NULL);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
pthread_mutex_lock(NULL);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
pthread_mutex_trylock(NULL);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
pthread_mutex_unlock(NULL);
|
2015-01-30 07:52:32 +01:00
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void memleak_getaddrinfo()
|
|
|
|
{
|
|
|
|
//TODO: nothing to report yet, see http://sourceforge.net/p/cppcheck/discussion/general/thread/d9737d5d/
|
|
|
|
struct addrinfo * res=NULL;
|
|
|
|
getaddrinfo("node", NULL, NULL, &res);
|
|
|
|
freeaddrinfo(res);
|
2015-02-21 11:08:58 +01:00
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void memleak_mmap(int fd)
|
|
|
|
{
|
2015-02-16 22:19:51 +01:00
|
|
|
// cppcheck-suppress unreadVariable
|
2015-02-15 18:11:09 +01:00
|
|
|
void *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
2018-01-30 08:43:15 +01:00
|
|
|
void resourceLeak_fdopen(int fd)
|
|
|
|
{
|
2018-01-29 11:19:31 +01:00
|
|
|
// cppcheck-suppress unreadVariable
|
2015-02-15 18:11:09 +01:00
|
|
|
FILE *f = fdopen(fd, "r");
|
|
|
|
// cppcheck-suppress resourceLeak
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void resourceLeak_fdopendir(int fd)
|
|
|
|
{
|
2015-02-16 22:19:51 +01:00
|
|
|
// cppcheck-suppress unreadVariable
|
2015-02-15 18:11:09 +01:00
|
|
|
DIR* leak1 = fdopendir(fd);
|
|
|
|
// cppcheck-suppress resourceLeak
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void resourceLeak_opendir(void)
|
|
|
|
{
|
2015-02-16 22:19:51 +01:00
|
|
|
// cppcheck-suppress unreadVariable
|
2015-02-15 18:11:09 +01:00
|
|
|
DIR* leak1 = opendir("abc");
|
|
|
|
// cppcheck-suppress resourceLeak
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void resourceLeak_socket(void)
|
|
|
|
{
|
2015-02-16 22:19:51 +01:00
|
|
|
// cppcheck-suppress unreadVariable
|
2015-02-15 18:11:09 +01:00
|
|
|
int s = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
// cppcheck-suppress resourceLeak
|
|
|
|
}
|
|
|
|
|
2018-01-18 13:56:36 +01:00
|
|
|
void resourceLeak_open1(void)
|
|
|
|
{
|
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
int fd = open("file", O_RDWR | O_CREAT);
|
2018-01-19 23:26:28 +01:00
|
|
|
// cppcheck-suppress resourceLeak
|
2018-01-18 13:56:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void resourceLeak_open2(void)
|
|
|
|
{
|
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
int fd = open("file", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
|
2018-01-19 23:26:28 +01:00
|
|
|
// cppcheck-suppress resourceLeak
|
2018-01-18 13:56:36 +01:00
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void noleak(int x, int y, int z)
|
|
|
|
{
|
|
|
|
DIR *p1 = fdopendir(x);
|
|
|
|
closedir(p1);
|
|
|
|
DIR *p2 = opendir("abc");
|
|
|
|
closedir(p2);
|
|
|
|
int s = socket(AF_INET,SOCK_STREAM,0);
|
|
|
|
close(s);
|
2018-01-18 13:56:36 +01:00
|
|
|
int fd1 = open("a", O_RDWR | O_CREAT);
|
|
|
|
close(fd1);
|
|
|
|
int fd2 = open("a", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
|
|
|
|
close(fd2);
|
2015-08-14 01:36:44 +02:00
|
|
|
/* TODO: add configuration for open/fdopen
|
|
|
|
// #2830
|
|
|
|
int fd = open("path", O_RDONLY);
|
|
|
|
FILE *f = fdopen(fd, "rt");
|
|
|
|
fclose(f);
|
|
|
|
*/
|
2015-02-15 18:11:09 +01:00
|
|
|
}
|
2015-02-16 16:25:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
// unused return value
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void ignoredReturnValue(void *addr, int fd)
|
|
|
|
{
|
2015-02-16 22:19:51 +01:00
|
|
|
// cppcheck-suppress ignoredReturnValue
|
2015-02-16 16:25:27 +01:00
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
mmap(addr, 255, PROT_NONE, MAP_PRIVATE, fd, 0);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
2015-02-16 22:19:51 +01:00
|
|
|
setuid(42);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
getuid();
|
2018-01-22 14:54:14 +01:00
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
access("filename", 1);
|
2015-02-16 16:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// valid range
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void invalidFunctionArg()
|
|
|
|
{
|
2015-02-16 16:25:27 +01:00
|
|
|
// cppcheck-suppress invalidFunctionArg
|
2015-11-22 17:52:47 +01:00
|
|
|
// cppcheck-suppress usleepCalled
|
2015-02-16 16:25:27 +01:00
|
|
|
usleep(-1);
|
2015-11-22 17:52:47 +01:00
|
|
|
// cppcheck-suppress usleepCalled
|
2015-02-16 16:25:27 +01:00
|
|
|
usleep(0);
|
2015-11-22 17:52:47 +01:00
|
|
|
// cppcheck-suppress usleepCalled
|
2015-02-16 16:25:27 +01:00
|
|
|
usleep(999999);
|
|
|
|
// cppcheck-suppress invalidFunctionArg
|
2015-11-22 17:52:47 +01:00
|
|
|
// cppcheck-suppress usleepCalled
|
2015-02-16 16:25:27 +01:00
|
|
|
usleep(1000000);
|
|
|
|
}
|
2015-02-17 18:08:19 +01:00
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninitvar(int fd)
|
|
|
|
{
|
2019-03-02 19:38:11 +01:00
|
|
|
int x1, x2, x3, x4;
|
2015-02-17 18:08:19 +01:00
|
|
|
char buf[2];
|
2015-08-14 01:36:44 +02:00
|
|
|
int decimal, sign;
|
2015-03-08 15:21:50 +01:00
|
|
|
double d;
|
2015-09-10 11:50:47 +02:00
|
|
|
void *p;
|
2019-03-02 19:38:11 +01:00
|
|
|
pthread_mutex_t mutex, mutex1, mutex2, mutex3;
|
2015-02-17 18:08:19 +01:00
|
|
|
// cppcheck-suppress uninitvar
|
2019-03-02 19:38:11 +01:00
|
|
|
write(x1,"ab",2);
|
2018-01-10 15:52:06 +01:00
|
|
|
// TODO cppcheck-suppress uninitvar
|
2015-09-10 13:23:34 +02:00
|
|
|
write(fd,buf,2); // #6325
|
2015-02-17 18:08:19 +01:00
|
|
|
// cppcheck-suppress uninitvar
|
2019-03-02 19:38:11 +01:00
|
|
|
write(fd,"ab",x2);
|
2015-09-10 11:50:47 +02:00
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
write(fd,p,2);
|
2015-08-14 01:36:44 +02:00
|
|
|
|
|
|
|
|
2015-02-20 20:50:20 +01:00
|
|
|
/* int regcomp(regex_t *restrict preg, const char *restrict pattern, int cflags); */
|
|
|
|
regex_t reg;
|
|
|
|
const char * pattern;
|
2019-03-02 19:38:11 +01:00
|
|
|
int cflags1, cflags2;
|
2015-02-20 20:50:20 +01:00
|
|
|
// cppcheck-suppress uninitvar
|
2019-03-02 19:38:11 +01:00
|
|
|
regcomp(®, pattern, cflags1);
|
2015-02-20 20:50:20 +01:00
|
|
|
pattern="";
|
|
|
|
// cppcheck-suppress uninitvar
|
2019-03-02 19:38:11 +01:00
|
|
|
regcomp(®, pattern, cflags2);
|
2015-08-14 01:36:44 +02:00
|
|
|
regerror(0, ®, 0, 0);
|
2018-02-22 12:27:46 +01:00
|
|
|
#ifndef __CYGWIN__
|
2015-03-01 20:18:26 +01:00
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
// cppcheck-suppress unreadVariable
|
2015-11-22 17:52:47 +01:00
|
|
|
// cppcheck-suppress ecvtCalled
|
2015-03-01 20:18:26 +01:00
|
|
|
char *buffer = ecvt(d, 11, &decimal, &sign);
|
2018-02-22 12:27:46 +01:00
|
|
|
#endif
|
2015-11-22 17:52:47 +01:00
|
|
|
// cppcheck-suppress gcvtCalled
|
2015-03-14 19:40:13 +01:00
|
|
|
gcvt(3.141, 2, buf);
|
2015-08-14 01:36:44 +02:00
|
|
|
|
2015-03-08 21:38:43 +01:00
|
|
|
char *filename;
|
|
|
|
struct utimbuf *times;
|
|
|
|
// cppcheck-suppress uninitvar
|
2015-11-22 17:52:47 +01:00
|
|
|
// cppcheck-suppress utimeCalled
|
2015-03-08 21:38:43 +01:00
|
|
|
utime(filename, times);
|
|
|
|
struct timeval times1[2];
|
|
|
|
// cppcheck-suppress uninitvar
|
2015-11-22 17:52:47 +01:00
|
|
|
// cppcheck-suppress utimeCalled
|
2015-03-08 21:38:43 +01:00
|
|
|
utime(filename, times1);
|
2018-01-23 18:15:37 +01:00
|
|
|
|
2018-01-22 14:54:14 +01:00
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
// cppcheck-suppress uninitvar
|
2019-03-02 19:38:11 +01:00
|
|
|
int access_ret = access("file", x3);
|
2018-01-29 11:19:31 +01:00
|
|
|
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress leakReturnValNotUsed
|
|
|
|
// cppcheck-suppress uninitvar
|
2019-03-02 19:38:11 +01:00
|
|
|
fdopen(x4, "rw");
|
2018-01-31 10:58:30 +01:00
|
|
|
|
|
|
|
char *strtok_arg1;
|
|
|
|
// cppcheck-suppress strtokCalled
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
strtok(strtok_arg1, ";");
|
2018-07-21 17:52:12 +02:00
|
|
|
|
|
|
|
// cppcheck-suppress uninitvar
|
2019-03-02 19:38:11 +01:00
|
|
|
pthread_mutex_lock(&mutex1);
|
2018-07-21 17:52:12 +02:00
|
|
|
// cppcheck-suppress uninitvar
|
2019-03-02 19:38:11 +01:00
|
|
|
pthread_mutex_trylock(&mutex2);
|
2018-07-21 17:52:12 +02:00
|
|
|
// cppcheck-suppress uninitvar
|
2019-03-02 19:38:11 +01:00
|
|
|
pthread_mutex_unlock(&mutex3);
|
2018-07-21 17:52:12 +02:00
|
|
|
// after initialization it must be OK to call lock, trylock and unlock for this mutex
|
|
|
|
pthread_mutex_init(&mutex, NULL);
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
pthread_mutex_trylock(&mutex);
|
|
|
|
pthread_mutex_unlock(&mutex);
|
2015-02-17 18:08:19 +01:00
|
|
|
}
|
2015-02-17 18:16:29 +01:00
|
|
|
|
2015-11-25 11:02:32 +01:00
|
|
|
void uninitvar_getcwd(void)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
size_t size;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
(void)getcwd(buf,size);
|
|
|
|
}
|
|
|
|
|
2015-03-08 21:38:43 +01:00
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninitvar_types(void)
|
|
|
|
{
|
2015-02-17 19:58:07 +01:00
|
|
|
// cppcheck-suppress unassignedVariable
|
2015-02-17 18:16:29 +01:00
|
|
|
blkcnt_t b;
|
|
|
|
// cppcheck-suppress uninitvar
|
2015-02-17 19:58:07 +01:00
|
|
|
b + 1;
|
2015-02-17 18:16:29 +01:00
|
|
|
|
2015-02-17 19:58:07 +01:00
|
|
|
struct dirent d;
|
2015-02-17 18:16:29 +01:00
|
|
|
// cppcheck-suppress uninitvar
|
2015-02-17 19:58:07 +01:00
|
|
|
d.d_ino + 1;
|
2015-02-17 18:16:29 +01:00
|
|
|
}
|
2015-03-11 18:59:34 +01:00
|
|
|
|
2015-09-09 08:53:12 +02:00
|
|
|
void timet_h(struct timespec* ptp1)
|
2015-08-14 01:36:44 +02:00
|
|
|
{
|
2019-03-02 19:38:11 +01:00
|
|
|
clockid_t clk_id1, clk_id2, clk_id3;
|
2015-08-14 01:36:44 +02:00
|
|
|
struct timespec* ptp;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
clock_settime(CLOCK_REALTIME, ptp);
|
2015-09-09 08:53:12 +02:00
|
|
|
// cppcheck-suppress uninitvar
|
2019-03-02 19:38:11 +01:00
|
|
|
clock_settime(clk_id1, ptp);
|
2015-09-09 08:53:12 +02:00
|
|
|
// cppcheck-suppress uninitvar
|
2019-03-02 19:38:11 +01:00
|
|
|
clock_settime(clk_id2, ptp1);
|
2015-09-09 08:53:12 +02:00
|
|
|
|
|
|
|
struct timespec tp;
|
2018-01-10 15:52:06 +01:00
|
|
|
// TODO cppcheck-suppress uninitvar
|
2015-09-09 08:53:12 +02:00
|
|
|
clock_settime(CLOCK_REALTIME, &tp); // #6577 - false negative
|
|
|
|
// cppcheck-suppress uninitvar
|
2019-03-02 19:38:11 +01:00
|
|
|
clock_settime(clk_id3, &tp);
|
2015-08-14 01:36:44 +02:00
|
|
|
|
|
|
|
time_t clock = time(0);
|
|
|
|
char buf[26];
|
2015-11-22 17:52:47 +01:00
|
|
|
// cppcheck-suppress ctime_rCalled
|
2015-08-14 01:36:44 +02:00
|
|
|
ctime_r(&clock, buf);
|
2015-03-11 18:59:34 +01:00
|
|
|
}
|
2019-01-10 21:14:37 +01:00
|
|
|
|
|
|
|
void dl(const char* libname, const char* func)
|
|
|
|
{
|
|
|
|
void* lib = dlopen(libname, RTLD_NOW);
|
|
|
|
// cppcheck-suppress resourceLeak
|
|
|
|
// cppcheck-suppress redundantAssignment
|
|
|
|
lib = dlopen(libname, RTLD_LAZY);
|
|
|
|
const char* funcname;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
void* sym = dlsym(lib, funcname);
|
2019-01-12 21:50:02 +01:00
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
dlsym(lib, "foo");
|
2019-01-10 21:14:37 +01:00
|
|
|
void* uninit;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
dlclose(uninit);
|
|
|
|
// cppcheck-suppress resourceLeak
|
|
|
|
}
|