cppcheck/lib/checkbufferoverrun.h

125 lines
4.0 KiB
C
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
2019-02-09 07:24:06 +01:00
* Copyright (C) 2007-2019 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"
2017-05-27 04:33:47 +02:00
#include "config.h"
#include "errorlogger.h"
#include "mathlib.h"
2017-05-27 04:33:47 +02:00
#include "tokenize.h"
2017-05-27 04:33:47 +02:00
#include <cstddef>
#include <list>
2017-05-27 04:33:47 +02:00
#include <map>
#include <string>
2017-05-27 04:33:47 +02:00
#include <vector>
class Settings;
class SymbolDatabase;
class Token;
namespace ValueFlow {
2017-05-28 15:56:26 +02:00
class Value;
2017-05-27 04:33:47 +02:00
} // namespace ValueFlow
namespace tinyxml2 {
2017-05-28 15:56:26 +02:00
class XMLElement;
2017-05-27 04:33:47 +02:00
} // namespace tinyxml2
// CWE ids used
static const struct CWE CWE119(119U); // Improper Restriction of Operations within the Bounds of a Memory Buffer
class Variable;
/// @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.
*/
class CPPCHECKLIB CheckBufferOverrun : public Check {
public:
2009-03-20 17:35:53 +01:00
/** This constructor is used when registering the CheckClass */
2018-06-17 17:20:16 +02:00
CheckBufferOverrun() : Check(myName()) {
}
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)
2018-06-17 17:20:16 +02:00
: Check(myName(), tokenizer, settings, errorLogger) {
}
2009-03-20 17:35:53 +01:00
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) OVERRIDE {
2009-07-13 16:00:15 +02:00
CheckBufferOverrun checkBufferOverrun(tokenizer, settings, errorLogger);
checkBufferOverrun.arrayIndex();
checkBufferOverrun.bufferOverflow();
checkBufferOverrun.arrayIndexThenCheck();
2009-03-20 17:35:53 +01:00
}
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) OVERRIDE {
(void)tokenizer;
(void)settings;
(void)errorLogger;
}
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const OVERRIDE {
CheckBufferOverrun c(nullptr, settings, errorLogger);
c.arrayIndexError(nullptr, nullptr, nullptr);
c.negativeIndexError(nullptr, nullptr, nullptr);
c.arrayIndexThenCheckError(nullptr, "i");
}
private:
void arrayIndex();
void arrayIndexError(const Token *tok, const Variable *var, const ValueFlow::Value *index);
void negativeIndexError(const Token *tok, const Variable *var, const ValueFlow::Value *negativeValue);
void bufferOverflow();
void bufferOverflowError(const Token *tok);
void arrayIndexThenCheck();
void arrayIndexThenCheckError(const Token *tok, const std::string &indexName);
size_t getBufferSize(const Token *bufTok) const;
2014-11-20 14:20:09 +01:00
static std::string myName() {
2009-06-12 15:20:08 +02:00
return "Bounds checking";
}
std::string classInfo() const OVERRIDE {
return "Out of bounds checking:\n"
"- Array index out of bounds\n"
"- Buffer overflow\n"
"- Dangerous usage of strncat()\n"
"- Using array index before checking it\n";
}
};
/// @}
//---------------------------------------------------------------------------
#endif // checkbufferoverrunH