From b91f42453b51a1d609b9a9aa9452c0b9f09f9fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 20 Jan 2014 22:26:55 +0100 Subject: [PATCH] value flow: improved handling of cast --- lib/tokenize.cpp | 2 ++ lib/valueflow.cpp | 10 ++++++++-- test/testbufferoverrun.cpp | 6 ++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 52b4c6500..1c629fbd7 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3641,6 +3641,8 @@ bool Tokenizer::simplifyTokenList2() list.createAst(); + ValueFlow::setValues(&list, _errorLogger, _settings); + if (_settings->terminated()) return false; diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index be1626d50..d5edcb9d6 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -164,9 +164,15 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value) --it; } - // Calculations.. Token *parent = const_cast(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::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) { diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index fcdf7184d..3383c28be 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -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() {