2015-02-16 22:06:08 +01:00
|
|
|
|
|
|
|
// Test library configuration for gnu.cfg
|
|
|
|
//
|
|
|
|
// Usage:
|
2023-03-09 20:04:20 +01:00
|
|
|
// $ cppcheck --check-library --library=gnu --enable=style,information --inconclusive --error-exitcode=1 --disable=missingInclude --inline-suppr test/cfg/gnu.c
|
2015-02-16 22:06:08 +01:00
|
|
|
// =>
|
|
|
|
// No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <string.h>
|
2018-10-27 18:25:05 +02:00
|
|
|
#include <stdlib.h>
|
2018-10-28 21:36:28 +01:00
|
|
|
#include <stdint.h>
|
2019-02-27 15:17:34 +01:00
|
|
|
#include <stdio.h>
|
2019-09-09 15:02:55 +02:00
|
|
|
#include <stdarg.h>
|
2022-05-04 20:29:12 +02:00
|
|
|
#include <netdb.h>
|
2019-09-08 19:17:15 +02:00
|
|
|
#include <sys/time.h>
|
2022-03-23 17:14:26 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <pwd.h>
|
2019-10-30 09:39:50 +01:00
|
|
|
#include <sys/mman.h>
|
2022-05-01 11:41:51 +02:00
|
|
|
#include <sys/sem.h>
|
2022-04-21 09:19:32 +02:00
|
|
|
#include <wchar.h>
|
2023-03-02 20:54:30 +01:00
|
|
|
#if !defined(__CYGWIN__) && !(defined(__APPLE__) && defined(__MACH__))
|
2018-09-25 09:07:49 +02:00
|
|
|
#include <sys/epoll.h>
|
2018-09-29 16:49:12 +02:00
|
|
|
#endif
|
2022-05-01 15:29:35 +02:00
|
|
|
#include <strings.h>
|
2023-03-02 20:54:30 +01:00
|
|
|
#ifdef __gnu_linux__
|
2022-07-17 14:43:52 +02:00
|
|
|
#include <error.h>
|
2023-03-02 20:54:30 +01:00
|
|
|
#endif
|
2022-12-21 17:06:08 +01:00
|
|
|
#include <getopt.h>
|
2022-12-22 09:47:19 +01:00
|
|
|
#include <netdb.h>
|
2022-05-01 15:29:35 +02:00
|
|
|
|
2023-03-02 20:54:30 +01:00
|
|
|
#ifdef __gnu_linux__
|
2022-07-17 14:43:52 +02:00
|
|
|
void unreachableCode_error(void) // #11197
|
|
|
|
{
|
|
|
|
error(1, 0, ""); // will call exit() if the first parameter is non-zero
|
|
|
|
// cppcheck-suppress unusedVariable
|
|
|
|
// TODO cppcheck-suppress unreachableCode
|
|
|
|
int i;
|
|
|
|
}
|
2023-03-02 20:54:30 +01:00
|
|
|
#endif
|
2022-05-03 11:53:36 +02:00
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
int nullPointer_gethostbyname2_r(const char* name, int af, struct hostent* ret, const char* buf, size_t buflen, struct hostent** result, const int* h_errnop)
|
2022-12-22 09:47:19 +01:00
|
|
|
{
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyname2_r(NULL, af, ret, buf, buflen, result, h_errnop);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyname2_r(name, af, NULL, buf, buflen, result, h_errnop);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyname2_r(name, af, ret, NULL, buflen, result, h_errnop);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyname2_r(name, af, ret, buf, buflen, NULL, h_errnop);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyname2_r(name, af, ret, buf, buflen, result, NULL);
|
|
|
|
return gethostbyname2_r(name, af, ret, buf, buflen, result, h_errnop);
|
|
|
|
}
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
int nullPointer_gethostbyname_r(const char* name, struct hostent* ret, const char* buf, size_t buflen, struct hostent** result, const int* h_errnop)
|
2022-12-22 09:47:19 +01:00
|
|
|
{
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyname_r(NULL, ret, buf, buflen, result, h_errnop);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyname_r(name, NULL, buf, buflen, result, h_errnop);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyname_r(name, ret, NULL, buflen, result, h_errnop);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyname_r(name, ret, buf, buflen, NULL, h_errnop);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyname_r(name, ret, buf, buflen, result, NULL);
|
|
|
|
return gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
int nullPointer_gethostbyaddr_r(const void* addr, socklen_t len, int type, struct hostent* ret, const char* buf, size_t buflen, struct hostent** result, const int* h_errnop)
|
2022-12-22 09:36:53 +01:00
|
|
|
{
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyaddr_r(NULL, len, type, ret, buf, buflen, result, h_errnop);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyaddr_r(addr, len, type, NULL, buf, buflen, result, h_errnop);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyaddr_r(addr, len, type, ret, NULL, buflen, result, h_errnop);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyaddr_r(addr, len, type, ret, buf, buflen, NULL, h_errnop);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) gethostbyaddr_r(addr, len, type, ret, buf, buflen, result, NULL);
|
|
|
|
return gethostbyaddr_r(addr, len, type, ret, buf, buflen, result, h_errnop);
|
|
|
|
}
|
|
|
|
|
2022-12-21 17:06:08 +01:00
|
|
|
int nullPointer_getopt_long(int argc, char **argv, const char *optstring,
|
|
|
|
const struct option *longopts, int *longindex)
|
|
|
|
{
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) getopt_long(argc, argv, NULL, longopts, longindex);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) getopt_long(argc, argv, optstring, NULL, longindex);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) getopt_long(argc, NULL, optstring, longopts, longindex);
|
|
|
|
return getopt_long(argc, argv, optstring, longopts, longindex);
|
|
|
|
}
|
|
|
|
|
2022-12-22 09:13:18 +01:00
|
|
|
int nullPointer_getopt_long_only(int argc, char* const* argv, const char* optstring,
|
|
|
|
const struct option* longopts, int* longindex)
|
|
|
|
{
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) getopt_long_only(argc, NULL, optstring, longopts, longindex);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) getopt_long_only(argc, argv, NULL, longopts, longindex);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) getopt_long_only(argc, argv, optstring, NULL, longindex);
|
|
|
|
return getopt_long_only(argc, argv, optstring, longopts, longindex);
|
|
|
|
}
|
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
int nullPointer_getservent_r(struct servent *restrict result_buf, const char *restrict buf, size_t buflen, struct servent **restrict result)
|
2022-05-03 11:53:36 +02:00
|
|
|
{
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) getservent_r(NULL, buf, buflen, result);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) getservent_r(result_buf, NULL, buflen, result);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) getservent_r(result_buf, buf, buflen, NULL);
|
|
|
|
return getservent_r(result_buf, buf, buflen, result);
|
|
|
|
}
|
|
|
|
|
2022-05-01 18:10:19 +02:00
|
|
|
void *bufferAccessOutOfBounds_memrchr(const void *s, int c, size_t n)
|
|
|
|
{
|
2023-08-05 18:48:43 +02:00
|
|
|
const char buf[42]={0};
|
2022-05-01 18:10:19 +02:00
|
|
|
(void)memrchr(buf,c,42);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
(void)memrchr(buf,c,43);
|
|
|
|
return memrchr(s,c,n);
|
|
|
|
}
|
|
|
|
|
2022-05-01 15:29:35 +02:00
|
|
|
void knownConditionTrueFalse_ffsl(long i)
|
|
|
|
{
|
|
|
|
// ffs() returns the position of the first bit set, or 0 if no bits are set in i.
|
|
|
|
const int x = ffsl(0);
|
|
|
|
// cppcheck-suppress knownConditionTrueFalse
|
|
|
|
if (x == 0) {}
|
|
|
|
if (ffsl(i) == 0) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
void knownConditionTrueFalse_ffsll(long long i)
|
|
|
|
{
|
|
|
|
// ffs() returns the position of the first bit set, or 0 if no bits are set in i.
|
|
|
|
const int x = ffsll(0);
|
|
|
|
// cppcheck-suppress knownConditionTrueFalse
|
|
|
|
if (x == 0) {}
|
|
|
|
if (ffsll(i) == 0) {}
|
|
|
|
}
|
|
|
|
|
2022-05-01 11:41:51 +02:00
|
|
|
int nullPointer_semtimedop(int semid, struct sembuf *sops, size_t nsops, const struct timespec *timeout)
|
|
|
|
{
|
|
|
|
(void) semtimedop(semid, sops, nsops, NULL); // If the timeout argument is NULL, then semtimedop() behaves exactly like semop().
|
|
|
|
(void) semtimedop(semid, sops, nsops, timeout);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
return semtimedop(semid, NULL, nsops, timeout);
|
|
|
|
}
|
|
|
|
|
2022-04-21 09:19:32 +02:00
|
|
|
void *nullPointer_mempcpy(void *dest, const void *src, size_t n)
|
|
|
|
{
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) mempcpy(NULL,src,n);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) mempcpy(dest,NULL,n);
|
|
|
|
return mempcpy(dest,src,n);
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t *nullPointer_wmempcpy(wchar_t *dest, const wchar_t *src, size_t n)
|
|
|
|
{
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) wmempcpy(NULL,src,n);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void) wmempcpy(dest,NULL,n);
|
|
|
|
return wmempcpy(dest,src,n);
|
|
|
|
}
|
|
|
|
|
2022-03-23 17:14:26 +01:00
|
|
|
int uninitvar_getpw(uid_t uid, char *buf)
|
|
|
|
{
|
|
|
|
uid_t someUid;
|
|
|
|
// cppcheck-suppress getpwCalled
|
|
|
|
(void)getpw(uid, buf);
|
|
|
|
// cppcheck-suppress getpwCalled
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
return getpw(someUid, buf);
|
|
|
|
}
|
|
|
|
|
2019-09-08 19:17:15 +02:00
|
|
|
// #9323, #9331
|
2019-09-10 11:51:47 +02:00
|
|
|
void syntaxError_timercmp(struct timeval t)
|
2019-09-08 19:17:15 +02:00
|
|
|
{
|
|
|
|
(void)timercmp(&t, &t, <);
|
|
|
|
(void)timercmp(&t, &t, <=);
|
|
|
|
(void)timercmp(&t, &t, ==);
|
|
|
|
(void)timercmp(&t, &t, !=);
|
|
|
|
(void)timercmp(&t, &t, >=);
|
|
|
|
(void)timercmp(&t, &t, >);
|
|
|
|
}
|
|
|
|
|
2019-09-10 11:51:47 +02:00
|
|
|
// False negative: #9346
|
|
|
|
void uninitvar_timercmp(struct timeval t)
|
|
|
|
{
|
|
|
|
struct timeval uninit;
|
|
|
|
(void)timercmp(&t, &uninit, <);
|
|
|
|
(void)timercmp(&uninit, &t, <=);
|
|
|
|
(void)timercmp(&uninit, &uninit, ==);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nullPointer_timercmp(struct timeval t)
|
|
|
|
{
|
2023-05-22 19:53:51 +02:00
|
|
|
// cppcheck-suppress constVariablePointer
|
2019-09-10 11:51:47 +02:00
|
|
|
struct timeval *p=0;
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void)timercmp(&t, p, <);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void)timercmp(p, &t, <=);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
(void)timercmp(p, p, ==);
|
|
|
|
}
|
|
|
|
|
2019-07-16 08:12:21 +02:00
|
|
|
// Declaration necessary because there is no specific / portable header.
|
|
|
|
extern void *xcalloc(size_t nmemb, size_t size);
|
|
|
|
extern void *xmalloc(size_t size);
|
|
|
|
extern void *xrealloc(void *block, size_t newsize);
|
2019-08-02 07:41:32 +02:00
|
|
|
extern void xfree(void *ptr);
|
2019-07-16 08:12:21 +02:00
|
|
|
|
2019-05-16 17:49:33 +02:00
|
|
|
void resourceLeak_mkostemps(char *template, int suffixlen, int flags)
|
|
|
|
{
|
2019-05-17 09:32:14 +02:00
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
int fp = mkostemps(template, suffixlen, flags);
|
|
|
|
// cppcheck-suppress resourceLeak
|
2019-05-16 17:49:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void no_resourceLeak_mkostemps_01(char *template, int suffixlen, int flags)
|
|
|
|
{
|
2019-05-17 09:32:14 +02:00
|
|
|
int fp = mkostemps(template, suffixlen, flags);
|
|
|
|
close(fp);
|
2019-05-16 17:49:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int no_resourceLeak_mkostemps_02(char *template, int suffixlen, int flags)
|
|
|
|
{
|
2019-05-17 09:32:14 +02:00
|
|
|
return mkostemps(template, suffixlen, flags);
|
2019-05-16 17:49:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void resourceLeak_mkstemps(char *template, int suffixlen)
|
|
|
|
{
|
2019-05-17 09:32:14 +02:00
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
int fp = mkstemps(template, suffixlen);
|
|
|
|
// cppcheck-suppress resourceLeak
|
2019-05-16 17:49:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void no_resourceLeak_mkstemps_01(char *template, int suffixlen)
|
|
|
|
{
|
2019-05-17 09:32:14 +02:00
|
|
|
int fp = mkstemps(template, suffixlen);
|
|
|
|
close(fp);
|
2019-05-16 17:49:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int no_resourceLeak_mkstemps_02(char *template, int suffixlen)
|
|
|
|
{
|
2019-05-17 09:32:14 +02:00
|
|
|
return mkstemps(template, suffixlen);
|
2019-05-16 17:49:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void resourceLeak_mkostemp(char *template, int flags)
|
|
|
|
{
|
2019-05-17 09:32:14 +02:00
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
int fp = mkostemp(template, flags);
|
|
|
|
// cppcheck-suppress resourceLeak
|
2019-05-16 17:49:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void no_resourceLeak_mkostemp_01(char *template, int flags)
|
|
|
|
{
|
2019-05-17 09:32:14 +02:00
|
|
|
int fp = mkostemp(template, flags);
|
|
|
|
close(fp);
|
2019-05-16 17:49:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int no_resourceLeak_mkostemp_02(char *template, int flags)
|
|
|
|
{
|
2019-05-17 09:32:14 +02:00
|
|
|
return mkostemp(template, flags);
|
2019-05-16 17:49:33 +02:00
|
|
|
}
|
2019-03-19 15:24:02 +01:00
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
void valid_code(int argInt1, va_list valist_arg, const int * parg)
|
2019-03-19 15:24:02 +01:00
|
|
|
{
|
2019-08-02 07:41:32 +02:00
|
|
|
char *p;
|
|
|
|
|
2019-03-19 15:24:02 +01:00
|
|
|
if (__builtin_expect(argInt1, 0)) {}
|
|
|
|
if (__builtin_expect_with_probability(argInt1 + 1, 2, 0.5)) {}
|
2019-11-27 08:04:55 +01:00
|
|
|
if (__glibc_unlikely(argInt1 != 0)) {}
|
2019-11-27 08:04:01 +01:00
|
|
|
if (__glibc_likely(parg != NULL)) {}
|
2023-08-05 18:48:43 +02:00
|
|
|
const void *ax1 = __builtin_assume_aligned(parg, 16);
|
2019-10-10 10:23:21 +02:00
|
|
|
printf("%p", ax1);
|
2023-08-05 18:48:43 +02:00
|
|
|
const void *ax2 = __builtin_assume_aligned(parg, 32, 8);
|
2019-10-10 10:23:21 +02:00
|
|
|
printf("%p", ax2);
|
2019-08-02 07:41:32 +02:00
|
|
|
|
|
|
|
p = (char *)malloc(10);
|
|
|
|
free(p);
|
|
|
|
p = (char *)malloc(5);
|
|
|
|
xfree(p);
|
|
|
|
p = (char *)xmalloc(10);
|
|
|
|
free(p);
|
|
|
|
p = (char *)xmalloc(5);
|
|
|
|
xfree(p);
|
2019-08-02 12:27:46 +02:00
|
|
|
|
|
|
|
// cppcheck-suppress allocaCalled
|
|
|
|
p = __builtin_alloca(5);
|
|
|
|
p[0] = 1;
|
|
|
|
// TODO cppcheck-suppress arrayIndexOutOfBounds
|
|
|
|
p[5] = 1;
|
|
|
|
__builtin_prefetch(p, 0, 1);
|
|
|
|
|
|
|
|
if (__builtin_types_compatible_p(int, char)) {}
|
2019-09-09 15:02:55 +02:00
|
|
|
|
|
|
|
char * pStr = NULL;
|
|
|
|
if (vasprintf(&pStr, "%d %d", valist_arg) != -1) {
|
|
|
|
free(pStr);
|
|
|
|
}
|
2019-09-19 12:27:21 +02:00
|
|
|
|
|
|
|
printf("%d", 0b010);
|
|
|
|
printf("%d", __extension__ 0b10001000);
|
2019-10-14 20:59:38 +02:00
|
|
|
|
|
|
|
if (__alignof__(int) == 4) {}
|
2019-10-30 09:39:50 +01:00
|
|
|
|
2023-08-05 18:48:43 +02:00
|
|
|
const void * p_mmap = mmap(NULL, 1, PROT_NONE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
|
2019-10-30 09:39:50 +01:00
|
|
|
printf("%p", p_mmap);
|
|
|
|
munmap(p_mmap, 1);
|
2021-05-03 10:45:37 +02:00
|
|
|
|
|
|
|
uint16_t i16_1 = 0, i16_2;
|
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
i16_2 = __builtin_bswap16(i16_1++);
|
|
|
|
uint32_t i32_1 = 0, i32_2;
|
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
i32_2 = __builtin_bswap32(i32_1++);
|
|
|
|
uint64_t i64_1 = 0, i64_2;
|
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
i64_2 = __builtin_bswap64(i64_1++);
|
2021-05-03 16:34:55 +02:00
|
|
|
|
2021-05-03 10:45:37 +02:00
|
|
|
// cppcheck-suppress zerodiv
|
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
i16_1 /= bswap_16(0x1234) - 0x3412;
|
|
|
|
// cppcheck-suppress zerodiv
|
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
i32_1 /= bswap_32(0x12345678) - 0x78563412;
|
|
|
|
// cppcheck-suppress zerodiv
|
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
i64_1 /= bswap_64(0x023456789abcde0f) - 0x0fdebc9a78563402;
|
2019-03-19 15:24:02 +01:00
|
|
|
}
|
|
|
|
|
2018-10-27 18:25:05 +02:00
|
|
|
void ignoreleak(void)
|
|
|
|
{
|
|
|
|
char *p = (char *)malloc(10);
|
|
|
|
__builtin_memset(&(p[0]), 0, 10);
|
2019-07-03 08:39:44 +02:00
|
|
|
// cppcheck-suppress memleak
|
2018-10-27 18:25:05 +02:00
|
|
|
}
|
|
|
|
|
2019-02-27 15:17:34 +01:00
|
|
|
void memleak_asprintf(char **ptr, const char *fmt, const int arg)
|
|
|
|
{
|
2019-02-27 15:38:31 +01:00
|
|
|
// No warning is expected for
|
2019-02-27 15:17:34 +01:00
|
|
|
if (-1 != asprintf(ptr,fmt,arg)) {
|
|
|
|
free(ptr);
|
|
|
|
}
|
2023-11-16 15:23:40 +01:00
|
|
|
if (-1 != asprintf(ptr,fmt,arg)) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
void memleak_asprintf2() { // #12186
|
|
|
|
char* p = malloc(5);
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
(void)asprintf(&p, "%s", "test");
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void memleak_asprintf3() {
|
|
|
|
char* p = malloc(5);
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
asprintf(&p, "%s", "test");
|
|
|
|
free(p);
|
2019-02-27 15:17:34 +01:00
|
|
|
}
|
|
|
|
|
2023-11-16 18:35:32 +01:00
|
|
|
void memleak_asprintf4(char** p) {
|
|
|
|
asprintf(p, "%s", "test");
|
|
|
|
}
|
|
|
|
|
|
|
|
void memleak_asprintf5(char* p) {
|
|
|
|
asprintf(&p, "%s", "test");
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
2023-11-19 19:51:32 +01:00
|
|
|
void memleak_asprintf6(const char* fmt, const int arg) {
|
|
|
|
char* ptr;
|
|
|
|
if (-1 == asprintf(&ptr, fmt, arg))
|
|
|
|
return;
|
|
|
|
printf("%s", ptr);
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void memleak_asprintf7(const char* fmt, const int arg) {
|
|
|
|
char* ptr;
|
|
|
|
if (asprintf(&ptr, fmt, arg) != -1) {
|
|
|
|
printf("%s", ptr);
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-02 07:41:32 +02:00
|
|
|
void memleak_xmalloc()
|
|
|
|
{
|
|
|
|
char *p = (char*)xmalloc(10);
|
|
|
|
p[9] = 0;
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
2019-10-30 09:39:50 +01:00
|
|
|
void memleak_mmap()
|
|
|
|
{
|
2023-08-05 18:48:43 +02:00
|
|
|
const void * p_mmap = mmap(NULL, 1, PROT_NONE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
|
2019-10-30 09:39:50 +01:00
|
|
|
printf("%p", p_mmap);
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
2018-10-27 18:25:05 +02:00
|
|
|
void uninitvar__builtin_memset(void)
|
|
|
|
{
|
|
|
|
void *s;
|
|
|
|
int c;
|
|
|
|
size_t n;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
(void)__builtin_memset(s,c,n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bufferAccessOutOfBounds__builtin_memset(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[42];
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
(void)__builtin_memset(buf,0,1000);
|
|
|
|
}
|
|
|
|
|
2018-07-18 09:40:06 +02:00
|
|
|
void bufferAccessOutOfBounds()
|
|
|
|
{
|
2023-08-05 18:48:43 +02:00
|
|
|
const char buf[2] = "a";
|
2018-07-18 09:40:06 +02:00
|
|
|
// This is valid
|
|
|
|
sethostname(buf, 2);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
sethostname(buf, 4);
|
2019-03-21 10:44:18 +01:00
|
|
|
|
|
|
|
char * pAlloc1 = xcalloc(2, 4);
|
|
|
|
memset(pAlloc1, 0, 8);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
memset(pAlloc1, 0, 9);
|
|
|
|
free(pAlloc1);
|
2019-07-16 08:12:21 +02:00
|
|
|
|
|
|
|
char * pAlloc2 = xmalloc(4);
|
|
|
|
memset(pAlloc2, 0, 4);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
memset(pAlloc2, 0, 5);
|
|
|
|
|
|
|
|
pAlloc2 = xrealloc(pAlloc2, 10);
|
|
|
|
memset(pAlloc2, 0, 10);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
memset(pAlloc2, 0, 11);
|
|
|
|
|
|
|
|
free(pAlloc2);
|
2018-07-18 09:40:06 +02:00
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void leakReturnValNotUsed()
|
|
|
|
{
|
2023-05-22 19:53:51 +02:00
|
|
|
// cppcheck-suppress [unreadVariable, constVariablePointer]
|
2015-08-14 01:36:44 +02:00
|
|
|
char* ptr = (char*)strdupa("test");
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
strdupa("test");
|
2023-05-22 19:53:51 +02:00
|
|
|
// cppcheck-suppress [unreadVariable, constVariablePointer]
|
2015-08-14 01:36:44 +02:00
|
|
|
char* ptr2 = (char*)strndupa("test", 1);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
strndupa("test", 1);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strcasestr("test", NULL);
|
|
|
|
|
2019-01-25 17:03:16 +01:00
|
|
|
// FIXME cppcheck-suppress knownConditionTrueFalse
|
2016-07-20 12:21:00 +02:00
|
|
|
// cppcheck-suppress duplicateExpression
|
2015-08-14 01:36:44 +02:00
|
|
|
if (42 == __builtin_expect(42, 0))
|
|
|
|
return;
|
2015-02-16 22:06:08 +01:00
|
|
|
}
|
2018-09-25 09:07:49 +02:00
|
|
|
|
2023-03-02 20:54:30 +01:00
|
|
|
#if !defined(__CYGWIN__) && !(defined(__APPLE__) && defined(__MACH__))
|
2018-09-25 09:07:49 +02:00
|
|
|
int nullPointer_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
|
|
|
|
{
|
|
|
|
// no warning is expected
|
|
|
|
(void)epoll_ctl(epfd, op, fd, event);
|
|
|
|
|
|
|
|
// No nullpointer warning is expected in case op is set to EPOLL_CTL_DEL
|
|
|
|
// EPOLL_CTL_DEL
|
|
|
|
// Remove (deregister) the target file descriptor fd from the
|
|
|
|
// epoll instance referred to by epfd. The event is ignored and
|
|
|
|
// can be NULL.
|
|
|
|
return epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
|
|
|
|
}
|
2018-09-29 16:49:12 +02:00
|
|
|
#endif
|