Skip to content

Commit 2b13a27

Browse files
Fix #9696 FP uninitdata - writing pointer to stream (#3772)
1 parent d928b57 commit 2b13a27

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

lib/checkuninitvar.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,8 +1263,13 @@ const Token* CheckUninitVar::isVariableUsage(bool cpp, const Token *vartok, cons
12631263
if (isLikelyStreamRead(cpp, vartok->previous()))
12641264
return nullptr;
12651265

1266-
if (valueExpr->valueType() && valueExpr->valueType()->type == ValueType::Type::VOID)
1267-
return nullptr;
1266+
if (const auto* vt = valueExpr->valueType()) {
1267+
if (vt->type == ValueType::Type::VOID)
1268+
return nullptr;
1269+
// passing a char* to a stream will dereference it
1270+
if ((alloc == CTOR_CALL || alloc == ARRAY) && vt->pointer && vt->type != ValueType::Type::CHAR && vt->type != ValueType::Type::WCHAR_T)
1271+
return nullptr;
1272+
}
12681273
}
12691274
if (astIsRhs(derefValue) && isLikelyStreamRead(cpp, derefValue->astParent()))
12701275
return nullptr;

test/testuninitvar.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,24 @@ class TestUninitVar : public TestFixture {
525525
"}");
526526
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str());
527527

528+
checkUninitVar("void f() {\n" // #9696
529+
" int *p = new int[10];\n"
530+
" std::cout << p << 1;\n"
531+
"}");
532+
ASSERT_EQUALS("", errout.str());
533+
534+
checkUninitVar("void f() {\n"
535+
" int i[10];\n"
536+
" std::cout << i;\n"
537+
" char c[10];\n"
538+
" std::cout << c;\n"
539+
" wchar_t w[10];\n"
540+
" std::cout << w;\n"
541+
"}");
542+
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: c\n"
543+
"[test.cpp:7]: (error) Uninitialized variable: w\n",
544+
errout.str());
545+
528546
checkUninitVar("void f() {\n"
529547
" char p[10];\n"
530548
" std::cout << p << 1;\n"

0 commit comments

Comments
 (0)