2009-01-31 20:29:27 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2010-04-13 21:23:17 +02:00
|
|
|
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
|
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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-01-31 20:29:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#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"
|
2010-11-21 11:48:27 +01:00
|
|
|
#include "mathlib.h"
|
2009-02-11 06:16:10 +01:00
|
|
|
#include <list>
|
2010-02-14 19:58:17 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2009-02-11 06:16:10 +01:00
|
|
|
|
|
|
|
class ErrorLogger;
|
|
|
|
class Token;
|
|
|
|
class Tokenizer;
|
2009-01-31 20:29:27 +01:00
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
2010-03-13 21:42:59 +01:00
|
|
|
/**
|
|
|
|
* @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
|
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-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)
|
2010-04-15 20:08:51 +02:00
|
|
|
: Check(tokenizer, settings, errorLogger)
|
2009-03-20 17:35:53 +01:00
|
|
|
{ }
|
|
|
|
|
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
|
|
|
{
|
2009-07-13 16:00:15 +02:00
|
|
|
CheckBufferOverrun checkBufferOverrun(tokenizer, settings, errorLogger);
|
2009-10-13 21:39:51 +02:00
|
|
|
checkBufferOverrun.bufferOverrun();
|
2010-04-18 20:51:39 +02:00
|
|
|
checkBufferOverrun.negativeIndex();
|
2010-04-10 21:12:00 +02:00
|
|
|
|
|
|
|
/** ExecutionPath checking.. */
|
|
|
|
checkBufferOverrun.executionPaths();
|
2009-03-20 17:35:53 +01:00
|
|
|
}
|
2009-01-31 20:29:27 +01:00
|
|
|
|
2010-03-13 21:42:59 +01:00
|
|
|
/** @brief %Check for buffer overruns */
|
2009-01-31 20:29:27 +01:00
|
|
|
void bufferOverrun();
|
2010-03-13 21:42:59 +01:00
|
|
|
|
2010-04-10 21:12:00 +02:00
|
|
|
/** @brief %Check for buffer overruns by inspecting execution paths */
|
|
|
|
void executionPaths();
|
|
|
|
|
2010-03-13 21:42:59 +01:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2010-08-06 21:02:43 +02:00
|
|
|
static unsigned int countSprintfLength(const std::string &input_string, const std::list<const Token*> ¶meters);
|
2009-09-25 18:23:44 +02:00
|
|
|
|
2009-10-07 14:37:20 +02:00
|
|
|
/**
|
2010-03-13 21:42:59 +01:00
|
|
|
* @brief %Check code that matches: "sprintf ( %varid% , %str% [,)]" when varid is not 0,
|
2009-10-07 14:37:20 +02:00
|
|
|
* and report found errors.
|
|
|
|
* @param tok The "sprintf" token.
|
|
|
|
* @param size The size of the buffer where sprintf is writing.
|
|
|
|
*/
|
2010-08-06 21:02:43 +02:00
|
|
|
void checkSprintfCall(const Token *tok, const unsigned int size);
|
2009-10-07 14:37:20 +02:00
|
|
|
|
2009-01-31 20:29:27 +01:00
|
|
|
/** Check for buffer overruns - locate struct variables and check them with the .._CheckScope function */
|
2009-07-05 22:16:43 +02:00
|
|
|
void checkStructVariable();
|
2009-01-31 20:29:27 +01:00
|
|
|
|
2009-07-05 22:16:43 +02:00
|
|
|
/** Check for buffer overruns - locate global variables and local function variables and check them with the checkScope function */
|
|
|
|
void checkGlobalAndLocalVariable();
|
2009-01-31 20:29:27 +01:00
|
|
|
|
2010-05-26 10:56:34 +02:00
|
|
|
/** Check for buffer overruns due to allocating strlen(src) bytes instead of (strlen(src)+1) bytes before copying a string */
|
|
|
|
void checkBufferAllocatedWithStrlen();
|
2010-06-02 18:09:25 +02:00
|
|
|
|
2010-06-02 07:41:07 +02:00
|
|
|
/** Check for buffer overruns due to copying command-line args to fixed-sized buffers without bounds checking */
|
|
|
|
void checkInsecureCmdLineArgs();
|
2010-05-26 10:56:34 +02:00
|
|
|
|
2010-04-18 20:51:39 +02:00
|
|
|
/** Check for negative index */
|
|
|
|
void negativeIndex();
|
|
|
|
|
2010-04-21 18:33:21 +02:00
|
|
|
/** Check for buffer overruns */
|
2010-11-21 11:48:27 +01:00
|
|
|
void checkScope(const Token *tok, const std::vector<std::string> &varname, const MathLib::bigint size, const MathLib::bigint total_size, unsigned int varid);
|
2009-01-31 20:29:27 +01:00
|
|
|
|
2010-04-18 11:08:29 +02:00
|
|
|
/** Information about N-dimensional array */
|
|
|
|
class ArrayInfo
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
/** number of elements of array */
|
|
|
|
std::vector<unsigned int> _num;
|
|
|
|
|
|
|
|
/** size of each element in array */
|
2010-04-18 20:18:25 +02:00
|
|
|
unsigned int _element_size;
|
2010-04-18 11:08:29 +02:00
|
|
|
|
2010-04-18 19:46:45 +02:00
|
|
|
/** variable id */
|
|
|
|
unsigned int _varid;
|
|
|
|
|
2010-04-18 11:08:29 +02:00
|
|
|
/** full name of variable as pattern */
|
|
|
|
std::string _varname;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ArrayInfo();
|
|
|
|
ArrayInfo(const ArrayInfo &);
|
2010-09-15 22:25:12 +02:00
|
|
|
ArrayInfo & operator=(const ArrayInfo &ai);
|
2010-04-18 11:08:29 +02:00
|
|
|
|
2010-04-21 18:33:21 +02:00
|
|
|
/**
|
|
|
|
* Create array info with specified data
|
|
|
|
* The intention is that this is only a temporary solution.. all
|
|
|
|
* checking should be based on ArrayInfo from the start and then
|
|
|
|
* this will not be needed as the declare can be used instead.
|
|
|
|
*/
|
|
|
|
ArrayInfo(unsigned int id, const std::string &name, unsigned int size1, unsigned int n);
|
|
|
|
|
2010-04-24 21:48:58 +02:00
|
|
|
/** Create a copy ArrayInfo where the number of elements have been limited by a value */
|
2010-11-21 11:48:27 +01:00
|
|
|
ArrayInfo limit(MathLib::bigint value) const;
|
2010-04-24 21:48:58 +02:00
|
|
|
|
2010-04-18 11:08:29 +02:00
|
|
|
/**
|
|
|
|
* Declare array - set info
|
2010-04-18 19:46:45 +02:00
|
|
|
* \param tok first token in array declaration
|
|
|
|
* \param tokenizer The tokenizer (for type size)
|
2010-04-18 11:08:29 +02:00
|
|
|
* \return success => true
|
|
|
|
*/
|
2010-04-18 19:46:45 +02:00
|
|
|
bool declare(const Token *tok, const Tokenizer &tokenizer);
|
2010-04-18 11:08:29 +02:00
|
|
|
|
|
|
|
/** array size */
|
|
|
|
const std::vector<unsigned int> #
|
|
|
|
|
2010-04-18 20:18:25 +02:00
|
|
|
/** size of each element */
|
|
|
|
const unsigned int &element_size;
|
2010-04-18 11:08:29 +02:00
|
|
|
|
2010-04-18 19:46:45 +02:00
|
|
|
/** Variable name */
|
|
|
|
const unsigned int &varid;
|
|
|
|
|
2010-04-18 11:08:29 +02:00
|
|
|
/** Variable name */
|
|
|
|
const std::string &varname;
|
|
|
|
};
|
|
|
|
|
2010-04-21 18:33:21 +02:00
|
|
|
/** Check for buffer overruns (based on ArrayInfo) */
|
|
|
|
void checkScope(const Token *tok, const ArrayInfo &arrayInfo);
|
|
|
|
|
|
|
|
|
|
|
|
/** Helper function used when parsing for-loops */
|
|
|
|
void parse_for_body(const Token *tok2, const ArrayInfo &arrayInfo, const std::string &strindex, bool condition_out_of_bounds, unsigned int counter_varid, const std::string &min_counter_value, const std::string &max_counter_value);
|
|
|
|
|
2010-04-21 20:02:58 +02:00
|
|
|
/**
|
|
|
|
* Helper function for checkScope - check a function call
|
|
|
|
* \param tok token for the function name
|
|
|
|
* \param par on what parameter is the array used
|
|
|
|
* \param arrayInfo the array information
|
|
|
|
*/
|
|
|
|
void checkFunctionCall(const Token &tok, const unsigned int par, const ArrayInfo &arrayInfo);
|
2010-04-18 11:08:29 +02:00
|
|
|
|
2010-11-21 11:48:27 +01:00
|
|
|
void arrayIndexOutOfBounds(const Token *tok, MathLib::bigint size, MathLib::bigint index);
|
2010-08-06 21:02:43 +02:00
|
|
|
void arrayIndexOutOfBounds(const Token *tok, const ArrayInfo &arrayInfo, const std::vector<unsigned int> &index);
|
|
|
|
void arrayIndexOutOfBounds(const std::list<const Token *> &callstack, const ArrayInfo &arrayInfo, const std::vector<unsigned int> &index);
|
2010-02-21 15:23:50 +01:00
|
|
|
void bufferOverrun(const Token *tok, const std::string &varnames = "");
|
2009-03-21 21:33:27 +01:00
|
|
|
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-12-18 17:26:15 +01:00
|
|
|
void terminateStrncpyError(const Token *tok);
|
2010-11-21 11:48:27 +01:00
|
|
|
void negativeIndexError(const Token *tok, MathLib::bigint index);
|
2010-06-02 07:41:07 +02:00
|
|
|
void cmdLineArgsError(const Token *tok);
|
2010-12-26 21:23:28 +01:00
|
|
|
void pointerOutOfBounds(const Token *tok); // UB when result of calculation is out of bounds
|
2009-03-22 08:20:15 +01:00
|
|
|
|
|
|
|
void getErrorMessages()
|
|
|
|
{
|
2009-12-25 15:25:58 +01:00
|
|
|
arrayIndexOutOfBounds(0, 2, 2);
|
2010-02-21 15:23:50 +01:00
|
|
|
bufferOverrun(0, std::string("buffer"));
|
2009-03-22 08:20:15 +01:00
|
|
|
strncatUsage(0);
|
|
|
|
outOfBounds(0, "index");
|
2009-03-25 07:25:10 +01:00
|
|
|
sizeArgumentAsChar(0);
|
2009-12-18 17:33:33 +01:00
|
|
|
terminateStrncpyError(0);
|
2010-04-18 20:51:39 +02:00
|
|
|
negativeIndexError(0, -1);
|
2010-06-02 07:41:07 +02:00
|
|
|
cmdLineArgsError(0);
|
2010-12-26 21:23:28 +01:00
|
|
|
pointerOutOfBounds(0);
|
2009-03-22 08:20:15 +01:00
|
|
|
}
|
2009-06-12 12:19:37 +02:00
|
|
|
|
2009-06-12 15:20:08 +02:00
|
|
|
std::string name() const
|
|
|
|
{
|
|
|
|
return "Bounds checking";
|
|
|
|
}
|
|
|
|
|
2009-06-12 12:19:37 +02:00
|
|
|
std::string classInfo() const
|
|
|
|
{
|
|
|
|
return "out of bounds checking";
|
|
|
|
}
|
2009-01-31 20:29:27 +01:00
|
|
|
};
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @}
|
2009-01-31 20:29:27 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif
|
|
|
|
|
2009-09-25 18:23:44 +02:00
|
|
|
|
|
|
|
|