cppcheck/test/cfg/posix.c

295 lines
7.3 KiB
C
Raw Normal View History

2015-01-30 07:52:32 +01:00
// Test library configuration for posix.cfg
//
// Usage:
// $ 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
//
#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>
#include <fcntl.h>
// unavailable on some linux systems #include <ndbm.h>
#include <netdb.h>
#include <regex.h>
#include <time.h>
#include <unistd.h>
2015-02-15 18:11:09 +01: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;
2015-02-10 17:29:36 +01:00
}
void nullPointer(char *p, int fd)
{
// cppcheck-suppress ignoredReturnValue
isatty(0);
mkdir(p, 0);
getcwd(0, 0);
2015-01-30 07:52:32 +01:00
// cppcheck-suppress nullPointer
// cppcheck-suppress readdirCalled
readdir(0);
2015-03-08 14:43:25 +01:00
// cppcheck-suppress nullPointer
// cppcheck-suppress utimeCalled
2015-03-08 14:43:25 +01:00
utime(NULL, NULL);
// not implemented yet: cppcheck-suppress nullPointer
read(fd,NULL,42);
read(fd,NULL,0);
// not implemented yet: cppcheck-suppress nullPointer
write(fd,NULL,42);
write(fd,NULL,0);
// cppcheck-suppress leakReturnValNotUsed
// cppcheck-suppress nullPointer
open(NULL, 0);
// cppcheck-suppress leakReturnValNotUsed
// cppcheck-suppress nullPointer
open(NULL, 0, 0);
// cppcheck-suppress unreadVariable
// cppcheck-suppress nullPointer
int ret = access(NULL, 0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
// cppcheck-suppress nullPointer
fdopen(fd, NULL);
// cppcheck-suppress strtokCalled
// cppcheck-suppress nullPointer
strtok(p, NULL);
2015-01-30 07:52:32 +01: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);
}
void memleak_mmap(int fd)
{
// 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
}
void resourceLeak_fdopen(int fd)
{
// cppcheck-suppress unreadVariable
2015-02-15 18:11:09 +01:00
FILE *f = fdopen(fd, "r");
// cppcheck-suppress resourceLeak
}
void resourceLeak_fdopendir(int fd)
{
// cppcheck-suppress unreadVariable
2015-02-15 18:11:09 +01:00
DIR* leak1 = fdopendir(fd);
// cppcheck-suppress resourceLeak
}
void resourceLeak_opendir(void)
{
// cppcheck-suppress unreadVariable
2015-02-15 18:11:09 +01:00
DIR* leak1 = opendir("abc");
// cppcheck-suppress resourceLeak
}
void resourceLeak_socket(void)
{
// cppcheck-suppress unreadVariable
2015-02-15 18:11:09 +01:00
int s = socket(AF_INET, SOCK_STREAM, 0);
// cppcheck-suppress resourceLeak
}
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
}
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
}
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);
int fd1 = open("a", O_RDWR | O_CREAT);
close(fd1);
int fd2 = open("a", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
close(fd2);
/* 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
void ignoredReturnValue(void *addr, int fd)
{
// 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
setuid(42);
// cppcheck-suppress ignoredReturnValue
getuid();
// cppcheck-suppress ignoredReturnValue
access("filename", 1);
2015-02-16 16:25:27 +01:00
}
// valid range
void invalidFunctionArg()
{
2015-02-16 16:25:27 +01:00
// cppcheck-suppress invalidFunctionArg
// cppcheck-suppress usleepCalled
2015-02-16 16:25:27 +01:00
usleep(-1);
// cppcheck-suppress usleepCalled
2015-02-16 16:25:27 +01:00
usleep(0);
// cppcheck-suppress usleepCalled
2015-02-16 16:25:27 +01:00
usleep(999999);
// cppcheck-suppress invalidFunctionArg
// cppcheck-suppress usleepCalled
2015-02-16 16:25:27 +01:00
usleep(1000000);
}
void uninitvar(int fd)
{
int x;
char buf[2];
int decimal, sign;
double d;
void *p;
// cppcheck-suppress uninitvar
write(x,"ab",2);
// TODO cppcheck-suppress uninitvar
write(fd,buf,2); // #6325
// cppcheck-suppress uninitvar
write(fd,"ab",x);
// cppcheck-suppress uninitvar
write(fd,p,2);
/* int regcomp(regex_t *restrict preg, const char *restrict pattern, int cflags); */
regex_t reg;
const char * pattern;
int cflags;
// cppcheck-suppress uninitvar
regcomp(&reg, pattern, cflags);
pattern="";
// cppcheck-suppress uninitvar
regcomp(&reg, pattern, cflags);
regerror(0, &reg, 0, 0);
// cppcheck-suppress uninitvar
// cppcheck-suppress unreadVariable
// cppcheck-suppress ecvtCalled
char *buffer = ecvt(d, 11, &decimal, &sign);
// cppcheck-suppress gcvtCalled
2015-03-14 19:40:13 +01:00
gcvt(3.141, 2, buf);
char *filename;
struct utimbuf *times;
// cppcheck-suppress uninitvar
// cppcheck-suppress utimeCalled
utime(filename, times);
struct timeval times1[2];
// cppcheck-suppress uninitvar
// cppcheck-suppress utimeCalled
utime(filename, times1);
2018-01-23 18:15:37 +01:00
// cppcheck-suppress unreadVariable
// cppcheck-suppress uninitvar
int access_ret = access("file", x);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
// cppcheck-suppress uninitvar
fdopen(x, "rw");
char *strtok_arg1;
// cppcheck-suppress strtokCalled
// cppcheck-suppress uninitvar
strtok(strtok_arg1, ";");
}
void uninitvar_getcwd(void)
{
char *buf;
size_t size;
// cppcheck-suppress uninitvar
(void)getcwd(buf,size);
}
void uninitvar_types(void)
{
2015-02-17 19:58:07 +01:00
// cppcheck-suppress unassignedVariable
blkcnt_t b;
// cppcheck-suppress uninitvar
2015-02-17 19:58:07 +01:00
b + 1;
2015-02-17 19:58:07 +01:00
struct dirent d;
// cppcheck-suppress uninitvar
2015-02-17 19:58:07 +01:00
d.d_ino + 1;
}
void timet_h(struct timespec* ptp1)
{
clockid_t clk_id;
struct timespec* ptp;
// cppcheck-suppress uninitvar
clock_settime(CLOCK_REALTIME, ptp);
// cppcheck-suppress uninitvar
clock_settime(clk_id, ptp);
// cppcheck-suppress uninitvar
clock_settime(clk_id, ptp1);
struct timespec tp;
// TODO cppcheck-suppress uninitvar
clock_settime(CLOCK_REALTIME, &tp); // #6577 - false negative
// cppcheck-suppress uninitvar
clock_settime(clk_id, &tp);
time_t clock = time(0);
char buf[26];
// cppcheck-suppress ctime_rCalled
ctime_r(&clock, buf);
}