value flow: improved handling of cast

This commit is contained in:
Daniel Marjamäki 2014-01-20 22:26:55 +01:00
parent bb3cc67aaa
commit b91f42453b
3 changed files with 16 additions and 2 deletions

View File

@ -3641,6 +3641,8 @@ bool Tokenizer::simplifyTokenList2()
list.createAst();
ValueFlow::setValues(&list, _errorLogger, _settings);
if (_settings->terminated())
return false;

View File

@ -164,9 +164,15 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value)
--it;
}
// Calculations..
Token *parent = const_cast<Token*>(tok->astParent());
if (parent && parent->isArithmeticalOp() && parent->astOperand1() && parent->astOperand2()) {
// Cast..
if (parent && parent->str() == "(" && tok == parent->link()->next()) {
setTokenValue(parent,value);
}
// Calculations..
else if (parent && parent->isArithmeticalOp() && parent->astOperand1() && parent->astOperand2()) {
std::list<ValueFlow::Value>::const_iterator value1, value2;
for (value1 = parent->astOperand1()->values.begin(); value1 != parent->astOperand1()->values.end(); ++value1) {
for (value2 = parent->astOperand2()->values.begin(); value2 != parent->astOperand2()->values.end(); ++value2) {

View File

@ -2063,6 +2063,12 @@ private:
" if (i==10) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (warning) Array 'str[3]' accessed at index 10, which is out of bounds. Otherwise condition 'i==10' is redundant.\n", errout.str());
check("void f() {\n"
" char str[3];\n"
" str[((unsigned char)3) - 1] = 0;\n"
"}", false, "test.cpp", false);
ASSERT_EQUALS("", errout.str());
}
void buffer_overrun_1_standard_functions() {