cppcheck/test/cfg/posix.c

969 lines
26 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 <aio.h>
#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 <sys/time.h>
#include <sys/types.h>
#include <dlfcn.h>
2015-02-15 18:11:09 +01:00
#include <fcntl.h>
// unavailable on some linux systems #include <ndbm.h>
#include <netdb.h>
#include <regex.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <syslog.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdbool.h>
#define _XOPEN_SOURCE
#include <wchar.h>
#include <string.h>
int nullPointer_ttyname_r(int fd, char *buf, size_t buflen)
{
// cppcheck-suppress nullPointer
(void)ttyname_r(fd,NULL,buflen);
return ttyname_r(fd,buf,buflen);
}
size_t bufferAccessOutOfBounds_wcsnrtombs(char *restrict dest, const wchar_t **restrict src, size_t nwc, size_t len, mbstate_t *restrict ps)
{
char buf[42];
(void)wcsnrtombs(buf,src,nwc,42,ps);
// cppcheck-suppress bufferAccessOutOfBounds
(void)wcsnrtombs(buf,src,nwc,43,ps);
return wcsnrtombs(dest,src,nwc,len,ps);
}
size_t nullPointer_wcsnrtombs(char *restrict dest, const wchar_t **restrict src, size_t nwc, size_t len, mbstate_t *restrict ps)
{
// It is allowed to set the first arg to NULL
(void)wcsnrtombs(NULL,src,nwc,len,ps);
// cppcheck-suppress nullPointer
(void)wcsnrtombs(dest,NULL,nwc,len,ps);
// It is allowed to set the last arg to NULL
(void)wcsnrtombs(dest,src,nwc,len,NULL);
return wcsnrtombs(dest,src,nwc,len,ps);
}
int nullPointer_wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
{
// cppcheck-suppress nullPointer
(void)wcsncasecmp(NULL,s2,n);
// cppcheck-suppress nullPointer
(void)wcsncasecmp(s1,NULL,n);
return wcsncasecmp(s1,s2,n);
}
int uninitvar_wcwidth(const wchar_t c)
{
wchar_t wc;
// cppcheck-suppress uninitvar
(void)wcwidth(wc);
// No warning is expected
return wcwidth(c);
}
2022-04-20 15:27:14 +02:00
int nullPointer_wcsnlen(const wchar_t *s, size_t n)
{
// cppcheck-suppress nullPointer
(void)wcsnlen(NULL, n);
// No warning is expected
return wcsnlen(s, n);
}
2022-04-21 09:10:13 +02:00
size_t bufferAccessOutOfBounds_wcsnlen(void) // #10997
{
wchar_t buf[2]={L'4',L'2'};
size_t len = wcsnlen(buf,2);
// TODO cppcheck-suppress bufferAccessOutOfBounds
len+=wcsnlen(buf,3);
return len;
}
int nullPointer_gethostname(char *s, size_t n)
{
// cppcheck-suppress nullPointer
(void)gethostname(NULL, n);
// No warning is expected
return gethostname(s, n);
}
int nullPointer_wcswidth(const wchar_t *s, size_t n)
{
// cppcheck-suppress nullPointer
(void)wcswidth(NULL, n);
// No warning is expected
return wcswidth(s, n);
}
int nullPointer_aio_cancel(int fd, struct aiocb *aiocbp)
{
// No warning is expected
(void)aio_cancel(fd, NULL);
// No warning is expected
return aio_cancel(fd, aiocbp);
}
int nullPointer_aio_fsync(int op, struct aiocb *aiocbp)
{
// cppcheck-suppress nullPointer
(void)aio_fsync(op, NULL);
// No warning is expected
return aio_fsync(op, aiocbp);
}
ssize_t nullPointer_aio_return(struct aiocb *aiocbp)
{
// cppcheck-suppress nullPointer
(void)aio_return(NULL);
// No warning is expected
return aio_return(aiocbp);
}
int nullPointer_aio_error(const struct aiocb *aiocbp)
{
// cppcheck-suppress nullPointer
(void)aio_error(NULL);
// No warning is expected
return aio_error(aiocbp);
}
int nullPointer_aio_read(struct aiocb *aiocbp)
{
// cppcheck-suppress nullPointer
(void)aio_read(NULL);
// No warning is expected
return aio_read(aiocbp);
}
int nullPointer_aio_write(struct aiocb *aiocbp)
{
// cppcheck-suppress nullPointer
(void)aio_write(NULL);
// No warning is expected
return aio_write(aiocbp);
}
int nullPointer_aio_suspend(const struct aiocb *const aiocb_list[], int nitems, const struct timespec *restrict timeout)
{
// cppcheck-suppress nullPointer
(void)aio_suspend(NULL, nitems, timeout);
// No warning is expected
return aio_suspend(aiocb_list, nitems, timeout);
}
// Note: Since glibc 2.28, this function symbol is no longer available to newly linked applications.
void invalidFunctionArg_llseek(int fd, loff_t offset, int origin)
{
// cppcheck-suppress llseekCalled
// cppcheck-suppress invalidFunctionArg
(void)llseek(-1, offset, SEEK_SET);
// cppcheck-suppress llseekCalled
// cppcheck-suppress invalidFunctionArg
(void)llseek(fd, offset, -1);
// cppcheck-suppress llseekCalled
// cppcheck-suppress invalidFunctionArg
(void)llseek(fd, offset, 3);
// cppcheck-suppress llseekCalled
// cppcheck-suppress invalidFunctionArg
(void)llseek(fd, offset, 42+SEEK_SET);
// cppcheck-suppress llseekCalled
// cppcheck-suppress invalidFunctionArg
(void)llseek(fd, offset, SEEK_SET+42);
// No invalidFunctionArg warning is expected for
// cppcheck-suppress llseekCalled
(void)llseek(0, offset, origin);
// cppcheck-suppress llseekCalled
(void)llseek(fd, offset, origin);
// cppcheck-suppress llseekCalled
(void)llseek(fd, offset, SEEK_SET);
// cppcheck-suppress llseekCalled
(void)llseek(fd, offset, SEEK_CUR);
// cppcheck-suppress llseekCalled
(void)llseek(fd, offset, SEEK_END);
}
void invalidFunctionArg_lseek64(int fd, off_t offset, int origin)
{
// cppcheck-suppress invalidFunctionArg
(void)lseek64(-1, offset, SEEK_SET);
// cppcheck-suppress invalidFunctionArg
(void)lseek64(fd, offset, -1);
// cppcheck-suppress invalidFunctionArg
(void)lseek64(fd, offset, 3);
// cppcheck-suppress invalidFunctionArg
(void)lseek64(fd, offset, 42+SEEK_SET);
// cppcheck-suppress invalidFunctionArg
(void)lseek64(fd, offset, SEEK_SET+42);
// No warning is expected for
(void)lseek64(0, offset, origin);
(void)lseek64(fd, offset, origin);
(void)lseek64(fd, offset, SEEK_SET);
(void)lseek64(fd, offset, SEEK_CUR);
(void)lseek64(fd, offset, SEEK_END);
}
void invalidFunctionArg_lseek(int fd, off_t offset, int origin)
{
// cppcheck-suppress invalidFunctionArg
(void)lseek(-1, offset, SEEK_SET);
// cppcheck-suppress invalidFunctionArg
(void)lseek(fd, offset, -1);
// cppcheck-suppress invalidFunctionArg
(void)lseek(fd, offset, 3);
// cppcheck-suppress invalidFunctionArg
(void)lseek(fd, offset, 42+SEEK_SET);
// cppcheck-suppress invalidFunctionArg
(void)lseek(fd, offset, SEEK_SET+42);
// No warning is expected for
(void)lseek(0, offset, origin);
(void)lseek(fd, offset, origin);
(void)lseek(fd, offset, SEEK_SET);
(void)lseek(fd, offset, SEEK_CUR);
(void)lseek(fd, offset, SEEK_END);
}
void invalidFunctionArg_fseeko(FILE* stream, off_t offset, int origin)
{
// cppcheck-suppress invalidFunctionArg
(void)fseeko(stream, offset, -1);
// cppcheck-suppress invalidFunctionArg
(void)fseeko(stream, offset, 3);
// cppcheck-suppress invalidFunctionArg
(void)fseeko(stream, offset, 42+SEEK_SET);
// cppcheck-suppress invalidFunctionArg
(void)fseeko(stream, offset, SEEK_SET+42);
// No warning is expected for
(void)fseeko(stream, offset, origin);
(void)fseeko(stream, offset, SEEK_SET);
(void)fseeko(stream, offset, SEEK_CUR);
(void)fseeko(stream, offset, SEEK_END);
}
wchar_t *nullPointer_wcpncpy(wchar_t *dest, const wchar_t *src, size_t n)
{
// cppcheck-suppress nullPointer
(void)wcpncpy(NULL, src, n);
// cppcheck-suppress nullPointer
(void)wcpncpy(dest, NULL, n);
return wcpncpy(dest, src, n);
}
int nullPointer_utimes(const char *path, const struct timeval times[2])
{
// cppcheck-suppress nullPointer
// cppcheck-suppress utimesCalled
(void)utimes(NULL, times);
// cppcheck-suppress utimesCalled
return utimes(path, times);
}
char * overlappingWriteFunction_stpcpy(char *src, char *dest)
{
// No warning shall be shown:
(void) stpcpy(dest, src);
// cppcheck-suppress overlappingWriteFunction
return stpcpy(src, src);
}
int nullPointer_strcasecmp(char *a, char *b)
{
// No warning shall be shown:
(void) strcasecmp(a, b);
// cppcheck-suppress nullPointer
(void) strcasecmp(a, NULL);
// cppcheck-suppress nullPointer
return strcasecmp(NULL, b);
}
int nullPointer_strncasecmp(char *a, char *b, size_t n)
{
// No warning shall be shown:
(void) strncasecmp(a, b, n);
// cppcheck-suppress nullPointer
(void) strncasecmp(a, NULL, n);
// cppcheck-suppress nullPointer
return strncasecmp(NULL, b, n);
}
int nullPointer_bcmp(const void *a, const void *b, size_t n)
{
// No nullPointer warning shall be shown:
// cppcheck-suppress bcmpCalled
(void) bcmp(a, b, n);
// cppcheck-suppress nullPointer
// cppcheck-suppress bcmpCalled
(void) bcmp(a, NULL, n);
// cppcheck-suppress nullPointer
// cppcheck-suppress bcmpCalled
return bcmp(NULL, b, n);
}
void nullPointer_bzero(void *s, size_t n)
{
// cppcheck-suppress nullPointer
// cppcheck-suppress bzeroCalled
bzero(NULL,n);
// No nullPointer-warning shall be shown:
// cppcheck-suppress bzeroCalled
bzero(s,n);
}
void bufferAccessOutOfBounds_bzero(void *s, size_t n)
{
char buf[42];
// cppcheck-suppress bufferAccessOutOfBounds
// cppcheck-suppress bzeroCalled
bzero(buf,43);
// cppcheck-suppress bzeroCalled
bzero(buf,42);
// No nullPointer-warning shall be shown:
// cppcheck-suppress bzeroCalled
bzero(s,n);
}
size_t bufferAccessOutOfBounds_strnlen(const char *s, size_t maxlen)
{
char buf[2]={'4','2'};
size_t len = strnlen(buf,2);
// cppcheck-suppress bufferAccessOutOfBounds
len+=strnlen(buf,3);
return len;
}
size_t nullPointer_strnlen(const char *s, size_t maxlen)
{
// No warning shall be shown:
(void) strnlen(s, maxlen);
// cppcheck-suppress nullPointer
return strnlen(NULL, maxlen);
}
char * nullPointer_stpcpy(char *src, char *dest)
{
// No warning shall be shown:
(void) stpcpy(dest, src);
// cppcheck-suppress nullPointer
(void) stpcpy(dest, NULL);
// cppcheck-suppress nullPointer
return stpcpy(NULL, src);
}
char * nullPointer_strsep(char **stringptr, char *delim)
{
// No warning shall be shown:
(void) strsep(stringptr, delim);
// cppcheck-suppress nullPointer
(void) strsep(stringptr, NULL);
// cppcheck-suppress nullPointer
return strsep(NULL, delim);
}
void overlappingWriteFunction_bcopy(char *buf, const size_t count)
{
// No warning shall be shown:
// cppcheck-suppress bcopyCalled
bcopy(&buf[0], &buf[3], count); // size is not known
// cppcheck-suppress bcopyCalled
bcopy(&buf[0], &buf[3], 3U); // no-overlap
// cppcheck-suppress bcopyCalled
bcopy(&buf[0], &buf[3], 4U); // The result is correct, even when both areas overlap.
}
void nullPointer_bcopy(const void *src, void *dest, size_t n)
{
// No warning shall be shown:
// cppcheck-suppress bcopyCalled
bcopy(src, dest, n);
// cppcheck-suppress bcopyCalled
// cppcheck-suppress nullPointer
bcopy(NULL, dest, n);
// cppcheck-suppress bcopyCalled
// cppcheck-suppress nullPointer
bcopy(src, NULL, n);
}
void overlappingWriteFunction_memccpy(unsigned char *src, unsigned char *dest, int c, size_t count)
{
// No warning shall be shown:
(void)memccpy(dest, src, c, count);
(void)memccpy(dest, src, 42, count);
// cppcheck-suppress overlappingWriteFunction
(void)memccpy(dest, dest, c, 4);
// cppcheck-suppress overlappingWriteFunction
(void)memccpy(dest, dest+3, c, 4);
}
void overlappingWriteFunction_stpncpy(char *src, char *dest, ssize_t n)
{
// No warning shall be shown:
(void) stpncpy(dest, src, n);
// cppcheck-suppress overlappingWriteFunction
(void)stpncpy(src, src+3, 4);
}
wchar_t* overlappingWriteFunction_wcpncpy(wchar_t *src, wchar_t *dest, ssize_t n)
{
// No warning shall be shown:
(void) wcpncpy(dest, src, n);
// cppcheck-suppress overlappingWriteFunction
return wcpncpy(src, src+3, 4);
}
2021-07-10 18:28:26 +02:00
void overlappingWriteFunction_swab(char *src, char *dest, ssize_t n)
{
// No warning shall be shown:
swab(dest, src, n);
// cppcheck-suppress overlappingWriteFunction
swab(src, src+3, 4);
}
void bufferAccessOutOfBounds_swab(char *src, char *dest, ssize_t n)
{
// No warning shall be shown:
swab(dest, src, n);
char srcBuf[42] = {0};
char destBuf[42] = {0};
swab(srcBuf, dest, 42);
// cppcheck-suppress bufferAccessOutOfBounds
swab(srcBuf, dest, 43);
swab(src, destBuf, 42);
// cppcheck-suppress bufferAccessOutOfBounds
swab(src, destBuf, 43);
}
void nullPointer_swab(char *src, char *dest, ssize_t n)
{
// No warning shall be shown:
swab(dest, src, n);
// cppcheck-suppress nullPointer
swab(NULL, dest, n);
// cppcheck-suppress nullPointer
swab(src, NULL, n);
}
bool invalidFunctionArgBool_isascii(bool b, int c)
{
// cppcheck-suppress invalidFunctionArgBool
(void)isascii(b);
// cppcheck-suppress invalidFunctionArgBool
return isascii(c != 0);
}
2015-02-15 18:11:09 +01:00
void uninitvar_putenv(char * envstr)
{
// No warning is expected
(void)putenv(envstr);
char * p;
// cppcheck-suppress uninitvar
(void)putenv(p);
}
void nullPointer_putenv(char * envstr)
{
// No warning is expected
(void)putenv(envstr);
char * p=NULL;
// cppcheck-suppress nullPointer
(void)putenv(p);
}
void memleak_scandir(void)
{
struct dirent **namelist;
int n = scandir(".", &namelist, NULL, alphasort);
if (n == -1) {
return;
}
// http://man7.org/linux/man-pages/man3/scandir.3.html
/* The scandir() function scans the directory dirp, calling filter() on
each directory entry. Entries for which filter() returns nonzero are
stored in strings allocated via malloc(3), sorted using qsort(3) with
the comparison function compar(), and collected in array namelist
which is allocated via malloc(3). If filter is NULL, all entries are
selected.*/
// TODO: cppcheck-suppress memleak
}
void no_memleak_scandir(void)
{
struct dirent **namelist;
int n = scandir(".", &namelist, NULL, alphasort);
if (n == -1) {
return;
}
while (n--) {
free(namelist[n]);
}
free(namelist);
}
void validCode(va_list valist_arg1, va_list valist_arg2)
{
void *ptr;
if (posix_memalign(&ptr, sizeof(void *), sizeof(void *)) == 0)
free(ptr);
syslog(LOG_ERR, "err %u", 0U);
syslog(LOG_WARNING, "warn %d %d", 5, 1);
vsyslog(LOG_EMERG, "emerg %d", valist_arg1);
vsyslog(LOG_INFO, "test %s %d %p", valist_arg2);
void* handle = dlopen("/lib.so", RTLD_NOW);
if (handle) {
dlclose(handle);
}
}
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;
readlink("path", a, 5);
// cppcheck-suppress bufferAccessOutOfBounds
readlink("path", a, 6);
readlinkat(1, "path", a, 5);
// cppcheck-suppress bufferAccessOutOfBounds
readlinkat(1, "path", a, 6);
// This is valid
gethostname(a, 5);
// cppcheck-suppress bufferAccessOutOfBounds
gethostname(a, 6);
2015-02-10 17:29:36 +01:00
}
void nullPointer(char *p, int fd, pthread_mutex_t mutex)
{
// 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);
// 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
(void)pthread_mutex_trylock(NULL);
// cppcheck-suppress nullPointer
pthread_mutex_unlock(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 unusedAllocatedMemory
// 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 * memleak_mmap2() // #8327
{
void * data = mmap(NULL, 10, PROT_READ, MAP_PRIVATE, 1, 0);
if (data != MAP_FAILED)
return data;
return NULL;
}
void * identicalCondition_mmap(int fd, size_t size) // #9940
{
void* buffer = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buffer == MAP_FAILED) {
return NULL;
}
return buffer;
}
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_mkstemp(char *template)
{
2019-05-17 09:32:14 +02:00
// cppcheck-suppress unreadVariable
int fp = mkstemp(template);
// cppcheck-suppress resourceLeak
}
void no_resourceLeak_mkstemp_01(char *template)
{
2019-05-17 09:32:14 +02:00
int fp = mkstemp(template);
close(fp);
}
int no_resourceLeak_mkstemp_02(char *template)
{
2019-05-17 09:32:14 +02:00
return mkstemp(template);
}
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);
2021-08-07 20:51:18 +02:00
*/
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
getuid();
// cppcheck-suppress ignoredReturnValue
access("filename", 1);
// no ignoredReturnValue shall be shown for
setuid(42);
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 invalidFunctionArg_close(int fd)
{
if (fd < 0) {
// cppcheck-suppress invalidFunctionArg
(void)close(fd);
}
}
void uninitvar(int fd)
{
2019-03-02 19:38:11 +01:00
int x1, x2, x3, x4;
char buf[2];
int decimal, sign;
double d;
void *p;
2019-03-02 19:38:11 +01:00
pthread_mutex_t mutex, mutex1, mutex2, mutex3;
// cppcheck-suppress uninitvar
2019-03-02 19:38:11 +01:00
write(x1,"ab",2);
// TODO cppcheck-suppress uninitvar
write(fd,buf,2); // #6325
// cppcheck-suppress uninitvar
2019-03-02 19:38:11 +01:00
write(fd,"ab",x2);
// 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;
2019-03-02 19:38:11 +01:00
int cflags1, cflags2;
// cppcheck-suppress uninitvar
2019-03-02 19:38:11 +01:00
regcomp(&reg, pattern, cflags1);
pattern="";
// cppcheck-suppress uninitvar
2019-03-02 19:38:11 +01:00
regcomp(&reg, pattern, cflags2);
regerror(0, &reg, 0, 0);
2018-02-22 12:27:46 +01:00
#ifndef __CYGWIN__
// cppcheck-suppress uninitvar
// cppcheck-suppress unreadVariable
// cppcheck-suppress ecvtCalled
char *buffer = ecvt(d, 11, &decimal, &sign);
2018-02-22 12:27:46 +01:00
#endif
// cppcheck-suppress gcvtCalled
2015-03-14 19:40:13 +01:00
gcvt(3.141, 2, buf);
2021-10-03 20:32:39 +02:00
char *filename1, *filename2;
struct utimbuf *times;
// cppcheck-suppress uninitvar
// cppcheck-suppress utimeCalled
2021-10-03 20:32:39 +02:00
utime(filename1, times);
struct timeval times1[2];
// cppcheck-suppress uninitvar
// cppcheck-suppress utimeCalled
2021-10-03 20:32:39 +02:00
utime(filename2, times1);
2018-01-23 18:15:37 +01:00
// cppcheck-suppress unreadVariable
// cppcheck-suppress uninitvar
2019-03-02 19:38:11 +01:00
int access_ret = access("file", x3);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
// cppcheck-suppress uninitvar
2019-03-02 19:38:11 +01:00
fdopen(x4, "rw");
char *strtok_arg1;
// cppcheck-suppress strtokCalled
// cppcheck-suppress uninitvar
strtok(strtok_arg1, ";");
2021-05-22 07:39:27 +02:00
// cppcheck-suppress uninitvar
2019-03-02 19:38:11 +01:00
pthread_mutex_lock(&mutex1);
2021-05-22 07:39:27 +02:00
// cppcheck-suppress uninitvar
(void)pthread_mutex_trylock(&mutex2);
2021-05-22 07:39:27 +02:00
// cppcheck-suppress uninitvar
2019-03-02 19:38:11 +01:00
pthread_mutex_unlock(&mutex3);
// after initialization it must be OK to call lock, trylock and unlock for this mutex
pthread_mutex_init(&mutex, NULL);
pthread_mutex_lock(&mutex);
(void)pthread_mutex_trylock(&mutex);
pthread_mutex_unlock(&mutex);
}
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;
2019-03-09 15:41:07 +01:00
// TODO cppcheck-suppress uninitvar
2015-02-17 19:58:07 +01:00
d.d_ino + 1;
}
void timet_h(struct timespec* ptp1)
{
2019-03-02 19:38:11 +01:00
clockid_t clk_id1, clk_id2, clk_id3;
struct timespec* ptp;
// cppcheck-suppress uninitvar
clock_settime(CLOCK_REALTIME, ptp);
// cppcheck-suppress uninitvar
2019-03-02 19:38:11 +01:00
clock_settime(clk_id1, ptp);
// cppcheck-suppress uninitvar
2019-03-02 19:38:11 +01:00
clock_settime(clk_id2, ptp1);
struct timespec tp;
// cppcheck-suppress uninitvar
clock_settime(CLOCK_REALTIME, &tp); // #6577 - false negative
// cppcheck-suppress uninitvar
2019-03-02 19:38:11 +01:00
clock_settime(clk_id3, &tp);
time_t clock = time(0);
char buf[26];
// cppcheck-suppress ctime_rCalled
ctime_r(&clock, buf);
}
void dl(const char* libname, const char* func)
{
void* lib = dlopen(libname, RTLD_NOW);
2019-09-04 13:38:19 +02:00
// cppcheck-suppress redundantInitialization
// cppcheck-suppress resourceLeak
lib = dlopen(libname, RTLD_LAZY);
const char* funcname;
// cppcheck-suppress uninitvar
// cppcheck-suppress unreadVariable
void* sym = dlsym(lib, funcname);
// cppcheck-suppress ignoredReturnValue
dlsym(lib, "foo");
void* uninit;
// cppcheck-suppress uninitvar
dlclose(uninit);
// cppcheck-suppress resourceLeak
}
void asctime_r_test(struct tm * tm, char * bufSizeUnknown)
{
struct tm tm_uninit_data;
struct tm * tm_uninit_pointer;
char bufSize5[5];
char bufSize25[25];
char bufSize26[26];
char bufSize100[100];
// cppcheck-suppress asctime_rCalled
// cppcheck-suppress bufferAccessOutOfBounds
asctime_r(tm, bufSize5);
// cppcheck-suppress asctime_rCalled
// cppcheck-suppress bufferAccessOutOfBounds
asctime_r(tm, bufSize25);
// cppcheck-suppress asctime_rCalled
asctime_r(tm, bufSize26);
// cppcheck-suppress asctime_rCalled
asctime_r(tm, bufSize100);
// cppcheck-suppress asctime_rCalled
2021-05-22 07:39:27 +02:00
// cppcheck-suppress uninitvar
asctime_r(&tm_uninit_data, bufSize100);
// cppcheck-suppress asctime_rCalled
// cppcheck-suppress uninitvar
asctime_r(tm_uninit_pointer, bufSize100);
// cppcheck-suppress asctime_rCalled
asctime_r(tm, bufSizeUnknown);
}
void ctime_r_test(time_t * timep, char * bufSizeUnknown)
{
time_t time_t_uninit_data;
time_t * time_t_uninit_pointer;
char bufSize5[5];
char bufSize25[25];
char bufSize26[26];
char bufSize100[100];
// cppcheck-suppress ctime_rCalled
// cppcheck-suppress bufferAccessOutOfBounds
ctime_r(timep, bufSize5);
// cppcheck-suppress ctime_rCalled
// cppcheck-suppress bufferAccessOutOfBounds
ctime_r(timep, bufSize25);
// cppcheck-suppress ctime_rCalled
ctime_r(timep, bufSize26);
// cppcheck-suppress ctime_rCalled
ctime_r(timep, bufSize100);
// cppcheck-suppress ctime_rCalled
2021-05-22 07:39:27 +02:00
// cppcheck-suppress uninitvar
ctime_r(&time_t_uninit_data, bufSize100);
// cppcheck-suppress ctime_rCalled
// cppcheck-suppress uninitvar
ctime_r(time_t_uninit_pointer, bufSize100);
// cppcheck-suppress ctime_rCalled
ctime_r(timep, bufSizeUnknown);
}