cppcheck/lib/checkbufferoverrun.h

139 lines
4.6 KiB
C
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
#ifndef CheckBufferOverrunH
#define CheckBufferOverrunH
//---------------------------------------------------------------------------
2009-03-20 17:35:53 +01:00
#include "check.h"
#include "settings.h"
#include <list>
#include <vector>
#include <string>
class ErrorLogger;
class Token;
class Tokenizer;
/// @addtogroup Checks
/// @{
/**
* @brief buffer overruns and array index out of bounds
*
* Buffer overrun and array index out of bounds are pretty much the same.
* But I generally use 'array index' if the code contains []. And the given
* index is out of bounds.
* I generally use 'buffer overrun' if you for example call a strcpy or
* other function and pass a buffer and reads or writes too much data.
*/
2009-07-13 16:00:15 +02:00
class CheckBufferOverrun : public Check
{
public:
2009-03-20 17:35:53 +01:00
/** This constructor is used when registering the CheckClass */
2009-07-13 16:00:15 +02:00
CheckBufferOverrun() : Check()
2009-03-20 17:35:53 +01:00
{ }
2010-03-17 22:16:18 +01:00
/** This constructor is used when running checks. */
2009-07-13 16:00:15 +02:00
CheckBufferOverrun(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(tokenizer, settings, errorLogger)
2009-03-20 17:35:53 +01:00
{ }
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
2009-03-20 17:35:53 +01:00
{
2009-07-13 16:00:15 +02:00
CheckBufferOverrun checkBufferOverrun(tokenizer, settings, errorLogger);
checkBufferOverrun.bufferOverrun();
/** ExecutionPath checking.. */
checkBufferOverrun.executionPaths();
2009-03-20 17:35:53 +01:00
}
/** @brief %Check for buffer overruns */
void bufferOverrun();
/** @brief %Check for buffer overruns by inspecting execution paths */
void executionPaths();
/**
* @brief Get minimum length of format string result
* @param input_string format string
* @param parameters given parameters to sprintf
* @return minimum length of resulting string
*/
static int countSprintfLength(const std::string &input_string, const std::list<const Token*> &parameters);
/**
* @brief %Check code that matches: "sprintf ( %varid% , %str% [,)]" when varid is not 0,
* and report found errors.
* @param tok The "sprintf" token.
* @param size The size of the buffer where sprintf is writing.
*/
void checkSprintfCall(const Token *tok, int size);
/** Check for buffer overruns - locate struct variables and check them with the .._CheckScope function */
void checkStructVariable();
/** Check for buffer overruns - locate global variables and local function variables and check them with the checkScope function */
void checkGlobalAndLocalVariable();
/** Check for buffer overruns - this is the function that performs the actual checking */
void checkScope(const Token *tok, const std::vector<std::string> &varname, const int size, const int total_size, unsigned int varid);
/** callstack - used during intra-function checking */
std::list<const Token *> _callStack;
2009-02-01 16:47:36 +01:00
void arrayIndexOutOfBounds(const Token *tok, int size, int index);
void arrayIndexOutOfBounds(int size, int index);
void bufferOverrun(const Token *tok, const std::string &varnames = "");
void dangerousStdCin(const Token *tok);
2009-03-21 21:33:27 +01:00
void strncatUsage(const Token *tok);
void outOfBounds(const Token *tok, const std::string &what);
void sizeArgumentAsChar(const Token *tok);
void terminateStrncpyError(const Token *tok);
void getErrorMessages()
{
arrayIndexOutOfBounds(0, 2, 2);
bufferOverrun(0, std::string("buffer"));
dangerousStdCin(0);
strncatUsage(0);
outOfBounds(0, "index");
sizeArgumentAsChar(0);
terminateStrncpyError(0);
}
2009-06-12 15:20:08 +02:00
std::string name() const
{
return "Bounds checking";
}
std::string classInfo() const
{
return "out of bounds checking";
}
};
/// @}
//---------------------------------------------------------------------------
#endif