test/cfg: Improved testing of functions from standard namespace. Added test/cfg/*.c* files to astyle script and formatted code.

This commit is contained in:
Martin Ettl 2015-08-14 01:36:44 +02:00
parent 3ab6c5aa85
commit 77869b7812
7 changed files with 331 additions and 251 deletions

View File

@ -29,6 +29,7 @@ $ASTYLE $style $options -r gui/test/*.h
$ASTYLE $style $options lib/*.cpp $ASTYLE $style $options lib/*.cpp
$ASTYLE $style $options lib/*.h $ASTYLE $style $options lib/*.h
$ASTYLE $style $options test/*.cpp $ASTYLE $style $options test/*.cpp
$ASTYLE $style $options test/cfg/*.c*
$ASTYLE $style $options test/*.h $ASTYLE $style $options test/*.h
$ASTYLE $style $options tools/*.cpp $ASTYLE $style $options tools/*.cpp

View File

@ -9,7 +9,8 @@
#include <string.h> #include <string.h>
void leakReturnValNotUsed() { void leakReturnValNotUsed()
{
// cppcheck-suppress unreadVariable // cppcheck-suppress unreadVariable
char* ptr = (char*)strdupa("test"); char* ptr = (char*)strdupa("test");
// cppcheck-suppress ignoredReturnValue // cppcheck-suppress ignoredReturnValue

View File

@ -19,7 +19,8 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
void bufferAccessOutOfBounds(int fd) { void bufferAccessOutOfBounds(int fd)
{
char a[5]; char a[5];
read(fd,a,5); read(fd,a,5);
// cppcheck-suppress bufferAccessOutOfBounds // cppcheck-suppress bufferAccessOutOfBounds
@ -43,25 +44,28 @@ void bufferAccessOutOfBounds(int fd) {
0; 0;
} }
void nullPointer(char *p) { void nullPointer(char *p)
{
// cppcheck-suppress ignoredReturnValue // cppcheck-suppress ignoredReturnValue
isatty (0); isatty(0);
mkdir (p, 0); mkdir(p, 0);
getcwd (0, 0); getcwd(0, 0);
// cppcheck-suppress nullPointer // cppcheck-suppress nullPointer
readdir (0); readdir(0);
// cppcheck-suppress nullPointer // cppcheck-suppress nullPointer
utime(NULL, NULL); utime(NULL, NULL);
} }
void memleak_getaddrinfo() { void memleak_getaddrinfo()
{
//TODO: nothing to report yet, see http://sourceforge.net/p/cppcheck/discussion/general/thread/d9737d5d/ //TODO: nothing to report yet, see http://sourceforge.net/p/cppcheck/discussion/general/thread/d9737d5d/
struct addrinfo * res=NULL; struct addrinfo * res=NULL;
getaddrinfo("node", NULL, NULL, &res); getaddrinfo("node", NULL, NULL, &res);
freeaddrinfo(res); freeaddrinfo(res);
} }
void memleak_mmap(int fd) { void memleak_mmap(int fd)
{
// cppcheck-suppress unreadVariable // cppcheck-suppress unreadVariable
void *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0); void *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);
// cppcheck-suppress memleak // cppcheck-suppress memleak
@ -74,40 +78,48 @@ void resourceLeak_fdopen(int fd) {
} }
*/ */
void resourceLeak_fdopendir(int fd) { void resourceLeak_fdopendir(int fd)
{
// cppcheck-suppress unreadVariable // cppcheck-suppress unreadVariable
DIR* leak1 = fdopendir(fd); DIR* leak1 = fdopendir(fd);
// cppcheck-suppress resourceLeak // cppcheck-suppress resourceLeak
} }
void resourceLeak_opendir(void) { void resourceLeak_opendir(void)
{
// cppcheck-suppress unreadVariable // cppcheck-suppress unreadVariable
DIR* leak1 = opendir("abc"); DIR* leak1 = opendir("abc");
// cppcheck-suppress resourceLeak // cppcheck-suppress resourceLeak
} }
void resourceLeak_socket(void) { void resourceLeak_socket(void)
{
// cppcheck-suppress unreadVariable // cppcheck-suppress unreadVariable
int s = socket(AF_INET, SOCK_STREAM, 0); int s = socket(AF_INET, SOCK_STREAM, 0);
// cppcheck-suppress resourceLeak // cppcheck-suppress resourceLeak
} }
void noleak(int x, int y, int z) { void noleak(int x, int y, int z)
DIR *p1 = fdopendir(x); closedir(p1); {
DIR *p2 = opendir("abc"); closedir(p2); DIR *p1 = fdopendir(x);
int s = socket(AF_INET,SOCK_STREAM,0); close(s); closedir(p1);
/* TODO: add configuration for open/fdopen DIR *p2 = opendir("abc");
closedir(p2);
int s = socket(AF_INET,SOCK_STREAM,0);
close(s);
/* TODO: add configuration for open/fdopen
// #2830 // #2830
int fd = open("path", O_RDONLY); int fd = open("path", O_RDONLY);
FILE *f = fdopen(fd, "rt"); FILE *f = fdopen(fd, "rt");
fclose(f); fclose(f);
*/ */
} }
// unused return value // unused return value
void ignoredReturnValue(void *addr, int fd) { void ignoredReturnValue(void *addr, int fd)
{
// cppcheck-suppress ignoredReturnValue // cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed // cppcheck-suppress leakReturnValNotUsed
mmap(addr, 255, PROT_NONE, MAP_PRIVATE, fd, 0); mmap(addr, 255, PROT_NONE, MAP_PRIVATE, fd, 0);
@ -120,7 +132,8 @@ void ignoredReturnValue(void *addr, int fd) {
// valid range // valid range
void invalidFunctionArg() { void invalidFunctionArg()
{
// cppcheck-suppress invalidFunctionArg // cppcheck-suppress invalidFunctionArg
usleep(-1); usleep(-1);
usleep(0); usleep(0);
@ -129,7 +142,8 @@ void invalidFunctionArg() {
usleep(1000000); usleep(1000000);
} }
void uninitvar(int fd) { void uninitvar(int fd)
{
int x; int x;
char buf[2]; char buf[2];
int decimal, sign; int decimal, sign;
@ -151,7 +165,7 @@ void uninitvar(int fd) {
pattern=""; pattern="";
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
regcomp(&reg, pattern, cflags); regcomp(&reg, pattern, cflags);
regerror (0, &reg, 0, 0); regerror(0, &reg, 0, 0);
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
// cppcheck-suppress unreadVariable // cppcheck-suppress unreadVariable
char *buffer = ecvt(d, 11, &decimal, &sign); char *buffer = ecvt(d, 11, &decimal, &sign);
@ -167,7 +181,8 @@ void uninitvar(int fd) {
} }
void uninitvar_types(void) { void uninitvar_types(void)
{
// cppcheck-suppress unassignedVariable // cppcheck-suppress unassignedVariable
blkcnt_t b; blkcnt_t b;
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
@ -178,7 +193,8 @@ void uninitvar_types(void) {
d.d_ino + 1; d.d_ino + 1;
} }
void timet_h() { void timet_h()
{
struct timespec* ptp; struct timespec* ptp;
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
clock_settime(CLOCK_REALTIME, ptp); clock_settime(CLOCK_REALTIME, ptp);

View File

@ -10,7 +10,7 @@ else # assume we are in repo root
fi fi
# Cppcheck options # Cppcheck options
CPPCHECK_OPT='--check-library --enable=information --enable=style --error-exitcode=1 --suppress=missingIncludeSystem --inline-suppr' CPPCHECK_OPT='--check-library --enable=information --enable=style --error-exitcode=1 --suppress=missingIncludeSystem --inline-suppr --template="{file}:{line}:{severity}:{id}:{message}"'
# Compiler settings # Compiler settings
CXX=g++ CXX=g++

View File

@ -12,7 +12,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <tgmath.h> // frexp #include <tgmath.h> // frexp
void bufferAccessOutOf(void) { void bufferAccessOutOfBounds(void)
{
char a[5]; char a[5];
fgets(a,5,stdin); fgets(a,5,stdin);
// cppcheck-suppress bufferAccessOutOfBounds // cppcheck-suppress bufferAccessOutOfBounds
@ -45,7 +46,8 @@ void bufferAccessOutOf(void) {
// memory leak // memory leak
void ignoreleak(void) { void ignoreleak(void)
{
char *p = (char *)malloc(10); char *p = (char *)malloc(10);
memset(&(p[0]), 0, 10); memset(&(p[0]), 0, 10);
// cppcheck-suppress memleak // cppcheck-suppress memleak
@ -53,7 +55,8 @@ void ignoreleak(void) {
// null pointer // null pointer
void nullpointer(int value){ void nullpointer(int value)
{
int res = 0; int res = 0;
FILE *fp; FILE *fp;
@ -75,13 +78,15 @@ void nullpointer(int value){
puts(0); puts(0);
// cppcheck-suppress nullPointer // cppcheck-suppress nullPointer
fp=fopen(0,0); fp=fopen(0,0);
fclose(fp); fp = 0; fclose(fp);
fp = 0;
// No FP // No FP
fflush(0); fflush(0);
// No FP // No FP
// cppcheck-suppress redundantAssignment // cppcheck-suppress redundantAssignment
fp = freopen(0,"abc",stdin); fp = freopen(0,"abc",stdin);
fclose(fp); fp = 0; fclose(fp);
fp = 0;
// cppcheck-suppress nullPointer // cppcheck-suppress nullPointer
fputc(0,0); fputc(0,0);
// cppcheck-suppress nullPointer // cppcheck-suppress nullPointer
@ -150,24 +155,28 @@ void nullpointer(int value){
snprintf(NULL, 42, "someformatstring"); // not legal snprintf(NULL, 42, "someformatstring"); // not legal
} }
void nullpointerMemchr1(char *p, char *s) { void nullpointerMemchr1(char *p, char *s)
{
// cppcheck-suppress uselessAssignmentPtrArg // cppcheck-suppress uselessAssignmentPtrArg
p = memchr (s, 'p', strlen(s)); p = memchr(s, 'p', strlen(s));
} }
void nullpointerMemchr2(char *p, char *s) { void nullpointerMemchr2(char *p, char *s)
{
// cppcheck-suppress uselessAssignmentPtrArg // cppcheck-suppress uselessAssignmentPtrArg
p = memchr (s, 0, strlen(s)); p = memchr(s, 0, strlen(s));
} }
void nullpointerMemchr3(char *p) { void nullpointerMemchr3(char *p)
{
char *s = 0; char *s = 0;
// cppcheck-suppress nullPointer // cppcheck-suppress nullPointer
// cppcheck-suppress uselessAssignmentPtrArg // cppcheck-suppress uselessAssignmentPtrArg
p = memchr (s, 0, strlen(s)); p = memchr(s, 0, strlen(s));
} }
void nullpointerMemcmp(char *p) { void nullpointerMemcmp(char *p)
{
// cppcheck-suppress ignoredReturnValue // cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer // cppcheck-suppress nullPointer
memcmp(p, 0, 123); memcmp(p, 0, 123);
@ -176,19 +185,22 @@ void nullpointerMemcmp(char *p) {
// uninit pointers // uninit pointers
void uninit_clearerr(void) { void uninit_clearerr(void)
{
FILE *fp; FILE *fp;
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
clearerr(fp); clearerr(fp);
} }
void uninit_fclose(void) { void uninit_fclose(void)
{
FILE *fp; FILE *fp;
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
fclose(fp); fclose(fp);
} }
void uninit_fopen(void) { void uninit_fopen(void)
{
const char *filename, *mode; const char *filename, *mode;
FILE *fp; FILE *fp;
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
@ -199,33 +211,38 @@ void uninit_fopen(void) {
fclose(fp); fclose(fp);
} }
void uninit_feof(void) { void uninit_feof(void)
{
FILE *fp; FILE *fp;
// cppcheck-suppress ignoredReturnValue // cppcheck-suppress ignoredReturnValue
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
feof(fp); feof(fp);
} }
void uninit_ferror(void) { void uninit_ferror(void)
{
FILE *fp; FILE *fp;
// cppcheck-suppress ignoredReturnValue // cppcheck-suppress ignoredReturnValue
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
ferror(fp); ferror(fp);
} }
void uninit_fflush(void) { void uninit_fflush(void)
{
FILE *fp; FILE *fp;
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
fflush(fp); fflush(fp);
} }
void uninit_fgetc(void) { void uninit_fgetc(void)
{
FILE *fp; FILE *fp;
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
fgetc(fp); fgetc(fp);
} }
void uninit_fgetpos(void) { void uninit_fgetpos(void)
{
FILE *fp; FILE *fp;
fpos_t pos; fpos_t pos;
fpos_t *ppos; fpos_t *ppos;
@ -238,7 +255,8 @@ void uninit_fgetpos(void) {
fclose(fp); fclose(fp);
} }
void uninit_fsetpos(void) { void uninit_fsetpos(void)
{
FILE *fp; FILE *fp;
fpos_t pos; fpos_t pos;
fpos_t *ppos; fpos_t *ppos;
@ -251,7 +269,8 @@ void uninit_fsetpos(void) {
fclose(fp); fclose(fp);
} }
void uninit_fgets(void) { void uninit_fgets(void)
{
FILE *fp; FILE *fp;
char buf[10]; char buf[10];
char *str; char *str;
@ -265,7 +284,8 @@ void uninit_fgets(void) {
fgets(buf,10,fp); fgets(buf,10,fp);
} }
void uninit_fputc(void) { void uninit_fputc(void)
{
int i; int i;
FILE *fp; FILE *fp;
@ -278,7 +298,8 @@ void uninit_fputc(void) {
fputc('a', fp); fputc('a', fp);
} }
void uninit_fputs(void) { void uninit_fputs(void)
{
const char *s; const char *s;
FILE *fp; FILE *fp;
@ -291,26 +312,30 @@ void uninit_fputs(void) {
fputs("a", fp); fputs("a", fp);
} }
void uninit_ftell(void) { void uninit_ftell(void)
{
FILE *fp; FILE *fp;
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
ftell(fp); ftell(fp);
} }
void uninit_puts(void) { void uninit_puts(void)
{
const char *s; const char *s;
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
puts(s); puts(s);
} }
void uninit_putchar(void) { void uninit_putchar(void)
{
char c; char c;
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
putchar(c); putchar(c);
} }
void ignoreretrn(void) { void ignoreretrn(void)
{
char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff"; char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
char * pEnd; char * pEnd;
strtol (szNumbers,&pEnd,10); strtol(szNumbers,&pEnd,10);
} }

View File

@ -10,8 +10,11 @@
#include <cstring> #include <cstring>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <ctime>
#include <cctype>
void bufferAccessOutOf(void) { void bufferAccessOutOfBounds(void)
{
char a[5]; char a[5];
std::strcpy(a,"abcd"); std::strcpy(a,"abcd");
// cppcheck-suppress bufferAccessOutOfBounds // cppcheck-suppress bufferAccessOutOfBounds
@ -23,3 +26,36 @@ void bufferAccessOutOf(void) {
// cppcheck-suppress redundantCopy // cppcheck-suppress redundantCopy
std::strncpy(a,"abcde",6); std::strncpy(a,"abcde",6);
} }
void uninitvar(void)
{
int i;
// cppcheck-suppress uninitvar
std::abs(i);
// cppcheck-suppress uninitvar
std::isalnum(i);
// cppcheck-suppress uninitvar
std::isalpha(i);
// cppcheck-suppress uninitvar
std::iscntrl(i);
// cppcheck-suppress uninitvar
std::isdigit(i);
// cppcheck-suppress uninitvar
std::isgraph(i);
// cppcheck-suppress uninitvar
std::islower(i);
// cppcheck-suppress uninitvar
std::isprint(i);
// cppcheck-suppress uninitvar
std::isspace(i);
// cppcheck-suppress uninitvar
std::isupper(i);
// cppcheck-suppress uninitvar
std::isxdigit(i);
const struct tm *tm;
// cppcheck-suppress uninitvar
// cppcheck-suppress obsoleteFunctionsasctime
std::asctime(tm);
}

View File

@ -12,7 +12,8 @@ class CSharedFilesCtrl {
}; };
void CSharedFilesCtrl::OnNmDblClk(NMHDR* /*pNMHDR*/, LRESULT* pResult) { void CSharedFilesCtrl::OnNmDblClk(NMHDR* /*pNMHDR*/, LRESULT* pResult)
{
if (file) if (file)
OpenFile(file,0,0); // <- not the windows OpenFile function OpenFile(file,0,0); // <- not the windows OpenFile function
} }