2014-01-04 20:57:02 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2014-02-15 07:45:39 +01:00
|
|
|
* Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team.
|
2014-01-04 20:57:02 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "testsuite.h"
|
|
|
|
#include "testutils.h"
|
|
|
|
#include "valueflow.h"
|
|
|
|
#include "tokenize.h"
|
|
|
|
#include "token.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
extern std::ostringstream errout;
|
|
|
|
class TestValueFlow : public TestFixture {
|
|
|
|
public:
|
|
|
|
TestValueFlow() : TestFixture("TestValueFlow") {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void run() {
|
2014-01-18 19:30:44 +01:00
|
|
|
TEST_CASE(valueFlowNumber);
|
|
|
|
|
2014-04-14 06:45:39 +02:00
|
|
|
TEST_CASE(valueFlowBitAnd);
|
|
|
|
|
2014-01-18 19:30:44 +01:00
|
|
|
TEST_CASE(valueFlowCalculations);
|
|
|
|
|
2014-01-06 10:09:49 +01:00
|
|
|
TEST_CASE(valueFlowBeforeCondition);
|
2014-01-18 08:45:24 +01:00
|
|
|
TEST_CASE(valueFlowBeforeConditionAndAndOrOrGuard);
|
|
|
|
TEST_CASE(valueFlowBeforeConditionAssignIncDec);
|
|
|
|
TEST_CASE(valueFlowBeforeConditionFunctionCall);
|
|
|
|
TEST_CASE(valueFlowBeforeConditionGlobalVariables);
|
|
|
|
TEST_CASE(valueFlowBeforeConditionGoto);
|
|
|
|
TEST_CASE(valueFlowBeforeConditionIfElse);
|
|
|
|
TEST_CASE(valueFlowBeforeConditionLoop);
|
|
|
|
TEST_CASE(valueFlowBeforeConditionMacro);
|
|
|
|
TEST_CASE(valueFlowBeforeConditionSizeof);
|
|
|
|
TEST_CASE(valueFlowBeforeConditionSwitch);
|
|
|
|
TEST_CASE(valueFlowBeforeConditionTernaryOp);
|
|
|
|
|
2014-01-21 21:13:49 +01:00
|
|
|
TEST_CASE(valueFlowAfterAssign);
|
|
|
|
|
2014-06-15 16:47:01 +02:00
|
|
|
TEST_CASE(valueFlowAfterCondition);
|
|
|
|
|
2014-01-07 19:20:56 +01:00
|
|
|
TEST_CASE(valueFlowForLoop);
|
2014-01-06 16:37:52 +01:00
|
|
|
TEST_CASE(valueFlowSubFunction);
|
2014-06-29 18:04:38 +02:00
|
|
|
TEST_CASE(valueFlowFunctionReturn);
|
2014-01-04 20:57:02 +01:00
|
|
|
}
|
|
|
|
|
2014-04-02 17:33:04 +02:00
|
|
|
bool testValueOfX(const char code[], unsigned int linenr, int value) {
|
2014-01-04 20:57:02 +01:00
|
|
|
Settings settings;
|
|
|
|
|
2014-01-12 18:19:00 +01:00
|
|
|
// strcpy cfg
|
|
|
|
const char cfg[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <function name=\"strcpy\"> <arg nr=\"1\"><not-null/></arg> </function>\n"
|
|
|
|
"</def>";
|
|
|
|
settings.library.loadxmldata(cfg, sizeof(cfg));
|
|
|
|
|
2014-01-04 20:57:02 +01:00
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
|
|
|
|
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
|
|
|
|
if (tok->str() == "x" && tok->linenr() == linenr) {
|
|
|
|
std::list<ValueFlow::Value>::const_iterator it;
|
|
|
|
for (it = tok->values.begin(); it != tok->values.end(); ++it) {
|
|
|
|
if (it->intvalue == value)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-06 07:44:58 +01:00
|
|
|
|
|
|
|
void bailout(const char code[]) {
|
|
|
|
Settings settings;
|
|
|
|
settings.debugwarnings = true;
|
|
|
|
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
errout.str("");
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
}
|
|
|
|
|
2014-01-20 21:45:30 +01:00
|
|
|
std::list<ValueFlow::Value> tokenValues(const char code[], const char tokstr[]) {
|
2014-01-18 19:30:44 +01:00
|
|
|
const Settings settings;
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
errout.str("");
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
const Token *tok = Token::findmatch(tokenizer.tokens(), tokstr);
|
2014-01-20 21:45:30 +01:00
|
|
|
return tok ? tok->values : std::list<ValueFlow::Value>();
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueFlow::Value valueOfTok(const char code[], const char tokstr[]) {
|
|
|
|
std::list<ValueFlow::Value> values = tokenValues(code, tokstr);
|
|
|
|
return values.size() == 1U ? values.front() : ValueFlow::Value();
|
2014-01-18 19:30:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void valueFlowNumber() {
|
|
|
|
const char *code;
|
|
|
|
|
|
|
|
code = "void f() {\n"
|
|
|
|
" x = 123;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(123, valueOfTok(code, "123").intvalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void valueFlowCalculations() {
|
|
|
|
const char *code;
|
|
|
|
/*
|
|
|
|
code = "void f() {\n"
|
|
|
|
" x = 123+456;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(579, valueOfTok(code, "+").intvalue);
|
|
|
|
*/
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" a = x+456;\n"
|
|
|
|
" if (x==123) {}"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(579, valueOfTok(code, "+").intvalue);
|
|
|
|
|
2014-01-20 21:45:30 +01:00
|
|
|
code = "void f(int x, int y) {\n"
|
|
|
|
" a = x+y;\n"
|
|
|
|
" if (x==123 || y==456) {}"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(0, valueOfTok(code, "+").intvalue);
|
|
|
|
|
2014-01-18 19:30:44 +01:00
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" a = x+x;\n"
|
|
|
|
" if (x==123) {}"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(246, valueOfTok(code, "+").intvalue);
|
|
|
|
|
|
|
|
code = "void f(int x, int y) {\n"
|
2014-01-20 21:45:30 +01:00
|
|
|
" a = x*x;\n"
|
|
|
|
" if (x==2) {}\n"
|
|
|
|
" if (x==4) {}\n"
|
2014-01-18 19:30:44 +01:00
|
|
|
"}";
|
2014-01-20 21:45:30 +01:00
|
|
|
std::list<ValueFlow::Value> values = tokenValues(code,"*");
|
|
|
|
ASSERT_EQUALS(2U, values.size());
|
|
|
|
ASSERT_EQUALS(4, values.front().intvalue);
|
|
|
|
ASSERT_EQUALS(16, values.back().intvalue);
|
2014-06-13 16:34:57 +02:00
|
|
|
|
|
|
|
// function call => calculation
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" a = x + 8;\n"
|
|
|
|
"}\n"
|
|
|
|
"void callf() {\n"
|
|
|
|
" f(7);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(15, valueOfTok(code, "+").intvalue);
|
2014-06-29 10:57:39 +02:00
|
|
|
|
|
|
|
code = "void f(int x, int y) {\n"
|
|
|
|
" a = x + y;\n"
|
|
|
|
"}\n"
|
|
|
|
"void callf() {\n"
|
|
|
|
" f(1,1);\n"
|
|
|
|
" f(10,10);\n"
|
|
|
|
"}";
|
|
|
|
values = tokenValues(code, "+");
|
|
|
|
ASSERT_EQUALS(true, values.empty());
|
|
|
|
if (!values.empty()) {
|
|
|
|
/* todo.. */
|
|
|
|
ASSERT_EQUALS(2U, values.size());
|
|
|
|
ASSERT_EQUALS(2, values.front().intvalue);
|
|
|
|
ASSERT_EQUALS(22, values.back().intvalue);
|
|
|
|
}
|
2014-01-18 19:30:44 +01:00
|
|
|
}
|
2014-01-06 07:44:58 +01:00
|
|
|
|
2014-01-04 20:57:02 +01:00
|
|
|
void valueFlowBeforeCondition() {
|
2014-01-08 06:39:15 +01:00
|
|
|
const char *code;
|
|
|
|
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" int a = x;\n"
|
|
|
|
" if (x == 123) {}\n"
|
|
|
|
"}";
|
2014-01-04 20:57:02 +01:00
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 123));
|
2014-01-06 07:44:58 +01:00
|
|
|
|
2014-01-08 06:53:17 +01:00
|
|
|
code = "void f(unsigned int x) {\n"
|
|
|
|
" int a = x;\n"
|
|
|
|
" if (x >= 1) {}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 1));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
|
2014-01-08 17:37:39 +01:00
|
|
|
|
2014-01-19 20:16:55 +01:00
|
|
|
code = "void f(unsigned int x) {\n"
|
|
|
|
" int a = x;\n"
|
|
|
|
" if (x > 0) {}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
|
|
|
|
|
|
|
|
code = "void f(unsigned int x) {\n"
|
|
|
|
" int a = x;\n"
|
|
|
|
" if (x > 1) {}\n" // not zero => don't consider > condition
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 2U, 1));
|
|
|
|
|
|
|
|
code = "void f(int x) {\n" // not unsigned => don't consider > condition
|
|
|
|
" int a = x;\n"
|
2014-01-20 06:31:28 +01:00
|
|
|
" if (x > 0) {}\n"
|
2014-01-19 20:16:55 +01:00
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 2U, 0));
|
|
|
|
|
2014-01-08 17:37:39 +01:00
|
|
|
code = "void f(int *x) {\n"
|
|
|
|
" *x = 100;\n"
|
|
|
|
" if (x) {}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
|
2014-01-08 06:53:17 +01:00
|
|
|
|
2014-01-10 05:47:56 +01:00
|
|
|
code = "extern const int x;\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" int a = x;\n"
|
|
|
|
" if (x == 123) {}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
|
2014-01-18 08:45:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void valueFlowBeforeConditionAssignIncDec() { // assignment / increment
|
|
|
|
const char *code;
|
2014-01-10 05:47:56 +01:00
|
|
|
|
2014-01-11 15:36:09 +01:00
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" x = 2 + x;\n"
|
|
|
|
" if (x == 65);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 2U, 65));
|
|
|
|
|
2014-01-11 20:53:23 +01:00
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" x = y = 2 + x;\n"
|
|
|
|
" if (x == 65);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 2U, 65));
|
2014-01-11 15:36:09 +01:00
|
|
|
|
2014-01-17 19:28:28 +01:00
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" a[x++] = 0;\n"
|
|
|
|
" if (x == 5);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 2U, 5));
|
|
|
|
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" a = x;\n"
|
|
|
|
" x++;\n"
|
|
|
|
" if (x == 4);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 3));
|
|
|
|
|
2014-01-18 08:45:24 +01:00
|
|
|
// bailout: assignment
|
|
|
|
bailout("void f(int x) {\n"
|
|
|
|
" x = y;\n"
|
|
|
|
" if (x == 123) {}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (debug) ValueFlow bailout: assignment of x\n", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void valueFlowBeforeConditionAndAndOrOrGuard() { // guarding by &&
|
|
|
|
const char *code;
|
|
|
|
|
2014-01-11 12:44:55 +01:00
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" if (!x || \n" // <- x can be 0
|
|
|
|
" a/x) {}\n" // <- x can't be 0
|
|
|
|
" if (x==0) {}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
|
2014-01-11 11:30:34 +01:00
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
|
|
|
|
|
2014-01-11 14:54:10 +01:00
|
|
|
code = "void f(int *x) {\n"
|
2014-01-11 15:18:39 +01:00
|
|
|
" ((x=ret())&&\n"
|
|
|
|
" (*x==0));\n" // <- x is not 0
|
2014-01-11 14:54:10 +01:00
|
|
|
" if (x==0) {}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
|
2014-01-18 08:45:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void valueFlowBeforeConditionFunctionCall() { // function calls
|
|
|
|
const char *code;
|
2014-01-11 14:54:10 +01:00
|
|
|
|
2014-01-11 14:31:51 +01:00
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" a = x;\n"
|
|
|
|
" setx(x);\n"
|
|
|
|
" if (x == 1) {}\n"
|
|
|
|
"}";
|
2014-04-02 17:33:04 +02:00
|
|
|
ASSERT_EQUALS(true, testValueOfX((std::string("void setx(int x);")+code).c_str(), 2U, 1));
|
|
|
|
ASSERT_EQUALS(false, testValueOfX((std::string("void setx(int &x);")+code).c_str(), 2U, 1));
|
2014-03-18 17:04:33 +01:00
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 1));
|
2014-01-11 14:31:51 +01:00
|
|
|
|
2014-01-12 18:19:00 +01:00
|
|
|
code = "void f(char* x) {\n"
|
|
|
|
" strcpy(x,\"abc\");\n"
|
|
|
|
" if (x) {}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
|
|
|
|
|
2014-01-13 05:52:28 +01:00
|
|
|
code = "void addNewFunction(Scope**scope, const Token**tok);\n"
|
|
|
|
"void f(Scope *x) {\n"
|
|
|
|
" x->functionList.back();\n"
|
|
|
|
" addNewFunction(&x,&tok);\n" // address-of, x can be changed by subfunction
|
|
|
|
" if (x) {}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
|
2014-01-18 08:45:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void valueFlowBeforeConditionLoop() { // while, for, do-while
|
|
|
|
const char *code;
|
2014-01-13 05:52:28 +01:00
|
|
|
|
2014-01-12 11:58:10 +01:00
|
|
|
code = "void f(int x) {\n" // loop condition, x is not assigned inside loop => use condition
|
2014-01-11 20:25:49 +01:00
|
|
|
" a = x;\n" // x can be 37
|
|
|
|
" while (x == 37) {}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 37));
|
|
|
|
|
2014-01-12 11:58:10 +01:00
|
|
|
code = "void f(int x) {\n" // loop condition, x is assigned inside loop => dont use condition
|
2014-01-11 20:25:49 +01:00
|
|
|
" a = x;\n" // don't assume that x can be 37
|
|
|
|
" while (x != 37) { x++; }\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 2U, 37));
|
|
|
|
|
2014-01-14 17:57:50 +01:00
|
|
|
code = "void f(menu *x) {\n"
|
|
|
|
" a = x->parent;\n"
|
|
|
|
" for (i=0;(i<10) && (x!=0); i++) { x = x->next; }\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 2U, 0));
|
|
|
|
|
2014-01-12 11:58:10 +01:00
|
|
|
code = "void f(int x) {\n" // condition inside loop, x is NOT assigned inside loop => use condition
|
|
|
|
" a = x;\n"
|
|
|
|
" do {\n"
|
|
|
|
" if (x==76) {}\n"
|
|
|
|
" } while (1);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 76));
|
|
|
|
|
|
|
|
code = "void f(int x) {\n" // conditions inside loop, x is assigned inside do-while => dont use condition
|
|
|
|
" a = x;\n"
|
|
|
|
" do {\n"
|
|
|
|
" if (x!=76) { x=do_something(); }\n"
|
|
|
|
" } while (1);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 2U, 76));
|
2014-01-18 11:54:00 +01:00
|
|
|
|
|
|
|
code = "void f(X x) {\n" // conditions inside loop, x is assigned inside do-while => dont use condition
|
|
|
|
" a = x;\n"
|
|
|
|
" for (i=1;i<=count;i++) {\n"
|
|
|
|
" BUGON(x==0)\n"
|
|
|
|
" x = x.next;\n"
|
|
|
|
" }\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 2U, 0));
|
2014-01-18 08:45:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void valueFlowBeforeConditionTernaryOp() { // bailout: ?:
|
|
|
|
const char *code;
|
2014-01-12 11:58:10 +01:00
|
|
|
|
2014-01-07 19:46:13 +01:00
|
|
|
bailout("void f(int x) {\n"
|
|
|
|
" y = ((x<0) ? x : ((x==2)?3:4));\n"
|
|
|
|
"}");
|
2014-01-08 06:39:15 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (debug) ValueFlow bailout: no simplification of x within ?: expression\n", errout.str());
|
|
|
|
|
|
|
|
bailout("int f(int x) {\n"
|
|
|
|
" int r = x ? 1 / x : 0;\n"
|
|
|
|
" if (x == 0) {}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (debug) ValueFlow bailout: no simplification of x within ?: expression\n", errout.str());
|
|
|
|
|
|
|
|
code = "void f(int x) {\n"
|
2014-01-10 16:13:39 +01:00
|
|
|
" int a =v x;\n"
|
2014-01-08 06:39:15 +01:00
|
|
|
" a = b ? x/2 : 20/x;\n"
|
|
|
|
" if (x == 123) {}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 123));
|
2014-01-07 19:46:13 +01:00
|
|
|
|
2014-01-10 16:13:39 +01:00
|
|
|
code = "void f(int x, int y) {\n"
|
|
|
|
" a = x;\n"
|
|
|
|
" if (y){}\n"
|
|
|
|
" if (x==123){}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 123));
|
2014-01-18 08:45:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void valueFlowBeforeConditionSizeof() { // skip sizeof
|
|
|
|
const char *code;
|
2014-01-10 16:13:39 +01:00
|
|
|
|
2014-01-11 21:10:01 +01:00
|
|
|
code = "void f(int *x) {\n"
|
|
|
|
" sizeof(x[0]);\n"
|
|
|
|
" if (x==63){}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 2U, 63));
|
2014-04-29 20:09:11 +02:00
|
|
|
|
|
|
|
code = "void f(int *x) {\n"
|
|
|
|
" char a[sizeof x.y];\n"
|
|
|
|
" if (x==0){}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 2U, 0));
|
2014-01-18 08:45:24 +01:00
|
|
|
}
|
2014-01-11 21:10:01 +01:00
|
|
|
|
2014-01-18 08:45:24 +01:00
|
|
|
void valueFlowBeforeConditionIfElse() { // bailout: if/else/etc
|
2014-01-19 09:05:48 +01:00
|
|
|
const char *code;
|
|
|
|
|
|
|
|
code = "void f(X * x) {\n"
|
|
|
|
" a = x;\n"
|
|
|
|
" if ((x != NULL) &&\n"
|
|
|
|
" (a(x->name, html)) &&\n"
|
|
|
|
" (a(x->name, body))) {}\n"
|
|
|
|
" if (x != NULL) { }\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
|
2014-01-19 09:31:40 +01:00
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 5U, 0));
|
2014-01-19 09:05:48 +01:00
|
|
|
|
2014-01-06 11:27:56 +01:00
|
|
|
bailout("void f(int x) {\n"
|
|
|
|
" if (x != 123) { b = x; }\n"
|
|
|
|
" if (x == 123) {}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (debug) ValueFlow bailout: variable x stopping on }\n", errout.str());
|
2014-06-30 00:02:49 +02:00
|
|
|
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" a = x;\n"
|
|
|
|
" if (abc) { x = 1; }\n" // <- condition must be false if x is 7 in next line
|
|
|
|
" if (x == 7) { }\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 7));
|
2014-01-18 08:45:24 +01:00
|
|
|
}
|
2014-01-06 11:27:56 +01:00
|
|
|
|
2014-01-18 08:45:24 +01:00
|
|
|
void valueFlowBeforeConditionGlobalVariables() {
|
2014-01-06 07:44:58 +01:00
|
|
|
// bailout: global variables
|
|
|
|
bailout("int x;\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" int a = x;\n"
|
|
|
|
" if (x == 123) {}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (debug) ValueFlow bailout: global variable x\n", errout.str());
|
2014-01-19 11:55:02 +01:00
|
|
|
|
|
|
|
// class variable
|
|
|
|
const char *code;
|
|
|
|
code = "class Fred { int x; void clear(); void f(); };\n"
|
|
|
|
"void Fred::f() {\n"
|
|
|
|
" int a = x;\n"
|
|
|
|
" clear();\n" // <- x might be assigned
|
|
|
|
" if (x == 234) {}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code,3,234));
|
2014-01-18 08:45:24 +01:00
|
|
|
}
|
2014-01-11 21:21:00 +01:00
|
|
|
|
2014-01-18 08:45:24 +01:00
|
|
|
void valueFlowBeforeConditionSwitch() {
|
2014-01-11 21:21:00 +01:00
|
|
|
// bailout: switch
|
2014-01-12 15:07:58 +01:00
|
|
|
// TODO : handle switch/goto more intelligently
|
2014-01-11 21:21:00 +01:00
|
|
|
bailout("void f(int x, int y) {\n"
|
|
|
|
" switch (y) {\n"
|
|
|
|
" case 1: a=x; break;\n"
|
|
|
|
" case 2: if (x==5) {} break;\n"
|
|
|
|
" };\n"
|
|
|
|
"}");
|
2014-06-30 17:56:42 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (debug) ValueFlow bailout: variable x stopping on break\n", errout.str());
|
2014-01-12 15:07:58 +01:00
|
|
|
|
|
|
|
bailout("void f(int x, int y) {\n"
|
|
|
|
" switch (y) {\n"
|
|
|
|
" case 1: a=x; return 1;\n"
|
|
|
|
" case 2: if (x==5) {} break;\n"
|
|
|
|
" };\n"
|
|
|
|
"}");
|
2014-06-30 17:56:42 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (debug) ValueFlow bailout: variable x stopping on return\n", errout.str());
|
2014-01-18 08:45:24 +01:00
|
|
|
}
|
2014-01-12 17:16:51 +01:00
|
|
|
|
2014-01-18 08:45:24 +01:00
|
|
|
void valueFlowBeforeConditionMacro() {
|
2014-01-12 17:16:51 +01:00
|
|
|
// bailout: condition is a expanded macro
|
|
|
|
bailout("void f(int x) {\n"
|
|
|
|
" a = x;\n"
|
|
|
|
" $if ($x==$123){}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (debug) ValueFlow bailout: variable x, condition is defined in macro\n", errout.str());
|
2014-01-18 08:45:24 +01:00
|
|
|
}
|
2014-01-13 16:07:25 +01:00
|
|
|
|
2014-01-18 08:45:24 +01:00
|
|
|
void valueFlowBeforeConditionGoto() {
|
2014-01-13 16:07:25 +01:00
|
|
|
// bailout: goto label (TODO: handle gotos more intelligently)
|
|
|
|
bailout("void f(int x) {\n"
|
|
|
|
" if (x == 123) { goto out; }\n"
|
|
|
|
" a=x;\n" // <- x is not 123
|
|
|
|
"out:"
|
|
|
|
" if (x==123){}\n"
|
|
|
|
"}");
|
2014-06-15 16:47:01 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (debug) ValueFlow bailout: variable x stopping on goto label\n"
|
|
|
|
"[test.cpp:2]: (debug) ValueFlow bailout: variable x. noreturn conditional scope.\n"
|
|
|
|
, errout.str());
|
2014-04-28 06:21:48 +02:00
|
|
|
|
|
|
|
// #5721 - FP
|
|
|
|
bailout("static void f(int rc) {\n"
|
|
|
|
" ABC* abc = getabc();\n"
|
|
|
|
" if (!abc) { goto out };\n"
|
|
|
|
"\n"
|
|
|
|
" abc->majortype = 0;\n"
|
|
|
|
" if (FAILED(rc)) {}\n"
|
|
|
|
"\n"
|
|
|
|
"out:\n"
|
|
|
|
" if (abc) {}\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (debug) ValueFlow bailout: assignment of abc\n"
|
2014-06-16 16:39:41 +02:00
|
|
|
"[test.cpp:8]: (debug) ValueFlow bailout: variable abc stopping on goto label\n"
|
|
|
|
"[test.cpp:3]: (debug) ValueFlow bailout: variable abc. noreturn conditional scope.\n",
|
2014-04-28 06:21:48 +02:00
|
|
|
errout.str());
|
2014-01-04 20:57:02 +01:00
|
|
|
}
|
2014-01-06 16:37:52 +01:00
|
|
|
|
2014-01-21 21:13:49 +01:00
|
|
|
void valueFlowAfterAssign() {
|
|
|
|
const char *code;
|
|
|
|
|
|
|
|
code = "void f() {\n"
|
|
|
|
" int x = 123;\n"
|
|
|
|
" a = x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
|
|
|
|
|
|
|
|
code = "void f() {\n"
|
|
|
|
" int x = 123;\n"
|
|
|
|
" a = sizeof(x);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 123));
|
2014-01-22 06:10:17 +01:00
|
|
|
|
|
|
|
code = "void f() {\n"
|
|
|
|
" int x = 123;\n"
|
|
|
|
" a = 2 + x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
|
2014-01-22 20:16:31 +01:00
|
|
|
|
2014-01-24 18:22:38 +01:00
|
|
|
code = "void f() {\n"
|
|
|
|
" int x = 9;\n"
|
|
|
|
" --x;\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 9));
|
|
|
|
|
2014-05-03 12:49:07 +02:00
|
|
|
code = "void f() {\n"
|
|
|
|
" int x = 0;\n"
|
|
|
|
" y = x += z;\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
|
|
|
|
|
2014-01-25 18:31:02 +01:00
|
|
|
code = "void f() {\n"
|
|
|
|
" static int x = 2;\n"
|
|
|
|
" x++;\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 2));
|
|
|
|
|
2014-02-22 12:09:54 +01:00
|
|
|
code = "void f() {\n"
|
|
|
|
" static int x = 2;\n"
|
|
|
|
" a >> x;\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 2));
|
|
|
|
|
2014-03-23 17:57:27 +01:00
|
|
|
code = "void f() {\n"
|
|
|
|
" static int x = 0;\n"
|
|
|
|
" if (x==0) x = getX();\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
|
|
|
|
|
2014-01-24 17:47:49 +01:00
|
|
|
// function
|
|
|
|
code = "void f() {\n"
|
|
|
|
" char *x = 0;\n"
|
|
|
|
" int success = getx((char**)&x);\n"
|
|
|
|
" if (success) x[0] = 0;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
|
|
|
|
|
2014-04-01 07:06:20 +02:00
|
|
|
code = "void f() {\n"
|
|
|
|
" char *x = 0;\n"
|
|
|
|
" getx(reinterpret_cast<void **>(&x));\n"
|
|
|
|
" *x = 0;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
|
|
|
|
|
2014-01-22 20:16:31 +01:00
|
|
|
// if/else
|
|
|
|
code = "void f() {\n"
|
|
|
|
" int x = 123;\n"
|
|
|
|
" if (condition) return;\n"
|
|
|
|
" a = 2 + x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 4U, 123));
|
|
|
|
|
|
|
|
code = "void f() {\n"
|
|
|
|
" int x = 1;\n"
|
|
|
|
" if (condition) x = 2;\n"
|
|
|
|
" a = 2 + x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 4U, 1));
|
|
|
|
|
|
|
|
code = "void f() {\n"
|
|
|
|
" int x = 123;\n"
|
|
|
|
" if (condition1) x = 456;\n"
|
|
|
|
" if (condition2) x = 789;\n"
|
|
|
|
" a = 2 + x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 123));
|
|
|
|
|
2014-01-24 18:22:38 +01:00
|
|
|
code = "void f(int a) {\n"
|
|
|
|
" int x = 123;\n"
|
|
|
|
" if (a > 1)\n"
|
|
|
|
" ++x;\n"
|
|
|
|
" else\n"
|
|
|
|
" ++x;\n"
|
|
|
|
" return 2 + x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 123));
|
|
|
|
|
2014-01-22 20:16:31 +01:00
|
|
|
code = "void f() {\n"
|
|
|
|
" int x = 1;\n"
|
|
|
|
" if (condition1) x = 2;\n"
|
|
|
|
" else return;\n"
|
|
|
|
" a = 2 + x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 5U, 1));
|
2014-01-25 19:13:33 +01:00
|
|
|
|
2014-01-26 15:50:25 +01:00
|
|
|
code = "void f(){\n"
|
|
|
|
" int x = 0;\n"
|
|
|
|
" if (a>=0) { x = getx(); }\n"
|
|
|
|
" if (x==0) { return; }\n"
|
|
|
|
" return 123 / x;\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 5U, 0));
|
|
|
|
|
2014-01-31 15:43:34 +01:00
|
|
|
code = "void f() {\n"
|
|
|
|
" X *x = getx();\n"
|
|
|
|
" if(false) { x = 0; }\n"
|
|
|
|
" else { x->y = 1; }\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
|
|
|
|
|
2014-03-18 17:04:33 +01:00
|
|
|
code = "void f() {\n"
|
|
|
|
" int x = 32;\n"
|
|
|
|
" if (x>=32) return;\n"
|
|
|
|
" a[x]=0;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 32));
|
|
|
|
|
|
|
|
code = "void f() {\n"
|
|
|
|
" int x = 32;\n"
|
|
|
|
" if (x>=32) {\n"
|
|
|
|
" a[x] = 0;\n" // <- should have possible value 32
|
|
|
|
" return;\n"
|
|
|
|
" }\n"
|
|
|
|
"}";
|
2014-06-15 17:15:09 +02:00
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 4U, 32));
|
2014-03-18 17:04:33 +01:00
|
|
|
|
2014-04-22 16:10:20 +02:00
|
|
|
code = "void f() {\n"
|
|
|
|
" int x = 32;\n"
|
|
|
|
" if (a==1) { z=x+12; }\n"
|
|
|
|
" if (a==2) { z=x+32; }\n"
|
|
|
|
" z = x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 32));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 4U, 32));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 5U, 32));
|
|
|
|
|
2014-04-18 16:10:18 +02:00
|
|
|
code = "void f() {\n" // #5656 - FP
|
|
|
|
" int x = 0;\n"
|
|
|
|
" if (!x) {\n"
|
|
|
|
" x = getx();\n"
|
|
|
|
" }\n"
|
|
|
|
" y = x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 6U, 0));
|
|
|
|
|
2014-04-26 11:27:58 +02:00
|
|
|
code = "void f () {\n"
|
|
|
|
" ST * x = g_pST;\n"
|
|
|
|
" if (x->y == 0) {\n"
|
|
|
|
" x = NULL;\n"
|
|
|
|
" return 1;\n"
|
|
|
|
" }\n"
|
|
|
|
" a = x->y;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 7U, 0));
|
|
|
|
|
2014-04-26 14:41:28 +02:00
|
|
|
code = "void f () {\n"
|
|
|
|
" ST * x = g_pST;\n"
|
|
|
|
" if (x->y == 0) {\n"
|
|
|
|
" x = NULL;\n"
|
|
|
|
" goto label;\n"
|
|
|
|
" }\n"
|
|
|
|
" a = x->y;\n"
|
|
|
|
"label:\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 7U, 0));
|
|
|
|
|
2014-05-01 15:15:26 +02:00
|
|
|
code = "void f() {\n" // #5752 - FP
|
|
|
|
" int *x = 0;\n"
|
|
|
|
" if (x && *x == 123) {\n"
|
|
|
|
" getx(*x);\n"
|
|
|
|
" }\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
|
|
|
|
|
2014-07-30 18:12:33 +02:00
|
|
|
code = "void f() {\n"
|
|
|
|
" int x = 0;\n"
|
|
|
|
" if (!x) {}\n"
|
|
|
|
" else { y = x; }\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
|
|
|
|
|
2014-01-25 19:13:33 +01:00
|
|
|
// multivariables
|
|
|
|
code = "void f(int a) {\n"
|
|
|
|
" int x = a;\n"
|
|
|
|
" if (a!=132) { b = x; }\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 132));
|
|
|
|
|
|
|
|
code = "void f(int a) {\n"
|
|
|
|
" int x = a;\n"
|
2014-02-17 20:07:38 +01:00
|
|
|
" b = x;\n" // <- line 3
|
2014-01-25 19:13:33 +01:00
|
|
|
" if (a!=132) {}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 132));
|
2014-01-25 20:14:49 +01:00
|
|
|
|
|
|
|
code = "void f() {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" if (n) { a = n; }\n"
|
|
|
|
" else { a = 0; }\n"
|
|
|
|
" int x = a;\n"
|
2014-02-17 20:07:38 +01:00
|
|
|
" if (a > 0) { a = b / x; }\n" // <- line 6
|
2014-01-25 20:14:49 +01:00
|
|
|
"}";
|
2014-02-17 20:07:38 +01:00
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 6U, 0)); // x is not 0 at line 6
|
|
|
|
|
|
|
|
// break
|
|
|
|
code = "void f() {\n"
|
|
|
|
" for (;;) {\n"
|
|
|
|
" int x = 1;\n"
|
|
|
|
" if (!abc()) {\n"
|
|
|
|
" x = 2;\n"
|
|
|
|
" break;\n"
|
|
|
|
" }\n"
|
|
|
|
" a = x;\n" // <- line 8
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 8U, 2)); // x is not 2 at line 8
|
2014-06-30 17:56:42 +02:00
|
|
|
|
|
|
|
code = "void f() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" switch (ab) {\n"
|
|
|
|
" case A: x = 12; break;\n"
|
|
|
|
" case B: x = 34; break;\n"
|
|
|
|
" }\n"
|
|
|
|
" v = x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 7U, 12));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 7U, 34));
|
2014-07-16 09:12:56 +02:00
|
|
|
|
|
|
|
code = "void f() {\n" // #5981
|
|
|
|
" int x;\n"
|
|
|
|
" switch (ab) {\n"
|
|
|
|
" case A: x = 12; break;\n"
|
|
|
|
" case B: x = 34; break;\n"
|
|
|
|
" }\n"
|
|
|
|
" switch (ab) {\n"
|
|
|
|
" case A: v = x; break;\n" // <- x is not 34
|
|
|
|
" }\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 8U, 34));
|
2014-01-21 21:13:49 +01:00
|
|
|
}
|
|
|
|
|
2014-06-15 16:47:01 +02:00
|
|
|
void valueFlowAfterCondition() {
|
|
|
|
const char *code;
|
|
|
|
|
|
|
|
// if
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" if (x == 123) {\n"
|
|
|
|
" a = x;\n"
|
|
|
|
" }\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
|
|
|
|
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" if (x != 123) {\n"
|
|
|
|
" a = x;\n"
|
|
|
|
" }\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 123));
|
|
|
|
|
|
|
|
// else
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" if (x == 123) {}\n"
|
|
|
|
" else a = x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 123));
|
|
|
|
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" if (x != 123) {}\n"
|
|
|
|
" else a = x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
|
2014-06-16 16:39:41 +02:00
|
|
|
|
|
|
|
// !
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" if (!x) { a = x; }\n"
|
|
|
|
" else a = x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
|
|
|
|
|
2014-07-07 17:48:58 +02:00
|
|
|
code = "void f(int x, int y) {\n"
|
|
|
|
" if (!(x&&y)) { return; }\n"
|
|
|
|
" a = x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
|
|
|
|
|
2014-06-18 06:57:48 +02:00
|
|
|
// if (var)
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" if (x) { a = x; }\n" // <- x is not 0
|
|
|
|
" else { b = x; }\n" // <- x is 0
|
|
|
|
" c = x;\n" // <- x might be 0
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 2U, 0));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 4U, 0));
|
|
|
|
|
2014-06-18 05:51:23 +02:00
|
|
|
// After while
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" while (x != 3) {}\n"
|
|
|
|
" a = x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 3));
|
2014-06-19 17:29:41 +02:00
|
|
|
|
2014-06-25 16:00:56 +02:00
|
|
|
// conditional code after if/else/while
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" if (x == 2) {}\n"
|
|
|
|
" if (x > 0)\n"
|
|
|
|
" a = x;\n"
|
|
|
|
" else\n"
|
|
|
|
" b = x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 4U, 2));
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 6U, 2));
|
|
|
|
|
2014-08-01 07:35:15 +02:00
|
|
|
// condition with 2nd variable
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" int y = 0;\n"
|
|
|
|
" if (x == 7) { y = 1; }\n"
|
|
|
|
" if (!y)\n"
|
|
|
|
" a = x;\n" // <- x can not be 7 here
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 5U, 7));
|
|
|
|
|
2014-06-19 17:29:41 +02:00
|
|
|
// In condition, after && and ||
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" a = (x != 3 ||\n"
|
|
|
|
" x);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 3));
|
|
|
|
|
|
|
|
code = "void f(int x) {\n"
|
|
|
|
" a = (x == 4 &&\n"
|
|
|
|
" x);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 4));
|
2014-06-25 06:17:44 +02:00
|
|
|
|
|
|
|
// TODO: float
|
|
|
|
code = "void f(float x) {\n"
|
|
|
|
" if (x == 0.5) {}\n"
|
|
|
|
" a = x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
|
2014-06-15 16:47:01 +02:00
|
|
|
}
|
|
|
|
|
2014-04-14 06:45:39 +02:00
|
|
|
void valueFlowBitAnd() {
|
|
|
|
const char *code;
|
|
|
|
|
|
|
|
code = "int f(int a) {\n"
|
|
|
|
" int x = a & 0x80;\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code,3U,0));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code,3U,0x80));
|
|
|
|
}
|
|
|
|
|
2014-01-07 19:20:56 +01:00
|
|
|
void valueFlowForLoop() {
|
2014-01-10 16:51:58 +01:00
|
|
|
const char *code;
|
|
|
|
|
|
|
|
code = "void f() {\n"
|
|
|
|
" for (int x = 0; x < 10; x++)\n"
|
|
|
|
" a[x] = 0;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 9));
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 10));
|
|
|
|
|
2014-03-29 20:20:22 +01:00
|
|
|
code = "void f(int a) {\n"
|
|
|
|
" for (int x = a; x < 10; x++)\n"
|
|
|
|
" a[x] = 0;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 9));
|
|
|
|
|
2014-03-22 19:02:33 +01:00
|
|
|
code = "void f() {\n"
|
|
|
|
" for (int x = 0; x < 10; x = x + 2)\n"
|
|
|
|
" a[x] = 0;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 8));
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 10));
|
|
|
|
|
2014-03-23 20:37:56 +01:00
|
|
|
code = "void f() {\n"
|
|
|
|
" for (int x = 0; x < 10; x = x / 0)\n"
|
|
|
|
" a[x] = 0;\n"
|
|
|
|
"}";
|
2014-03-24 00:16:02 +01:00
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0)); // don't crash
|
2014-03-23 20:37:56 +01:00
|
|
|
|
2014-02-22 17:58:48 +01:00
|
|
|
code = "void f() {\n"
|
|
|
|
" for (int x = 0; x < 10; x++)\n"
|
|
|
|
" x<4 ?\n"
|
|
|
|
" a[x] : 0;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 9));
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 9));
|
2014-03-25 18:22:22 +01:00
|
|
|
|
|
|
|
code = "void f() {\n"
|
|
|
|
" for (int x = 0; x < 10; x++)\n"
|
|
|
|
" x==0 ?\n"
|
|
|
|
" 0 : a[x];\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
|
2014-04-28 15:54:54 +02:00
|
|
|
|
|
|
|
code = "void f() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" for (int i = 0; x = bar[i]; i++)\n"
|
|
|
|
" x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
|
2014-06-30 07:26:48 +02:00
|
|
|
|
|
|
|
code = "void f() {\n" // #5939
|
|
|
|
" int x;\n"
|
|
|
|
" for (int x = 0; (x = do_something()) != 0;)\n"
|
|
|
|
" x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
|
|
|
|
|
|
|
|
code = "void f() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" for (int x = 0; x < 10 && y = do_something();)\n"
|
|
|
|
" x;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 4U, 0));
|
2014-07-17 08:44:55 +02:00
|
|
|
|
|
|
|
code = "void foo(double recoveredX) {\n"
|
|
|
|
" for (double x = 1e-18; x < 1e40; x *= 1.9) {\n"
|
|
|
|
" double relativeError = (x - recoveredX) / x;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
|
2014-08-01 16:12:57 +02:00
|
|
|
|
|
|
|
// &&
|
|
|
|
code = "void foo() {\n"
|
|
|
|
" for (int x = 0; x < 10; x++) {\n"
|
|
|
|
" if (x > 1\n"
|
|
|
|
" && x) {}" // <- x is not 0
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 4U, 9));
|
|
|
|
|
|
|
|
// ||
|
|
|
|
code = "void foo() {\n"
|
|
|
|
" for (int x = 0; x < 10; x++) {\n"
|
|
|
|
" if (x == 0\n"
|
|
|
|
" || x) {}" // <- x is not 0
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 4U, 9));
|
2014-01-07 19:20:56 +01:00
|
|
|
}
|
|
|
|
|
2014-01-06 16:37:52 +01:00
|
|
|
void valueFlowSubFunction() {
|
|
|
|
const char *code;
|
|
|
|
|
|
|
|
code = "void f1(int x) { return x; }\n"
|
|
|
|
"void f2(int x) {\n"
|
|
|
|
" f1(123);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 1U, 123));
|
|
|
|
|
|
|
|
code = "void f1(int x) { return x; }\n"
|
|
|
|
"void f2(int x) {\n"
|
|
|
|
" f1(x);\n"
|
|
|
|
" if (x==0){}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 1U, 0));
|
2014-01-08 06:04:51 +01:00
|
|
|
|
|
|
|
code = "void f1(int x) {\n"
|
|
|
|
" if (x == 0) return;\n"
|
|
|
|
" int y = 1234 / x;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"void f2() {\n"
|
|
|
|
" f1(0);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
|
2014-01-08 16:17:47 +01:00
|
|
|
|
|
|
|
code = "void f1(int x) { a=x; }\n"
|
|
|
|
"void f2(int y) { f1(y<123); }\n";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 1U, 0));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 1U, 1));
|
2014-01-11 07:52:25 +01:00
|
|
|
|
|
|
|
code = "void f1(int x) { a=(abc)x; }\n"
|
|
|
|
"void f2(int y) { f1(123); }\n";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 1U, 123));
|
2014-06-24 19:30:46 +02:00
|
|
|
|
|
|
|
code = "void f1(int x) {\n"
|
|
|
|
" x ?\n"
|
|
|
|
" 1024 / x :\n"
|
|
|
|
" 0; }\n"
|
|
|
|
"void f2() { f1(0); }";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
|
2014-06-28 12:04:20 +02:00
|
|
|
|
|
|
|
// #5861 - fp with float
|
|
|
|
code = "void f1(float x) {\n"
|
|
|
|
" return 1.0 / x;\n"
|
|
|
|
"}\n"
|
|
|
|
"void f2() { f1(0.5); }";
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 2U, 0));
|
2014-01-06 16:37:52 +01:00
|
|
|
}
|
2014-06-29 18:04:38 +02:00
|
|
|
|
|
|
|
void valueFlowFunctionReturn() {
|
|
|
|
const char *code;
|
|
|
|
|
|
|
|
code = "void f1(int x) {\n"
|
|
|
|
" return x+1;\n"
|
|
|
|
"}\n"
|
|
|
|
"void f2() {\n"
|
|
|
|
" x = 10 - f1(2);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(7, valueOfTok(code, "-").intvalue);
|
|
|
|
|
|
|
|
code = "void add(int x, int y) {\n"
|
|
|
|
" return x+y;\n"
|
|
|
|
"}\n"
|
|
|
|
"void f2() {\n"
|
|
|
|
" x = 1 * add(10+1,4);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(15, valueOfTok(code, "*").intvalue);
|
|
|
|
}
|
2014-01-04 20:57:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestValueFlow)
|