MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / stringLiteralWrite

Method stringLiteralWrite

lib/checkstring.cpp:50–67  ·  view source on GitHub ↗

--------------------------------------------------------------------------- Writing string literal is UB ---------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

48// Writing string literal is UB
49//---------------------------------------------------------------------------
50void CheckStringImpl::stringLiteralWrite()
51{
52 logChecker("CheckString::stringLiteralWrite");
53 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
54 for (const Scope * scope : symbolDatabase->functionScopes) {
55 for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
56 if (!tok->variable() || !tok->variable()->isPointer())
57 continue;
58 const Token *str = tok->getValueTokenMinStrSize(mSettings);
59 if (!str)
60 continue;
61 if (Token::Match(tok, "%var% [") && Token::simpleMatch(tok->linkAt(1), "] ="))
62 stringLiteralWriteError(tok, str);
63 else if (Token::Match(tok->previous(), "* %var% ="))
64 stringLiteralWriteError(tok, str);
65 }
66 }
67}
68
69void CheckStringImpl::stringLiteralWriteError(const Token *tok, const Token *strValue)
70{

Callers 1

runChecksMethod · 0.45

Calls 5

nextMethod · 0.80
variableMethod · 0.80
linkAtMethod · 0.80
simpleMatchFunction · 0.70

Tested by

no test coverage detected