SimplifyTokens: Replace 'sizeof(*abc)' with '100'. This prevents false positives in checks
This commit is contained in:
parent
1801f25bce
commit
adad786d75
|
@ -114,6 +114,8 @@ private:
|
||||||
|
|
||||||
TEST_CASE( linux_list_1 );
|
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() );
|
ASSERT_EQUALS( std::string(""), errout.str() );
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
// TODO: Add this test
|
|
||||||
void linux_list_2()
|
void linux_list_2()
|
||||||
{
|
{
|
||||||
check( "struct AB\n"
|
check( "struct AB\n"
|
||||||
|
@ -856,7 +856,34 @@ private:
|
||||||
|
|
||||||
ASSERT_EQUALS( std::string("[test.cpp:10]: Memory leak: ab\n"), errout.str() );
|
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 );
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ private:
|
||||||
void run()
|
void run()
|
||||||
{
|
{
|
||||||
TEST_CASE( cast0 );
|
TEST_CASE( cast0 );
|
||||||
|
TEST_CASE( sizeof1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string tok(const char code[])
|
static std::string tok(const char code[])
|
||||||
|
@ -58,6 +59,13 @@ private:
|
||||||
const char code2[] = " if ( p == 0 ) ";
|
const char code2[] = " if ( p == 0 ) ";
|
||||||
ASSERT_EQUALS( tok(code1), tok(code2) );
|
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 )
|
REGISTER_TEST( TestSimplifyTokens )
|
||||||
|
|
|
@ -759,6 +759,13 @@ void Tokenizer::SimplifyTokenList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (Match(tok, "sizeof ( * %var% )"))
|
||||||
|
{
|
||||||
|
tok->setstr("100");
|
||||||
|
for ( int i = 0; i < 4; ++i )
|
||||||
|
DeleteNextToken(tok);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace 'sizeof(var)'
|
// Replace 'sizeof(var)'
|
||||||
|
|
Loading…
Reference in New Issue