2009-01-31 20:29:27 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki, Reijo Tomperi, Nicolas Le Cam,
|
2009-03-01 20:52:33 +01:00
|
|
|
* Leandro Penz, Kimmo Varis, Vesa Pikki
|
2009-01-31 20:29:27 +01:00
|
|
|
*
|
|
|
|
* 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"
|
2009-02-11 06:16:10 +01:00
|
|
|
#include "settings.h"
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
class ErrorLogger;
|
|
|
|
class Token;
|
|
|
|
class Tokenizer;
|
2009-01-31 20:29:27 +01:00
|
|
|
|
2009-03-20 17:35:53 +01:00
|
|
|
class CheckBufferOverrunClass : public Check
|
2009-01-31 20:29:27 +01:00
|
|
|
{
|
|
|
|
public:
|
2009-03-20 17:35:53 +01:00
|
|
|
|
2009-03-21 07:53:23 +01:00
|
|
|
/** This constructor is used when registering the CheckClass */
|
2009-03-20 17:35:53 +01:00
|
|
|
CheckBufferOverrunClass() : Check()
|
|
|
|
{ }
|
|
|
|
|
2009-03-21 07:53:23 +01:00
|
|
|
/** This constructor is used when running checks.. */
|
2009-03-20 17:35:53 +01:00
|
|
|
CheckBufferOverrunClass(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
|
|
|
: Check(tokenizer, settings, errorLogger)
|
|
|
|
{ }
|
|
|
|
|
2009-03-21 07:53:23 +01:00
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
2009-03-20 17:35:53 +01:00
|
|
|
{
|
|
|
|
CheckBufferOverrunClass checkBufferOverrunClass(tokenizer, settings, errorLogger);
|
|
|
|
if (settings->_showAll)
|
|
|
|
checkBufferOverrunClass.bufferOverrun();
|
|
|
|
}
|
2009-01-31 20:29:27 +01:00
|
|
|
|
|
|
|
/** Check for buffer overruns */
|
|
|
|
void bufferOverrun();
|
|
|
|
private:
|
|
|
|
|
|
|
|
/** Check for buffer overruns - locate struct variables and check them with the .._CheckScope function */
|
|
|
|
void CheckBufferOverrun_StructVariable();
|
|
|
|
|
2009-02-11 17:12:29 +01:00
|
|
|
/** Check for buffer overruns - locate global variables and local function variables and check them with the .._CheckScope function */
|
|
|
|
void CheckBufferOverrun_GlobalAndLocalVariable();
|
2009-01-31 20:29:27 +01:00
|
|
|
|
|
|
|
/** Check for buffer overruns - this is the function that performs the actual checking */
|
|
|
|
void CheckBufferOverrun_CheckScope(const Token *tok, const char *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);
|
2009-03-21 21:33:27 +01:00
|
|
|
void arrayIndexOutOfBounds();
|
|
|
|
void bufferOverrun(const Token *tok);
|
|
|
|
void strncatUsage(const Token *tok);
|
|
|
|
void outOfBounds(const Token *tok, const std::string &what);
|
2009-03-25 07:25:10 +01:00
|
|
|
void sizeArgumentAsChar(const Token *tok);
|
2009-03-22 08:20:15 +01:00
|
|
|
|
|
|
|
void getErrorMessages()
|
|
|
|
{
|
|
|
|
arrayIndexOutOfBounds(0);
|
|
|
|
bufferOverrun(0);
|
|
|
|
strncatUsage(0);
|
|
|
|
outOfBounds(0, "index");
|
2009-03-25 07:25:10 +01:00
|
|
|
sizeArgumentAsChar(0);
|
2009-03-22 08:20:15 +01:00
|
|
|
}
|
2009-01-31 20:29:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif
|
|
|
|
|