Refactoring CheckString::sprintfOverlappingData. Use AST, isSameExpression(), getArguments(), ..

This commit is contained in:
Daniel Marjamäki 2017-04-23 10:51:31 +02:00
parent d2b85316e6
commit f2719ec6ca
2 changed files with 16 additions and 25 deletions

View File

@ -366,7 +366,7 @@ $(SRCDIR)/checksizeof.o: lib/checksizeof.cpp lib/cxx11emu.h lib/checksizeof.h li
$(SRCDIR)/checkstl.o: lib/checkstl.cpp lib/cxx11emu.h lib/checkstl.h lib/config.h lib/check.h lib/token.h lib/valueflow.h lib/mathlib.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/library.h lib/standards.h lib/platform.h lib/importproject.h lib/timer.h lib/symboldatabase.h lib/checknullpointer.h lib/utils.h $(SRCDIR)/checkstl.o: lib/checkstl.cpp lib/cxx11emu.h lib/checkstl.h lib/config.h lib/check.h lib/token.h lib/valueflow.h lib/mathlib.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/library.h lib/standards.h lib/platform.h lib/importproject.h lib/timer.h lib/symboldatabase.h lib/checknullpointer.h lib/utils.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CFG) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(SRCDIR)/checkstl.o $(SRCDIR)/checkstl.cpp $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CFG) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(SRCDIR)/checkstl.o $(SRCDIR)/checkstl.cpp
$(SRCDIR)/checkstring.o: lib/checkstring.cpp lib/cxx11emu.h lib/checkstring.h lib/config.h lib/check.h lib/token.h lib/valueflow.h lib/mathlib.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/library.h lib/standards.h lib/platform.h lib/importproject.h lib/timer.h lib/symboldatabase.h lib/utils.h $(SRCDIR)/checkstring.o: lib/checkstring.cpp lib/cxx11emu.h lib/checkstring.h lib/config.h lib/check.h lib/token.h lib/valueflow.h lib/mathlib.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/library.h lib/standards.h lib/platform.h lib/importproject.h lib/timer.h lib/symboldatabase.h lib/astutils.h lib/utils.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CFG) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(SRCDIR)/checkstring.o $(SRCDIR)/checkstring.cpp $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CFG) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(SRCDIR)/checkstring.o $(SRCDIR)/checkstring.cpp
$(SRCDIR)/checktype.o: lib/checktype.cpp lib/cxx11emu.h lib/checktype.h lib/config.h lib/check.h lib/token.h lib/valueflow.h lib/mathlib.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/library.h lib/standards.h lib/platform.h lib/importproject.h lib/timer.h lib/symboldatabase.h $(SRCDIR)/checktype.o: lib/checktype.cpp lib/cxx11emu.h lib/checktype.h lib/config.h lib/check.h lib/token.h lib/valueflow.h lib/mathlib.h lib/tokenize.h lib/errorlogger.h lib/suppressions.h lib/tokenlist.h lib/settings.h lib/library.h lib/standards.h lib/platform.h lib/importproject.h lib/timer.h lib/symboldatabase.h

View File

@ -20,6 +20,7 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "checkstring.h" #include "checkstring.h"
#include "symboldatabase.h" #include "symboldatabase.h"
#include "astutils.h"
#include "utils.h" #include "utils.h"
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -321,6 +322,7 @@ void CheckString::incorrectStringBooleanError(const Token *tok, const std::strin
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Overlapping source and destination passed to sprintf(). // Overlapping source and destination passed to sprintf().
// TODO: Library configuration for overlapping arguments
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CheckString::sprintfOverlappingData() void CheckString::sprintfOverlappingData()
{ {
@ -329,34 +331,23 @@ void CheckString::sprintfOverlappingData()
for (std::size_t i = 0; i < functions; ++i) { for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i]; const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
// Get variable id of target buffer.. if (!Token::Match(tok, "sprintf|snprintf|swprintf ("))
unsigned int varid = 0;
if (Token::Match(tok, "sprintf|snprintf|swprintf ( %var% ,"))
varid = tok->tokAt(2)->varId();
else if (Token::Match(tok, "sprintf|snprintf|swprintf ( %name% . %var% ,"))
varid = tok->tokAt(4)->varId();
else
continue; continue;
// goto next argument const std::vector<const Token *> args = getArguments(tok);
const Token *tok2 = tok->tokAt(2)->nextArgument();
if (tok->str() == "snprintf" || tok->str() == "swprintf") { // Jump over second parameter for snprintf and swprintf const int formatString = Token::Match(tok, "sprintf") ? 1 : 2;
tok2 = tok2->nextArgument(); for (unsigned int argnr = formatString + 1; argnr < args.size(); ++argnr) {
if (!tok2) bool same = isSameExpression(_tokenizer->isCPP(),
continue; false,
} args[0],
args[argnr],
// is any source buffer overlapping the target buffer? _settings->library,
do { true);
if (Token::Match(tok2, "%varid% [,)]", varid)) { if (same) {
sprintfOverlappingDataError(tok2, tok2->str()); sprintfOverlappingDataError(args[argnr], args[argnr]->expressionString());
break;
} }
} while (nullptr != (tok2 = tok2->nextArgument())); }
} }
} }
} }