2012-05-20 11:57:07 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2014-02-15 07:45:39 +01:00
|
|
|
* Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team.
|
2012-05-20 11:57:07 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#ifndef checkioH
|
|
|
|
#define checkioH
|
2012-05-20 11:57:07 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "check.h"
|
2012-06-10 14:19:09 +02:00
|
|
|
#include "config.h"
|
2013-09-03 05:50:19 +02:00
|
|
|
#include "symboldatabase.h"
|
2012-06-23 07:52:52 +02:00
|
|
|
|
2012-05-20 11:57:07 +02:00
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/** @brief %Check input output operations. */
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB CheckIO : public Check {
|
2012-05-20 11:57:07 +02:00
|
|
|
public:
|
|
|
|
/** @brief This constructor is used when registering CheckIO */
|
2013-08-07 16:27:37 +02:00
|
|
|
CheckIO() : Check(myName()) {
|
|
|
|
}
|
2012-05-20 11:57:07 +02:00
|
|
|
|
|
|
|
/** @brief This constructor is used when running checks. */
|
|
|
|
CheckIO(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
2013-08-07 16:27:37 +02:00
|
|
|
: Check(myName(), tokenizer, settings, errorLogger) {
|
|
|
|
}
|
2012-05-20 11:57:07 +02:00
|
|
|
|
|
|
|
/** @brief Run checks on the normal token list */
|
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
|
|
|
CheckIO checkIO(tokenizer, settings, errorLogger);
|
|
|
|
|
|
|
|
checkIO.checkWrongPrintfScanfArguments();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief Run checks on the simplified token list */
|
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
|
|
|
CheckIO checkIO(tokenizer, settings, errorLogger);
|
|
|
|
|
|
|
|
checkIO.checkCoutCerrMisusage();
|
2012-05-22 14:30:22 +02:00
|
|
|
checkIO.checkFileUsage();
|
2012-05-20 11:57:07 +02:00
|
|
|
checkIO.invalidScanf();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief %Check for missusage of std::cout */
|
|
|
|
void checkCoutCerrMisusage();
|
|
|
|
|
2012-05-22 14:30:22 +02:00
|
|
|
/** @brief %Check usage of files*/
|
|
|
|
void checkFileUsage();
|
2012-05-20 11:57:07 +02:00
|
|
|
|
|
|
|
/** @brief scanf can crash if width specifiers are not used */
|
|
|
|
void invalidScanf();
|
|
|
|
|
|
|
|
/** @brief %Checks type and number of arguments given to functions like printf or scanf*/
|
|
|
|
void checkWrongPrintfScanfArguments();
|
|
|
|
|
|
|
|
private:
|
2013-09-03 05:50:19 +02:00
|
|
|
class ArgumentInfo {
|
|
|
|
public:
|
|
|
|
ArgumentInfo(const Token *arg, const Settings *settings);
|
2013-11-01 05:42:37 +01:00
|
|
|
~ArgumentInfo();
|
|
|
|
|
2013-09-08 20:21:00 +02:00
|
|
|
bool isArrayOrPointer() const;
|
2013-09-03 05:50:19 +02:00
|
|
|
bool isComplexType() const;
|
|
|
|
bool isKnownType() const;
|
2013-09-08 20:21:00 +02:00
|
|
|
bool isStdVectorOrString();
|
2013-09-07 07:20:06 +02:00
|
|
|
bool isStdContainer(const Token *tok);
|
2013-09-03 05:50:19 +02:00
|
|
|
|
|
|
|
const Variable *variableInfo;
|
|
|
|
const Token *typeToken;
|
|
|
|
const Function *functionInfo;
|
|
|
|
bool element;
|
2013-09-08 20:21:00 +02:00
|
|
|
bool _template;
|
2013-09-22 06:56:31 +02:00
|
|
|
bool address;
|
2013-09-03 05:50:19 +02:00
|
|
|
Token *tempToken;
|
2013-09-05 01:46:58 +02:00
|
|
|
|
2013-09-03 05:50:19 +02:00
|
|
|
private:
|
|
|
|
ArgumentInfo(const ArgumentInfo &); // not implemented
|
|
|
|
ArgumentInfo operator = (const ArgumentInfo &); // not implemented
|
|
|
|
};
|
2013-08-20 06:16:31 +02:00
|
|
|
|
2012-05-20 11:57:07 +02:00
|
|
|
// Reporting errors..
|
|
|
|
void coutCerrMisusageError(const Token* tok, const std::string& streamName);
|
|
|
|
void fflushOnInputStreamError(const Token *tok, const std::string &varname);
|
2012-05-22 14:30:22 +02:00
|
|
|
void ioWithoutPositioningError(const Token *tok);
|
|
|
|
void readWriteOnlyFileError(const Token *tok);
|
|
|
|
void writeReadOnlyFileError(const Token *tok);
|
|
|
|
void useClosedFileError(const Token *tok);
|
2014-03-17 11:02:03 +01:00
|
|
|
void seekOnAppendedFileError(const Token *tok);
|
2012-07-07 13:34:37 +02:00
|
|
|
void invalidScanfError(const Token *tok, bool portability);
|
2012-05-20 11:57:07 +02:00
|
|
|
void wrongPrintfScanfArgumentsError(const Token* tok,
|
|
|
|
const std::string &function,
|
|
|
|
unsigned int numFormat,
|
|
|
|
unsigned int numFunction);
|
2013-07-28 15:00:28 +02:00
|
|
|
void wrongPrintfScanfPosixParameterPositionError(const Token* tok, const std::string& functionName,
|
2013-07-28 16:13:16 +02:00
|
|
|
unsigned int index, unsigned int numFunction);
|
2013-09-22 06:56:31 +02:00
|
|
|
void invalidScanfArgTypeError_s(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
|
|
|
|
void invalidScanfArgTypeError_int(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo, bool isUnsigned);
|
|
|
|
void invalidScanfArgTypeError_float(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
|
2013-10-27 10:48:49 +01:00
|
|
|
void invalidPrintfArgTypeError_s(const Token* tok, unsigned int numFormat, const ArgumentInfo* argInfo);
|
2013-09-22 06:56:31 +02:00
|
|
|
void invalidPrintfArgTypeError_n(const Token* tok, unsigned int numFormat, const ArgumentInfo* argInfo);
|
2013-09-05 01:46:58 +02:00
|
|
|
void invalidPrintfArgTypeError_p(const Token* tok, unsigned int numFormat, const ArgumentInfo* argInfo);
|
|
|
|
void invalidPrintfArgTypeError_int(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
|
|
|
|
void invalidPrintfArgTypeError_uint(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
|
|
|
|
void invalidPrintfArgTypeError_sint(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
|
|
|
|
void invalidPrintfArgTypeError_float(const Token* tok, unsigned int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
|
2013-08-10 12:37:31 +02:00
|
|
|
void invalidLengthModifierError(const Token* tok, unsigned int numFormat, const std::string& modifier);
|
2012-07-13 19:36:58 +02:00
|
|
|
void invalidScanfFormatWidthError(const Token* tok, unsigned int numFormat, int width, const Variable *var);
|
2013-09-05 01:46:58 +02:00
|
|
|
void argumentType(std::ostream & s, const ArgumentInfo * argInfo);
|
2012-05-20 11:57:07 +02:00
|
|
|
|
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
|
|
|
|
CheckIO c(0, settings, errorLogger);
|
|
|
|
|
|
|
|
c.coutCerrMisusageError(0, "cout");
|
|
|
|
c.fflushOnInputStreamError(0, "stdin");
|
2012-05-22 14:30:22 +02:00
|
|
|
c.ioWithoutPositioningError(0);
|
|
|
|
c.readWriteOnlyFileError(0);
|
|
|
|
c.writeReadOnlyFileError(0);
|
|
|
|
c.useClosedFileError(0);
|
2014-03-17 11:02:03 +01:00
|
|
|
c.seekOnAppendedFileError(0);
|
2012-07-07 13:34:37 +02:00
|
|
|
c.invalidScanfError(0, false);
|
2012-05-20 11:57:07 +02:00
|
|
|
c.wrongPrintfScanfArgumentsError(0,"printf",3,2);
|
2013-09-22 06:56:31 +02:00
|
|
|
c.invalidScanfArgTypeError_s(0, 1, "s", NULL);
|
|
|
|
c.invalidScanfArgTypeError_int(0, 1, "d", NULL, false);
|
|
|
|
c.invalidScanfArgTypeError_float(0, 1, "f", NULL);
|
2013-10-27 10:48:49 +01:00
|
|
|
c.invalidPrintfArgTypeError_s(0, 1, NULL);
|
2013-09-22 06:56:31 +02:00
|
|
|
c.invalidPrintfArgTypeError_n(0, 1, NULL);
|
2013-08-31 06:26:39 +02:00
|
|
|
c.invalidPrintfArgTypeError_p(0, 1, NULL);
|
|
|
|
c.invalidPrintfArgTypeError_int(0, 1, "X", NULL);
|
|
|
|
c.invalidPrintfArgTypeError_uint(0, 1, "u", NULL);
|
|
|
|
c.invalidPrintfArgTypeError_sint(0, 1, "i", NULL);
|
|
|
|
c.invalidPrintfArgTypeError_float(0, 1, "f", NULL);
|
2012-07-13 19:36:58 +02:00
|
|
|
c.invalidScanfFormatWidthError(0, 10, 5, NULL);
|
2013-07-28 20:33:49 +02:00
|
|
|
c.wrongPrintfScanfPosixParameterPositionError(0, "printf", 2, 1);
|
2012-05-20 11:57:07 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 18:50:48 +02:00
|
|
|
static std::string myName() {
|
2012-05-20 11:57:07 +02:00
|
|
|
return "IO";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string classInfo() const {
|
2012-05-20 17:40:39 +02:00
|
|
|
return "Check input/output operations.\n"
|
2012-05-22 14:30:22 +02:00
|
|
|
"* Bad usage of the function 'sprintf' (overlapping data)\n"
|
2012-07-13 19:36:58 +02:00
|
|
|
"* Missing or wrong width specifiers in 'scanf' format string\n"
|
2012-05-22 14:30:22 +02:00
|
|
|
"* Use a file that has been closed\n"
|
|
|
|
"* File input/output without positioning results in undefined behaviour\n"
|
|
|
|
"* Read to a file that has only been opened for writing (or vice versa)\n"
|
2014-03-17 11:02:03 +01:00
|
|
|
"* Repositioning operation on a file opened in append mode\n"
|
2012-05-22 14:30:22 +02:00
|
|
|
"* Using fflush() on an input stream\n"
|
2012-07-13 19:36:58 +02:00
|
|
|
"* Invalid usage of output stream. For example: 'std::cout << std::cout;'\n"
|
2012-05-22 14:30:22 +02:00
|
|
|
"* Wrong number of arguments given to 'printf' or 'scanf;'\n";
|
2012-05-20 11:57:07 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checkioH
|