Fix #9696 FP uninitdata - writing pointer to stream (#3772)

This commit is contained in:
chrchr-github 2022-02-02 12:24:32 +01:00 committed by GitHub
parent d928b57829
commit 2b13a27140
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View File

@ -1263,8 +1263,13 @@ const Token* CheckUninitVar::isVariableUsage(bool cpp, const Token *vartok, cons
if (isLikelyStreamRead(cpp, vartok->previous())) if (isLikelyStreamRead(cpp, vartok->previous()))
return nullptr; return nullptr;
if (valueExpr->valueType() && valueExpr->valueType()->type == ValueType::Type::VOID) if (const auto* vt = valueExpr->valueType()) {
if (vt->type == ValueType::Type::VOID)
return nullptr; return nullptr;
// passing a char* to a stream will dereference it
if ((alloc == CTOR_CALL || alloc == ARRAY) && vt->pointer && vt->type != ValueType::Type::CHAR && vt->type != ValueType::Type::WCHAR_T)
return nullptr;
}
} }
if (astIsRhs(derefValue) && isLikelyStreamRead(cpp, derefValue->astParent())) if (astIsRhs(derefValue) && isLikelyStreamRead(cpp, derefValue->astParent()))
return nullptr; return nullptr;

View File

@ -525,6 +525,24 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str());
checkUninitVar("void f() {\n" // #9696
" int *p = new int[10];\n"
" std::cout << p << 1;\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n"
" int i[10];\n"
" std::cout << i;\n"
" char c[10];\n"
" std::cout << c;\n"
" wchar_t w[10];\n"
" std::cout << w;\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: c\n"
"[test.cpp:7]: (error) Uninitialized variable: w\n",
errout.str());
checkUninitVar("void f() {\n" checkUninitVar("void f() {\n"
" char p[10];\n" " char p[10];\n"
" std::cout << p << 1;\n" " std::cout << p << 1;\n"