Fix 10645: FP knownEmptyContainer after conditional return (#3620)

This commit is contained in:
Paul Fultz II 2021-12-14 00:22:57 -06:00 committed by GitHub
parent f79bb40e3d
commit cb2738a60c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View File

@ -640,13 +640,13 @@ static ValueFlow::Value execute(const Token* expr, ProgramMemory& pm)
if (rhs.isIntValue()) { if (rhs.isIntValue()) {
std::vector<ValueFlow::Value> result = std::vector<ValueFlow::Value> result =
infer(makeIntegralInferModel(), expr->str(), expr->astOperand1()->values(), {rhs}); infer(makeIntegralInferModel(), expr->str(), expr->astOperand1()->values(), {rhs});
if (result.empty()) if (result.empty() || !result.front().isKnown())
return unknown; return unknown;
return result.front(); return result.front();
} else if (lhs.isIntValue()) { } else if (lhs.isIntValue()) {
std::vector<ValueFlow::Value> result = std::vector<ValueFlow::Value> result =
infer(makeIntegralInferModel(), expr->str(), {lhs}, expr->astOperand2()->values()); infer(makeIntegralInferModel(), expr->str(), {lhs}, expr->astOperand2()->values());
if (result.empty()) if (result.empty() || !result.front().isKnown())
return unknown; return unknown;
return result.front(); return result.front();
} }

View File

@ -2472,7 +2472,7 @@ private:
check("void func(std::list<std::string> strlist) {\n" check("void func(std::list<std::string> strlist) {\n"
" for (std::list<std::string>::iterator str = strlist.begin(); str != strlist.end(); str++) {\n" " for (std::list<std::string>::iterator str = strlist.begin(); str != strlist.end(); str++) {\n"
" if (func2(*str)) {\n" " if (func2(*str)) {\n"
" strlist.erase(str);\n" " strlist.erase(str);\n"
" if (strlist.empty())\n" " if (strlist.empty())\n"
" return;\n" " return;\n"
" }\n" " }\n"
@ -5387,6 +5387,18 @@ private:
"}\n", "}\n",
true); true);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void f(const std::vector<int>& v, int e) {\n"
" if (!v.empty()) {\n"
" if (e < 0 || true) {\n"
" if (e < 0)\n"
" return;\n"
" }\n"
" }\n"
" for (auto i : v) {}\n"
"}\n",
true);
ASSERT_EQUALS("", errout.str());
} }
void checkMutexes() { void checkMutexes() {