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/*.h
$ASTYLE $style $options test/*.cpp
$ASTYLE $style $options test/cfg/*.c*
$ASTYLE $style $options test/*.h
$ASTYLE $style $options tools/*.cpp

View File

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

View File

@ -19,7 +19,8 @@
#include <time.h>
#include <unistd.h>
void bufferAccessOutOfBounds(int fd) {
void bufferAccessOutOfBounds(int fd)
{
char a[5];
read(fd,a,5);
// cppcheck-suppress bufferAccessOutOfBounds
@ -43,25 +44,28 @@ void bufferAccessOutOfBounds(int fd) {
0;
}
void nullPointer(char *p) {
void nullPointer(char *p)
{
// cppcheck-suppress ignoredReturnValue
isatty (0);
mkdir (p, 0);
getcwd (0, 0);
isatty(0);
mkdir(p, 0);
getcwd(0, 0);
// cppcheck-suppress nullPointer
readdir (0);
readdir(0);
// cppcheck-suppress nullPointer
utime(NULL, NULL);
}
void memleak_getaddrinfo() {
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) {
void memleak_mmap(int fd)
{
// cppcheck-suppress unreadVariable
void *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);
// 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
DIR* leak1 = fdopendir(fd);
// cppcheck-suppress resourceLeak
}
void resourceLeak_opendir(void) {
void resourceLeak_opendir(void)
{
// cppcheck-suppress unreadVariable
DIR* leak1 = opendir("abc");
// cppcheck-suppress resourceLeak
}
void resourceLeak_socket(void) {
void resourceLeak_socket(void)
{
// cppcheck-suppress unreadVariable
int s = socket(AF_INET, SOCK_STREAM, 0);
// 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);
/* TODO: add configuration for open/fdopen
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);
/* TODO: add configuration for open/fdopen
// #2830
int fd = open("path", O_RDONLY);
FILE *f = fdopen(fd, "rt");
fclose(f);
*/
*/
}
// unused return value
void ignoredReturnValue(void *addr, int fd) {
void ignoredReturnValue(void *addr, int fd)
{
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
mmap(addr, 255, PROT_NONE, MAP_PRIVATE, fd, 0);
@ -120,7 +132,8 @@ void ignoredReturnValue(void *addr, int fd) {
// valid range
void invalidFunctionArg() {
void invalidFunctionArg()
{
// cppcheck-suppress invalidFunctionArg
usleep(-1);
usleep(0);
@ -129,7 +142,8 @@ void invalidFunctionArg() {
usleep(1000000);
}
void uninitvar(int fd) {
void uninitvar(int fd)
{
int x;
char buf[2];
int decimal, sign;
@ -151,7 +165,7 @@ void uninitvar(int fd) {
pattern="";
// cppcheck-suppress uninitvar
regcomp(&reg, pattern, cflags);
regerror (0, &reg, 0, 0);
regerror(0, &reg, 0, 0);
// cppcheck-suppress uninitvar
// cppcheck-suppress unreadVariable
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
blkcnt_t b;
// cppcheck-suppress uninitvar
@ -178,7 +193,8 @@ void uninitvar_types(void) {
d.d_ino + 1;
}
void timet_h() {
void timet_h()
{
struct timespec* ptp;
// cppcheck-suppress uninitvar
clock_settime(CLOCK_REALTIME, ptp);

View File

@ -10,7 +10,7 @@ else # assume we are in repo root
fi
# 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
CXX=g++

View File

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

View File

@ -10,8 +10,11 @@
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cctype>
void bufferAccessOutOf(void) {
void bufferAccessOutOfBounds(void)
{
char a[5];
std::strcpy(a,"abcd");
// cppcheck-suppress bufferAccessOutOfBounds
@ -23,3 +26,36 @@ void bufferAccessOutOf(void) {
// cppcheck-suppress redundantCopy
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)
OpenFile(file,0,0); // <- not the windows OpenFile function
}