2015-01-27 17:55:18 +01:00
|
|
|
|
|
|
|
// Test library configuration for std.cfg
|
|
|
|
//
|
|
|
|
// Usage:
|
2015-02-16 22:19:51 +01:00
|
|
|
// $ cppcheck --check-library --enable=information --error-exitcode=1 --suppress=missingIncludeSystem --inline-suppr test/cfg/std.c
|
2015-01-27 17:55:18 +01:00
|
|
|
// =>
|
|
|
|
// No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <string.h>
|
2015-01-28 16:22:45 +01:00
|
|
|
#include <stdio.h>
|
2015-02-15 15:56:05 +01:00
|
|
|
#include <stdlib.h>
|
2015-01-29 06:23:41 +01:00
|
|
|
#include <tgmath.h> // frexp
|
2015-01-27 17:55:18 +01:00
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void bufferAccessOutOfBounds(void)
|
|
|
|
{
|
|
|
|
char a[5];
|
|
|
|
fgets(a,5,stdin);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
fgets(a,6,stdin);
|
|
|
|
sprintf(a, "ab%s", "cd");
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
// cppcheck-suppress redundantCopy
|
|
|
|
sprintf(a, "ab%s", "cde");
|
|
|
|
// cppcheck-suppress redundantCopy
|
|
|
|
snprintf(a, 5, "abcde%i", 1);
|
|
|
|
// cppcheck-suppress redundantCopy
|
|
|
|
snprintf(a, 6, "abcde%i", 1); //TODO: cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
// cppcheck-suppress redundantCopy
|
|
|
|
strcpy(a,"abcd");
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
// cppcheck-suppress redundantCopy
|
|
|
|
strcpy(a, "abcde");
|
|
|
|
// cppcheck-suppress redundantCopy
|
|
|
|
strncpy(a,"abcde",5);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
// cppcheck-suppress redundantCopy
|
|
|
|
strncpy(a,"abcde",6);
|
|
|
|
fread(a,1,5,stdin);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
fread(a,1,6,stdin);
|
|
|
|
fwrite(a,1,5,stdout);
|
|
|
|
// cppcheck-suppress bufferAccessOutOfBounds
|
|
|
|
fread(a,1,6,stdout);
|
2015-02-09 16:20:28 +01:00
|
|
|
}
|
|
|
|
|
2015-02-15 15:56:05 +01:00
|
|
|
// memory leak
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void ignoreleak(void)
|
|
|
|
{
|
2015-02-15 15:56:05 +01:00
|
|
|
char *p = (char *)malloc(10);
|
|
|
|
memset(&(p[0]), 0, 10);
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
2015-01-28 16:22:45 +01:00
|
|
|
// null pointer
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void nullpointer(int value)
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
clearerr(0);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
feof(0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
fgetc(0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
fclose(0);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
ferror(0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
ftell(0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
puts(0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
fp=fopen(0,0);
|
|
|
|
fclose(fp);
|
|
|
|
fp = 0;
|
|
|
|
// No FP
|
|
|
|
fflush(0);
|
|
|
|
// No FP
|
|
|
|
// cppcheck-suppress redundantAssignment
|
|
|
|
fp = freopen(0,"abc",stdin);
|
|
|
|
fclose(fp);
|
|
|
|
fp = 0;
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
fputc(0,0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
fputs(0,0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
fgetpos(0,0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
frexp(1.0,0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
fsetpos(0,0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
itoa(123,0,10);
|
|
|
|
putchar(0);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strchr(0,0);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strlen(0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strcpy(0,0);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strspn(0,0);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strcspn(0,0);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strcoll(0,0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strcat(0,0);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strcmp(0,0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strncpy(0,0,1);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strncat(0,0,1);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strncmp(0,0,1);
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strstr(0,0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strtoul(0,0,0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strtoull(0,0,0);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
strtol(0,0,0);
|
|
|
|
|
|
|
|
// #6100 False positive nullPointer - calling mbstowcs(NULL,)
|
|
|
|
res += mbstowcs(0,"",0);
|
|
|
|
// cppcheck-suppress unreadVariable
|
|
|
|
res += wcstombs(0,L"",0);
|
|
|
|
|
|
|
|
strtok(NULL,"xyz");
|
|
|
|
|
|
|
|
strxfrm(0,"foo",0);
|
|
|
|
// TODO: error message
|
|
|
|
strxfrm(0,"foo",42);
|
|
|
|
|
|
|
|
snprintf(NULL, 0, "someformatstring"); // legal
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
snprintf(NULL, 42, "someformatstring"); // not legal
|
2015-01-28 16:22:45 +01:00
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void nullpointerMemchr1(char *p, char *s)
|
|
|
|
{
|
|
|
|
// cppcheck-suppress uselessAssignmentPtrArg
|
|
|
|
p = memchr(s, 'p', strlen(s));
|
2015-01-28 16:22:45 +01:00
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void nullpointerMemchr2(char *p, char *s)
|
|
|
|
{
|
|
|
|
// cppcheck-suppress uselessAssignmentPtrArg
|
|
|
|
p = memchr(s, 0, strlen(s));
|
2015-01-28 16:22:45 +01:00
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void nullpointerMemchr3(char *p)
|
|
|
|
{
|
|
|
|
char *s = 0;
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
// cppcheck-suppress uselessAssignmentPtrArg
|
|
|
|
p = memchr(s, 0, strlen(s));
|
2015-01-28 16:22:45 +01:00
|
|
|
}
|
2015-01-29 06:07:48 +01:00
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void nullpointerMemcmp(char *p)
|
|
|
|
{
|
|
|
|
// cppcheck-suppress ignoredReturnValue
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
memcmp(p, 0, 123);
|
2015-01-29 06:07:48 +01:00
|
|
|
}
|
2015-01-30 16:35:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
// uninit pointers
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_clearerr(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
FILE *fp;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
clearerr(fp);
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_fclose(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
FILE *fp;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_fopen(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
const char *filename, *mode;
|
|
|
|
FILE *fp;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fp = fopen(filename, "rt");
|
|
|
|
fclose(fp);
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fp = fopen("filename.txt", mode);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_feof(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
FILE *fp;
|
2015-02-25 22:03:10 +01:00
|
|
|
// cppcheck-suppress ignoredReturnValue
|
2015-01-30 16:35:17 +01:00
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
feof(fp);
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_ferror(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
FILE *fp;
|
2015-02-25 22:03:10 +01:00
|
|
|
// cppcheck-suppress ignoredReturnValue
|
2015-01-30 16:35:17 +01:00
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
ferror(fp);
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_fflush(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
FILE *fp;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fflush(fp);
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_fgetc(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
FILE *fp;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fgetc(fp);
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_fgetpos(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
FILE *fp;
|
|
|
|
fpos_t pos;
|
|
|
|
fpos_t *ppos;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fgetpos(fp,&pos);
|
|
|
|
|
|
|
|
fp = fopen("filename","rt");
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fgetpos(fp,ppos);
|
2015-02-09 16:20:28 +01:00
|
|
|
fclose(fp);
|
2015-01-30 16:35:17 +01:00
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_fsetpos(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
FILE *fp;
|
|
|
|
fpos_t pos;
|
|
|
|
fpos_t *ppos;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fsetpos(fp,&pos);
|
|
|
|
|
|
|
|
fp = fopen("filename","rt");
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fsetpos(fp,ppos);
|
2015-02-09 16:20:28 +01:00
|
|
|
fclose(fp);
|
2015-01-30 16:35:17 +01:00
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_fgets(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
FILE *fp;
|
|
|
|
char buf[10];
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
fgets(buf,10,stdin);
|
|
|
|
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fgets(str,10,stdin);
|
|
|
|
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fgets(buf,10,fp);
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_fputc(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
int i;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fputc('a', stdout);
|
|
|
|
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fputc(i, stdout);
|
|
|
|
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fputc('a', fp);
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_fputs(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
const char *s;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fputs("a", stdout);
|
|
|
|
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fputs(s, stdout);
|
|
|
|
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
fputs("a", fp);
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_ftell(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
FILE *fp;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
ftell(fp);
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_puts(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
const char *s;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
puts(s);
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void uninit_putchar(void)
|
|
|
|
{
|
2015-01-30 16:35:17 +01:00
|
|
|
char c;
|
|
|
|
// cppcheck-suppress uninitvar
|
|
|
|
putchar(c);
|
|
|
|
}
|
2015-02-25 22:03:10 +01:00
|
|
|
|
2015-08-14 01:36:44 +02:00
|
|
|
void ignoreretrn(void)
|
|
|
|
{
|
|
|
|
char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
|
|
|
|
char * pEnd;
|
|
|
|
strtol(szNumbers,&pEnd,10);
|
2015-02-25 22:03:10 +01:00
|
|
|
}
|