2015-02-16 22:06:08 +01:00
|
|
|
|
|
|
|
// Test library configuration for gnu.cfg
|
|
|
|
//
|
|
|
|
// Usage:
|
2015-02-16 22:19:51 +01:00
|
|
|
// $ cppcheck --check-library --library=gnu --enable=information --enable=style --error-exitcode=1 --suppress=missingIncludeSystem --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>
|
2018-09-29 16:49:12 +02:00
|
|
|
#ifndef __CYGWIN__
|
2018-09-25 09:07:49 +02:00
|
|
|
#include <sys/epoll.h>
|
2018-09-29 16:49:12 +02:00
|
|
|
#endif
|
2015-02-16 22:06:08 +01:00
|
|
|
|
2019-03-19 15:24:02 +01:00
|
|
|
|
|
|
|
void valid_code(int argInt1)
|
|
|
|
{
|
|
|
|
if (__builtin_expect(argInt1, 0)) {}
|
|
|
|
if (__builtin_expect_with_probability(argInt1 + 1, 2, 0.5)) {}
|
|
|
|
}
|
|
|
|
|
2018-10-27 18:25:05 +02:00
|
|
|
void ignoreleak(void)
|
|
|
|
{
|
|
|
|
char *p = (char *)malloc(10);
|
|
|
|
__builtin_memset(&(p[0]), 0, 10);
|
2019-03-16 18:17:16 +01:00
|
|
|
// TODO // 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);
|
|
|
|
}
|
|
|
|
if (-1 != asprintf(ptr,fmt,arg)) {
|
|
|
|
// TODO: Related to #8980 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()
|
|
|
|
{
|
2019-03-14 09:26:27 +01:00
|
|
|
char buf[2] = "a";
|
2018-07-18 09:40:06 +02:00
|
|
|
// This is valid
|
|
|
|
sethostname(buf, 2);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
sethostname(buf, 4);
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void leakReturnValNotUsed()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
char* ptr = (char*)strdupa("test");
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
strdupa("test");
|
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
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
|
|
|
|
2018-09-29 16:49:12 +02:00
|
|
|
#ifndef __CYGWIN__
|
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
|