Collected some more garbage code tests in testgarbage.cpp; Avoid std::string creation in testgarbage.cpp

This commit is contained in:
PKEuS 2015-08-16 19:11:56 +02:00
parent aab4ecc726
commit 128a926d9d
5 changed files with 66 additions and 68 deletions

View File

@ -234,8 +234,6 @@ private:
TEST_CASE(negativeMemoryAllocationSizeError) // #389
TEST_CASE(negativeArraySize);
TEST_CASE(garbage1) // #6303
}
@ -3806,14 +3804,6 @@ private:
"int c[x?y:-1];\n");
ASSERT_EQUALS("", errout.str());
}
void garbage1() {
check("void foo() {\n"
"char *a = malloc(10);\n"
"a[0]\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestBufferOverrun)

View File

@ -158,6 +158,11 @@ private:
TEST_CASE(garbageCode116); // #5356
TEST_CASE(garbageCode117); // #6121
TEST_CASE(garbageCode118); // #5600
TEST_CASE(garbageCode119); // #5598
TEST_CASE(garbageCode120); // #4927
TEST_CASE(garbageCode121); // #2585
TEST_CASE(garbageCode122); // #6303
TEST_CASE(garbageCode123);
TEST_CASE(garbageValueFlow);
TEST_CASE(garbageSymbolDatabase);
@ -165,9 +170,11 @@ private:
TEST_CASE(templateSimplifierCrashes);
}
std::string checkCode(const char code[], const std::string& filename = "test.cpp") {
std::string checkCode(const char code[], bool cpp = true) {
// double the tests - run each example as C as well as C++
const std::string alternatefilename = (filename=="test.c") ? "test.cpp" : "test.c";
const char* filename = cpp ? "test.cpp" : "test.c";
const char* alternatefilename = cpp ? "test.c" : "test.cpp";
// run alternate check first. It should only ensure stability
try {
checkCodeInternal(code, alternatefilename);
@ -177,7 +184,7 @@ private:
return checkCodeInternal(code, filename);
}
std::string checkCodeInternal(const char code[], const std::string& filename) {
std::string checkCodeInternal(const char code[], const char* filename) {
errout.str("");
Settings settings;
@ -192,7 +199,7 @@ private:
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename.c_str());
tokenizer.tokenize(istr, filename);
// call all "runChecks" in all registered Check classes
for (std::list<Check *>::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) {
@ -281,10 +288,11 @@ private:
// #3585
const char code[] = "class x y { };";
{
errout.str("");
Settings settings;
settings.addEnabled("information");
{
errout.str("");
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.c");
@ -293,8 +301,6 @@ private:
}
{
errout.str("");
Settings settings;
settings.addEnabled("information");
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -668,7 +674,7 @@ private:
}
void garbageCode67() { // #6744
checkCode("&g[0]; { (g[0] 0) } =", "test.c");
checkCode("&g[0]; { (g[0] 0) } =", false);
}
void garbageCode68() { // #6745
@ -700,7 +706,7 @@ private:
}
void garbageCode75() { // #6753
checkCode("{ { void foo() { struct }; { }; } }; struct S { } f =", "test.c");
checkCode("{ { void foo() { struct }; { }; } }; struct S { } f =", false);
}
void garbageCode76() { // #6754
@ -772,7 +778,7 @@ private:
}
void garbageCode93() { // #6800
checkCode(" namespace A { } class A{ { }} class A : T ;", "test.c"); // do not crash
checkCode(" namespace A { } class A{ { }} class A : T ;", false); // do not crash
}
void garbageCode94() { // #6803
@ -894,6 +900,39 @@ private:
"}"), InternalError);
}
void garbageCode119() { // #5598
checkCode("{ { void foo() { struct }; template <typename> struct S { Used x; void bar() } auto f = [this] { }; } };");
}
void garbageCode120() { // #4927
checkCode("int main() {\n"
" return 0\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void garbageCode121() { // #2585
checkCode("abcdef?""?<"
"123456?""?>"
"+?""?=");
ASSERT_EQUALS("", errout.str());
}
void garbageCode122() { // #6303
checkCode("void foo() {\n"
"char *a = malloc(10);\n"
"a[0]\n"
"}");
}
void garbageCode123() {
checkCode("namespace pr16989 {\n"
" class C {\n"
" C tpl_mem(T *) { return }\n"
" };\n"
"}");
}
void garbageValueFlow() {
// #6089
const char* code = "{} int foo(struct, x1, struct x2, x3, int, x5, x6, x7)\n"
@ -939,7 +978,7 @@ private:
}
void templateSimplifierCrashes() {
checkCode( //5950
checkCode( // #5950
"struct A { \n"
" template <class T> operator T*();\n"
"}; \n"
@ -961,7 +1000,7 @@ private:
" }\n"
"}\n");
checkCode( // 6034
checkCode( // #6034
"template<template<typename...> class T, typename... Args>\n"
"struct foo<T<Args...> > {\n"
" const bool value = true;\n"
@ -976,9 +1015,9 @@ private:
"}\n"
);
checkCode(" > template < . > struct Y < T > { = } ;\n"); //6108
checkCode(" > template < . > struct Y < T > { = } ;\n"); // #6108
checkCode( // 6117
checkCode( // #6117
"template <typename ...> struct something_like_tuple\n"
"{};\n"
"template <typename, typename> struct is_last {\n"
@ -997,7 +1036,7 @@ private:
"SA ((is_last<int, something_like_tuple_t>::value == false));\n"
);
checkCode( //6225
checkCode( // #6225
"template <typename...>\n"
"void templ_fun_with_ty_pack() {}\n"
" \n"
@ -1007,6 +1046,17 @@ private:
" using AliasA = A<T>;\n"
"}\n"
);
// #3449
ASSERT_EQUALS("template < typename T > struct A ;\n"
"struct B { template < typename T > struct C } ;\n"
"{ } ;",
checkCode("template<typename T> struct A;\n"
"struct B { template<typename T> struct C };\n"
"{};"));
// #4169
checkCode("volatile true , test < test < #ifdef __ppc__ true ,");
}
};

View File

@ -3186,13 +3186,6 @@ private:
" per_state_info() : enter(0), exit(0), events(0) {}\n"
"};", nullptr, false, false, false);
ASSERT_EQUALS("", errout.str());
// Garbage code - don't crash
check("namespace pr16989 {\n"
" class C {\n"
" C tpl_mem(T *) { return }\n"
" };\n"
"}");
}
@ -3815,12 +3808,6 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
// Ticket #2585 - segmentation fault for invalid code
check("abcdef?""?<"
"123456?""?>"
"+?""?=");
ASSERT_EQUALS("", errout.str());
check("void f(char c) {\n"
" printf(\"%i\", 1 + 1 ? 1 : 2);\n" // "1+1" is simplified away
"}",0,false,false,false);
@ -6054,12 +6041,6 @@ private:
"}", nullptr, true, false, false);
ASSERT_EQUALS("", errout.str());
// ticket #4927 Segfault in CheckOther::checkCommaSeparatedReturn() on invalid code
check("int main() {\n"
" return 0\n"
"}", nullptr, true, false, false);
ASSERT_EQUALS("", errout.str());
// #4943 take care of C++11 initializer lists
check("std::vector<Foo> Bar() {\n"
" return\n"

View File

@ -59,7 +59,6 @@ private:
TEST_CASE(template26); // #2721 - passing 'char[2]' as template parameter
TEST_CASE(template27); // #3350 - removing unused template in macro call
TEST_CASE(template28);
TEST_CASE(template29); // #3449 - don't crash for garbage code
TEST_CASE(template30); // #3529 - template < template < ..
TEST_CASE(template31); // #4010 - reference type
TEST_CASE(template32); // #3818 - mismatching template not handled well
@ -95,7 +94,6 @@ private:
// Test TemplateSimplifier::templateParameters
TEST_CASE(templateParameters);
TEST_CASE(templateParameters1); // #4169 - segmentation fault
TEST_CASE(templateNamePosition);
}
@ -659,14 +657,6 @@ private:
ASSERT_EQUALS("Fred<int,Fred<int,int>> x ; class Fred<int,int> { } ; class Fred<int,Fred<int,int>> { } ;", tok(code));
}
void template29() {
// #3449 - garbage code (don't segfault)
const char code[] = "template<typename T> struct A;\n"
"struct B { template<typename T> struct C };\n"
"{};";
ASSERT_EQUALS("template < typename T > struct A ; struct B { template < typename T > struct C } ; { } ;", tok(code));
}
void template30() {
// #3529 - template < template < ..
const char code[] = "template<template<class> class A, class B> void f(){}";
@ -1216,13 +1206,6 @@ private:
TODO_ASSERT_EQUALS(2U, 0U, templateParameters("<class, typename...> x;")); // Mishandled valid syntax
}
void templateParameters1() {
// #4169 - segmentation fault (invalid code)
const char code[] = "volatile true , test < test < #ifdef __ppc__ true ,";
// do not crash on invalid code
ASSERT_EQUALS(0, templateParameters(code));
}
// Helper function to unit test TemplateSimplifier::getTemplateNamePosition
int templateNamePositionHelper(const char code[], unsigned offset = 0) {
Settings settings;

View File

@ -45,7 +45,6 @@ private:
TEST_CASE(iterator11);
TEST_CASE(iterator12);
TEST_CASE(iterator13);
TEST_CASE(iterator14); // #5598 invalid code causing a crash
TEST_CASE(dereference);
TEST_CASE(dereference_break); // #3644 - handle "break"
@ -454,11 +453,6 @@ private:
ASSERT_EQUALS("", errout.str());
}
void iterator14() {
check(" { { void foo() { struct }; template <typename> struct S { Used x; void bar() } auto f = [this] { }; } };");
ASSERT_EQUALS("", errout.str());
}
// Dereferencing invalid pointer
void dereference() {
check("void f()\n"