Fixed #8988 (False positive: using memset on struct)

This commit is contained in:
Daniel Marjamäki 2019-04-01 19:32:00 +02:00
parent 974f01ce59
commit 761f18c75c
3 changed files with 54 additions and 7 deletions

View File

@ -7536,4 +7536,41 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
<podtype name="jmp_buf"/>
<podtype name="std::streamsize,streamsize" sign="s"/>
<podtype name="std::streamoff,streamoff" sign="s"/>
<podtype name="std::atomic_bool"/>
<podtype name="std::atomic_char"/>
<podtype name="std::atomic_schar"/>
<podtype name="std::atomic_uchar"/>
<podtype name="std::atomic_short"/>
<podtype name="std::atomic_ushort"/>
<podtype name="std::atomic_int"/>
<podtype name="std::atomic_uint"/>
<podtype name="std::atomic_long"/>
<podtype name="std::atomic_ulong"/>
<podtype name="std::atomic_llong"/>
<podtype name="std::atomic_ullong"/>
<podtype name="std::atomic_char16_t"/>
<podtype name="std::atomic_char32_t"/>
<podtype name="std::atomic_wchar_t"/>
<podtype name="std::atomic_int_least8_t"/>
<podtype name="std::atomic_uint_least8_t"/>
<podtype name="std::atomic_int_least16_t"/>
<podtype name="std::atomic_uint_least16_t"/>
<podtype name="std::atomic_int_least32_t"/>
<podtype name="std::atomic_uint_least32_t"/>
<podtype name="std::atomic_int_least64_t"/>
<podtype name="std::atomic_uint_least64_t"/>
<podtype name="std::atomic_int_fast8_t"/>
<podtype name="std::atomic_uint_fast8_t"/>
<podtype name="std::atomic_int_fast16_t"/>
<podtype name="std::atomic_uint_fast16_t"/>
<podtype name="std::atomic_int_fast32_t"/>
<podtype name="std::atomic_uint_fast32_t"/>
<podtype name="std::atomic_int_fast64_t"/>
<podtype name="std::atomic_uint_fast64_t"/>
<podtype name="std::atomic_intptr_t"/>
<podtype name="std::atomic_uintptr_t"/>
<podtype name="std::atomic_size_t"/>
<podtype name="std::atomic_ptrdiff_t"/>
<podtype name="std::atomic_intmax_t"/>
<podtype name="std::atomic_uintmax_t "/>
</def>

View File

@ -1135,7 +1135,6 @@ void CheckClass::checkMemset()
// 3 arguments.
continue;
const Token *typeTok = nullptr;
const Scope *type = nullptr;
if (Token::Match(arg3, "sizeof ( %type% ) )"))
@ -1237,12 +1236,23 @@ void CheckClass::checkMemsetType(const Scope *start, const Token *tok, const Sco
const Token *tok1 = var.typeStartToken();
const Scope *typeScope = var.typeScope();
std::string typeName;
if (Token::Match(tok1, "%type% ::")) {
const Token *typeTok = tok1;
while (Token::Match(typeTok, "%type% ::")) {
typeName += typeTok->str() + "::";
typeTok = typeTok->tokAt(2);
}
typeName += typeTok->str();
}
// check for std:: type
if (var.isStlType() && tok1->strAt(2) != "array" && !mSettings->library.podtype(tok1->strAt(2)))
if (var.isStlType() && typeName != "std::array" && !mSettings->library.podtype(typeName)) {
if (allocation)
mallocOnClassError(tok, tok->str(), type->classDef, "'std::" + tok1->strAt(2) + "'");
mallocOnClassError(tok, tok->str(), type->classDef, "'" + typeName + "'");
else
memsetError(tok, tok->str(), "'std::" + tok1->strAt(2) + "'", type->classDef->str());
memsetError(tok, tok->str(), "'" + typeName + "'", type->classDef->str());
}
// check for known type
else if (typeScope && typeScope != type)

View File

@ -3144,7 +3144,8 @@ private:
Settings settings;
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <podtype name=\"uint8_t\" sign=\"u\" size=\"1\"/>\n"
" <podtype name=\"std::uint8_t\" sign=\"u\" size=\"1\"/>\n"
" <podtype name=\"std::atomic_bool\"/>\n"
"</def>";
tinyxml2::XMLDocument doc;
doc.Parse(xmldata, sizeof(xmldata));
@ -3161,8 +3162,7 @@ private:
checkNoMemset("struct st {\n"
" std::uint8_t a;\n"
" std::uint8_t b;\n"
" std::uint8_t c;\n"
" std::atomic_bool b;\n"
"};\n"
"\n"
"void f() {\n"