SimplifyTokens: Replace 'sizeof(*abc)' with '100'. This prevents false positives in checks

This commit is contained in:
Daniel Marjamäki 2008-11-15 17:21:35 +00:00
parent 1801f25bce
commit adad786d75
3 changed files with 46 additions and 4 deletions

View File

@ -113,7 +113,9 @@ private:
TEST_CASE( throw1 );
TEST_CASE( linux_list_1 );
// TODO: TEST_CASE( linux_list_2 );
// TODO: TEST_CASE( linux_list_2 );
TEST_CASE( sizeof1 );
}
@ -839,8 +841,6 @@ private:
ASSERT_EQUALS( std::string(""), errout.str() );
}
/*
// TODO: Add this test
void linux_list_2()
{
check( "struct AB\n"
@ -856,8 +856,35 @@ private:
ASSERT_EQUALS( std::string("[test.cpp:10]: Memory leak: ab\n"), errout.str() );
}
*/
void sizeof1()
{
check( "void f()\n"
"{\n"
" struct s_t s1;\n"
" struct s_t cont *p = &s1;\n"
" struct s_t *s2;\n"
"\n"
" memset(p, 0, sizeof(*p));\n"
"\n"
" s2 = (struct s_t *) malloc(sizeof(*s2));\n"
"\n"
" if (s2->value != 0)\n"
" return;\n"
"\n"
" free(s2);\n"
"\n"
" return;\n"
"}\n" );
std::string err( errout.str() );
ASSERT_EQUALS( std::string("[test.cpp:12]: Memory leak: s2\n"), err );
}
};
REGISTER_TEST( TestMemleak )

View File

@ -34,6 +34,7 @@ private:
void run()
{
TEST_CASE( cast0 );
TEST_CASE( sizeof1 );
}
static std::string tok(const char code[])
@ -58,6 +59,13 @@ private:
const char code2[] = " if ( p == 0 ) ";
ASSERT_EQUALS( tok(code1), tok(code2) );
}
void sizeof1()
{
const char code1[] = " struct ABC *abc = malloc(sizeof(*abc)); ";
const char code2[] = " struct ABC *abc = malloc(100); ";
ASSERT_EQUALS( tok(code1), tok(code2) );
}
};
REGISTER_TEST( TestSimplifyTokens )

View File

@ -758,6 +758,13 @@ void Tokenizer::SimplifyTokenList()
DeleteNextToken(tok);
}
}
}
else if (Match(tok, "sizeof ( * %var% )"))
{
tok->setstr("100");
for ( int i = 0; i < 4; ++i )
DeleteNextToken(tok);
}
}