Moved cfg tests from TestUninitVar

This commit is contained in:
Daniel Marjamäki 2015-01-30 16:35:17 +01:00
parent 355a2d38b3
commit 6b20172a36
2 changed files with 134 additions and 294 deletions

View File

@ -123,3 +123,137 @@ void nullpointerMemcmp(char *p) {
// cppcheck-suppress nullPointer
memcmp(p, 0, 123);
}
// uninit pointers
void uninit_clearerr(void) {
FILE *fp;
// cppcheck-suppress uninitvar
clearerr(fp);
}
void uninit_fclose(void) {
FILE *fp;
// cppcheck-suppress uninitvar
fclose(fp);
}
void uninit_fopen(void) {
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);
}
void uninit_feof(void) {
FILE *fp;
// cppcheck-suppress uninitvar
feof(fp);
}
void uninit_ferror(void) {
FILE *fp;
// cppcheck-suppress uninitvar
ferror(fp);
}
void uninit_fflush(void) {
FILE *fp;
// cppcheck-suppress uninitvar
fflush(fp);
}
void uninit_fgetc(void) {
FILE *fp;
// cppcheck-suppress uninitvar
fgetc(fp);
}
void uninit_fgetpos(void) {
FILE *fp;
fpos_t pos;
fpos_t *ppos;
// cppcheck-suppress uninitvar
fgetpos(fp,&pos);
fp = fopen("filename","rt");
// cppcheck-suppress uninitvar
fgetpos(fp,ppos);
fclose(fp);
}
void uninit_fsetpos(void) {
FILE *fp;
fpos_t pos;
fpos_t *ppos;
// cppcheck-suppress uninitvar
fsetpos(fp,&pos);
fp = fopen("filename","rt");
// cppcheck-suppress uninitvar
fsetpos(fp,ppos);
fclose(fp);
}
void uninit_fgets(void) {
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);
}
void uninit_fputc(void) {
int i;
FILE *fp;
fputc('a', stdout);
// cppcheck-suppress uninitvar
fputc(i, stdout);
// cppcheck-suppress uninitvar
fputc('a', fp);
}
void uninit_fputs(void) {
const char *s;
FILE *fp;
fputs("a", stdout);
// cppcheck-suppress uninitvar
fputs(s, stdout);
// cppcheck-suppress uninitvar
fputs("a", fp);
}
void uninit_ftell(void) {
FILE *fp;
// cppcheck-suppress uninitvar
ftell(fp);
}
void uninit_puts(void) {
const char *s;
// cppcheck-suppress uninitvar
puts(s);
}
void uninit_putchar(void) {
char c;
// cppcheck-suppress uninitvar
putchar(c);
}

View File

@ -74,25 +74,6 @@ private:
TEST_CASE(syntax_error); // Ticket #5073
// Test that the functions from std.cfg are configured correctly
TEST_CASE(stdcfg_clearerr);
TEST_CASE(stdcfg_fclose);
TEST_CASE(stdcfg_fopen);
TEST_CASE(stdcfg_feof);
TEST_CASE(stdcfg_ferror);
TEST_CASE(stdcfg_fflush);
TEST_CASE(stdcfg_fgetc);
TEST_CASE(stdcfg_fgetpos);
TEST_CASE(stdcfg_fsetpos);
TEST_CASE(stdcfg_fgets);
TEST_CASE(stdcfg_fputc);
TEST_CASE(stdcfg_fputs);
TEST_CASE(stdcfg_ftell);
TEST_CASE(stdcfg_puts);
TEST_CASE(stdcfg_putchar);
TEST_CASE(stdcfg_wcstombs);
TEST_CASE(stdcfg_tmpnam);
TEST_CASE(uninitvar_posix_write);
TEST_CASE(uninitvar_posix_types);
@ -3839,281 +3820,6 @@ private:
ASSERT_EQUALS("[test.cpp:6]: (debug) assertion failed '} while ('\n", errout.str());
}
// Test that the clearerr function, defined in std.cfg is configured correctly.
void stdcfg_clearerr() {
checkUninitVar("void f() {\n"
" FILE * pFile;\n"
" clearerr (pFile);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: pFile\n", errout.str());
checkUninitVarB("void f(FILE * pFile) {\n"
" clearerr (pFile);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the fclose function, defined in std.cfg is configured correctly.
void stdcfg_fclose() {
checkUninitVar("void f() {\n"
" FILE * pFile;\n"
" fclose (pFile);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: pFile\n", errout.str());
checkUninitVarB("void f(FILE * pFile) {\n"
" fclose (pFile);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the fopen function, defined in std.cfg is configured correctly.
void stdcfg_fopen() {
checkUninitVar("void f() {\n"
" char * filename;\n"
" fopen (filename, \"w\");\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: filename\n", errout.str());
checkUninitVar("void f() {\n"
" char * filename;\n"
" char * mode;\n"
" fopen (filename, mode);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: filename\n"
"[test.cpp:4]: (error) Uninitialized variable: mode\n", errout.str());
checkUninitVarB("void f(FILE * name, char *mode) {\n"
" fopen (name, mode);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the feof function, defined in std.cfg is configured correctly.
void stdcfg_feof() {
checkUninitVar("void f() {\n"
" FILE * pFile;\n"
" feof (pFile);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: pFile\n", errout.str());
checkUninitVarB("void f(FILE * pFile) {\n"
" feof (pFile);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the ferror function, defined in std.cfg is configured correctly.
void stdcfg_ferror() {
checkUninitVar("void f() {\n"
" FILE * pFile;\n"
" ferror (pFile);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: pFile\n", errout.str());
checkUninitVarB("void f(FILE * pFile) {\n"
" ferror (pFile);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the fflush function, defined in std.cfg is configured correctly.
void stdcfg_fflush() {
checkUninitVar("void f() {\n"
" FILE * pFile;\n"
" fflush (pFile);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: pFile\n", errout.str());
checkUninitVarB("void f(FILE * pFile) {\n"
" fflush (pFile);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the fgetc function, defined in std.cfg is configured correctly.
void stdcfg_fgetc() {
checkUninitVar("void f() {\n"
" FILE * pFile;\n"
" fgetc (pFile);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: pFile\n", errout.str());
checkUninitVarB("void f(FILE * pFile) {\n"
" fgetc (pFile);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the fgetpos function, defined in std.cfg is configured correctly.
void stdcfg_fgetpos() {
checkUninitVar("void f() {\n"
" FILE * f;\n"
" fpos_t * p;\n"
" fgetpos (f, p);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: f\n"
"[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
checkUninitVarB("void f(FILE * f) {\n"
" fpos_t p;"
" fgetpos (f, &p);\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVarB("void f(FILE * f, fpos_t *p) {\n"
" fgetpos (f, p);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the fsetpos function, defined in std.cfg is configured correctly.
void stdcfg_fsetpos() {
checkUninitVar("void f() {\n"
" FILE * f;\n"
" fpos_t * p;\n"
" fsetpos (f, p);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: f\n"
"[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
checkUninitVar("void f(FILE * f) {\n"
" fpos_t *p;"
" fsetpos (f, p);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: p\n", errout.str());
checkUninitVarB("void f(FILE * f, fpos_t *p) {\n"
" fsetpos (f, p);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the fgets function, defined in std.cfg is configured correctly.
void stdcfg_fgets() {
checkUninitVar("void f(FILE *f) {\n"
" char *s;\n"
" int n;\n"
" fgets (s, n, f);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: n\n"
"[test.cpp:4]: (error) Uninitialized variable: s\n", errout.str());
checkUninitVar2("void f(char * s, int n) {\n"
" FILE *f;\n"
" fgets (s, n, f);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: f\n",errout.str());
checkUninitVarB("void f(char * s, int n, FILE *f) {\n"
" fgets (s, n, f);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the fputc function, defined in std.cfg is configured correctly.
void stdcfg_fputc() {
checkUninitVar("void f() {\n"
" int c;\n"
" FILE *f;"
" fputc (c, f);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: c\n"
"[test.cpp:3]: (error) Uninitialized variable: f\n", errout.str());
checkUninitVarB("void f(int c, FILE *f) {\n"
" fputc (c, f);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the fputs function, defined in std.cfg is configured correctly.
void stdcfg_fputs() {
checkUninitVar("void f() {\n"
" char *c;\n"
" FILE *f;"
" fputs (c, f);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: c\n"
"[test.cpp:3]: (error) Uninitialized variable: f\n", errout.str());
checkUninitVarB("void f(char *c, FILE *f) {\n"
" fputs (c, f);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the ftell function, defined in std.cfg is configured correctly.
void stdcfg_ftell() {
checkUninitVar("void f() {\n"
" FILE *f;"
" ftell (f);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: f\n", errout.str());
checkUninitVarB("void f( FILE *f) {\n"
" ftell (f);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the puts function, defined in std.cfg is configured correctly.
void stdcfg_puts() {
checkUninitVar("void f() {\n"
" char *c;"
" puts (c);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVarB("void f( char *c) {\n"
" puts (c);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the puts function, defined in std.cfg is configured correctly.
void stdcfg_putchar() {
checkUninitVar2("void f() {\n"
" char *c;"
" putchar (*c);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVarB("void f( char *c) {\n"
" putchar (*c);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the wcstombs function, defined in std.cfg is configured correctly.
void stdcfg_wcstombs() {
// #6116 False positive uninitvar - first argument to wcstombs()
checkUninitVarB("void f( wchar_t *wstr) {\n"
" char buf[10];\n"
" wcstombs (buf, wstr, 3);\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVarB("void f( char *str) {\n"
" wchar_t wbuf[10];\n"
" mbstowcs (wbuf, str, 3);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Test that the tmpnam function, defined in std.cfg is configured correctly.
void stdcfg_tmpnam() {
checkUninitVarB("void foo() {\n"
" char buf[10];\n"
" tmpnam(buf);\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar2("void foo() {\n"
" char *ptr;\n"
" tmpnam(ptr);\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: ptr\n", "", errout.str());
}
void uninitvar_posix_write() { // #6325
// Load posix library file
LOAD_LIB_2(settings.library, "posix.cfg");