2012-05-20 11:57:07 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2022-02-05 11:45:17 +01:00
|
|
|
* Copyright (C) 2007-2022 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"
|
2020-05-23 07:16:49 +02:00
|
|
|
#include "errortypes.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
|
|
|
#include <ostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
class Function;
|
|
|
|
class Settings;
|
|
|
|
class Token;
|
|
|
|
class Tokenizer;
|
|
|
|
class Variable;
|
2020-05-23 07:16:49 +02:00
|
|
|
class ErrorLogger;
|
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 */
|
2021-08-07 20:51:18 +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)
|
2021-08-07 20:51:18 +02:00
|
|
|
: Check(myName(), tokenizer, settings, errorLogger) {}
|
2012-05-20 11:57:07 +02:00
|
|
|
|
|
|
|
/** @brief Run checks on the normal token list */
|
2022-02-10 23:02:24 +01:00
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) override {
|
2012-05-20 11:57:07 +02:00
|
|
|
CheckIO checkIO(tokenizer, settings, errorLogger);
|
|
|
|
|
|
|
|
checkIO.checkWrongPrintfScanfArguments();
|
|
|
|
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:
|
lib: fix a bunch of warnings about differing function arguments in definition and declaration.
[lib/token.h:72] -> [lib/token.cpp:36]: (style, inconclusive) Function 'Token' argument 1 names different: declaration 'tokensBack' definition 't'.
[lib/token.h:445] -> [lib/token.cpp:497]: (style, inconclusive) Function 'multiCompare' argument 1 names different: declaration 'needle' definition 'tok'.
[lib/checkio.h:73] -> [lib/checkio.cpp:1385]: (style, inconclusive) Function 'ArgumentInfo' argument 3 names different: declaration 'isCPP' definition '_isCPP'.
[lib/checkother.h:216] -> [lib/checkother.cpp:2136]: (style, inconclusive) Function 'checkComparisonFunctionIsAlwaysTrueOrFalseError' argument 2 names different: declaration 'strFunctionName' definition 'functionName'.
[lib/errorlogger.h:214] -> [lib/errorlogger.cpp:51]: (style, inconclusive) Function 'ErrorMessage' argument 2 names different: declaration 'file0' definition 'file0_'.
[lib/errorlogger.h:215] -> [lib/errorlogger.cpp:65]: (style, inconclusive) Function 'ErrorMessage' argument 2 names different: declaration 'file0' definition 'file0_'.
[lib/library.h:327] -> [lib/library.cpp:1043]: (style, inconclusive) Function 'ignorefunction' argument 1 names different: declaration 'function' definition 'functionName'.
[lib/mathlib.h:112] -> [lib/mathlib.cpp:1275]: (style, inconclusive) Function 'isNullValue' argument 1 names different: declaration 'tok' definition 'str'.
[lib/preprocessor.h:91] -> [lib/preprocessor.cpp:122]: (style, inconclusive) Function 'setDirectives' argument 1 names different: declaration 'tokens' definition 'tokens1'.
[lib/symboldatabase.h:860] -> [lib/symboldatabase.cpp:1801]: (style, inconclusive) Function 'argsMatch' argument 1 names different: declaration 'info' definition 'scope'.
[lib/symboldatabase.h:1171] -> [lib/symboldatabase.cpp:2048]: (style, inconclusive) Function 'addClassFunction' argument 1 names different: declaration 'info' definition 'scope'.
[lib/symboldatabase.h:1174] -> [lib/symboldatabase.cpp:2208]: (style, inconclusive) Function 'addNewFunction' argument 1 names different: declaration 'info' definition 'scope'.
[lib/symboldatabase.h:1090] -> [lib/symboldatabase.cpp:3648]: (style, inconclusive) Function 'findVariableType' argument 2 names different: declaration 'type' definition 'typeTok'.
[lib/symboldatabase.h:1101] -> [lib/symboldatabase.cpp:4308]: (style, inconclusive) Function 'findType' argument 1 names different: declaration 'tok' definition 'startTok'.
[lib/symboldatabase.h:1176] -> [lib/symboldatabase.cpp:4349]: (style, inconclusive) Function 'findTypeInNested' argument 1 names different: declaration 'tok' definition 'startTok'.
[lib/symboldatabase.h:1193] -> [lib/symboldatabase.cpp:4501]: (style, inconclusive) Function 'setValueType' argument 2 names different: declaration 'enumerators' definition 'enumerator'.
[lib/path.h:159] -> [lib/path.cpp:247]: (style, inconclusive) Function 'isCPP' argument 1 names different: declaration 'extensionInLowerCase' definition 'path'.
[lib/path.h:145] -> [lib/path.cpp:266]: (style, inconclusive) Function 'acceptFile' argument 1 names different: declaration 'filename' definition 'path'.
2017-04-03 00:06:46 +02:00
|
|
|
ArgumentInfo(const Token *arg, const Settings *settings, bool _isCPP);
|
2013-11-01 05:42:37 +01:00
|
|
|
~ArgumentInfo();
|
|
|
|
|
2022-06-07 21:25:23 +02:00
|
|
|
ArgumentInfo(const ArgumentInfo &) = delete;
|
|
|
|
ArgumentInfo& operator= (const ArgumentInfo &) = delete;
|
|
|
|
|
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);
|
2014-06-12 07:01:44 +02:00
|
|
|
bool isLibraryType(const Settings *settings) const;
|
2013-09-03 05:50:19 +02:00
|
|
|
|
|
|
|
const Variable *variableInfo;
|
|
|
|
const Token *typeToken;
|
|
|
|
const Function *functionInfo;
|
2015-08-15 19:16:48 +02:00
|
|
|
Token *tempToken;
|
2013-09-03 05:50:19 +02:00
|
|
|
bool element;
|
2013-09-08 20:21:00 +02:00
|
|
|
bool _template;
|
2013-09-22 06:56:31 +02:00
|
|
|
bool address;
|
2015-06-17 09:09:23 +02:00
|
|
|
bool isCPP;
|
2013-09-03 05:50:19 +02:00
|
|
|
};
|
2013-08-20 06:16:31 +02:00
|
|
|
|
2015-10-10 20:08:15 +02:00
|
|
|
void checkFormatString(const Token * const tok,
|
|
|
|
const Token * const formatStringTok,
|
|
|
|
const Token * argListTok,
|
|
|
|
const bool scan,
|
|
|
|
const bool scanf_s);
|
|
|
|
|
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);
|
2021-07-10 13:59:47 +02:00
|
|
|
void incompatibleFileOpenError(const Token *tok, const std::string &filename);
|
2015-08-14 08:03:46 +02:00
|
|
|
void invalidScanfError(const Token *tok);
|
2012-05-20 11:57:07 +02:00
|
|
|
void wrongPrintfScanfArgumentsError(const Token* tok,
|
2017-04-01 10:34:53 +02:00
|
|
|
const std::string &functionName,
|
2019-07-16 08:37:26 +02:00
|
|
|
nonneg int numFormat,
|
|
|
|
nonneg int numFunction);
|
2013-07-28 15:00:28 +02:00
|
|
|
void wrongPrintfScanfPosixParameterPositionError(const Token* tok, const std::string& functionName,
|
2021-08-07 20:51:18 +02:00
|
|
|
nonneg int index, nonneg int numFunction);
|
2019-07-16 08:37:26 +02:00
|
|
|
void invalidScanfArgTypeError_s(const Token* tok, nonneg int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
|
|
|
|
void invalidScanfArgTypeError_int(const Token* tok, nonneg int numFormat, const std::string& specifier, const ArgumentInfo* argInfo, bool isUnsigned);
|
|
|
|
void invalidScanfArgTypeError_float(const Token* tok, nonneg int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
|
|
|
|
void invalidPrintfArgTypeError_s(const Token* tok, nonneg int numFormat, const ArgumentInfo* argInfo);
|
|
|
|
void invalidPrintfArgTypeError_n(const Token* tok, nonneg int numFormat, const ArgumentInfo* argInfo);
|
|
|
|
void invalidPrintfArgTypeError_p(const Token* tok, nonneg int numFormat, const ArgumentInfo* argInfo);
|
|
|
|
void invalidPrintfArgTypeError_uint(const Token* tok, nonneg int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
|
|
|
|
void invalidPrintfArgTypeError_sint(const Token* tok, nonneg int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
|
|
|
|
void invalidPrintfArgTypeError_float(const Token* tok, nonneg int numFormat, const std::string& specifier, const ArgumentInfo* argInfo);
|
|
|
|
void invalidLengthModifierError(const Token* tok, nonneg int numFormat, const std::string& modifier);
|
2021-12-01 09:51:48 +01:00
|
|
|
void invalidScanfFormatWidthError(const Token* tok, nonneg int numFormat, int width, const Variable *var, const std::string& specifier);
|
2017-04-01 10:34:53 +02:00
|
|
|
static void argumentType(std::ostream & os, const ArgumentInfo * argInfo);
|
2017-02-27 13:22:10 +01:00
|
|
|
static Severity::SeverityType getSeverity(const ArgumentInfo *argInfo);
|
2012-05-20 11:57:07 +02:00
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
|
2016-05-07 16:30:54 +02:00
|
|
|
CheckIO c(nullptr, settings, errorLogger);
|
|
|
|
|
|
|
|
c.coutCerrMisusageError(nullptr, "cout");
|
|
|
|
c.fflushOnInputStreamError(nullptr, "stdin");
|
|
|
|
c.ioWithoutPositioningError(nullptr);
|
|
|
|
c.readWriteOnlyFileError(nullptr);
|
|
|
|
c.writeReadOnlyFileError(nullptr);
|
|
|
|
c.useClosedFileError(nullptr);
|
|
|
|
c.seekOnAppendedFileError(nullptr);
|
2021-07-10 13:59:47 +02:00
|
|
|
c.incompatibleFileOpenError(nullptr, "tmp");
|
2016-05-07 16:30:54 +02:00
|
|
|
c.invalidScanfError(nullptr);
|
|
|
|
c.wrongPrintfScanfArgumentsError(nullptr, "printf",3,2);
|
|
|
|
c.invalidScanfArgTypeError_s(nullptr, 1, "s", nullptr);
|
|
|
|
c.invalidScanfArgTypeError_int(nullptr, 1, "d", nullptr, false);
|
|
|
|
c.invalidScanfArgTypeError_float(nullptr, 1, "f", nullptr);
|
|
|
|
c.invalidPrintfArgTypeError_s(nullptr, 1, nullptr);
|
|
|
|
c.invalidPrintfArgTypeError_n(nullptr, 1, nullptr);
|
|
|
|
c.invalidPrintfArgTypeError_p(nullptr, 1, nullptr);
|
|
|
|
c.invalidPrintfArgTypeError_uint(nullptr, 1, "u", nullptr);
|
|
|
|
c.invalidPrintfArgTypeError_sint(nullptr, 1, "i", nullptr);
|
|
|
|
c.invalidPrintfArgTypeError_float(nullptr, 1, "f", nullptr);
|
|
|
|
c.invalidLengthModifierError(nullptr, 1, "I");
|
2021-12-01 09:51:48 +01:00
|
|
|
c.invalidScanfFormatWidthError(nullptr, 10, 5, nullptr, "s");
|
|
|
|
c.invalidScanfFormatWidthError(nullptr, 99, -1, nullptr, "s");
|
2016-05-07 16:30:54 +02:00
|
|
|
c.wrongPrintfScanfPosixParameterPositionError(nullptr, "printf", 2, 1);
|
2012-05-20 11:57:07 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
static std::string myName() {
|
2014-09-30 14:56:12 +02:00
|
|
|
return "IO using format string";
|
2012-05-20 11:57:07 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
std::string classInfo() const override {
|
2014-09-30 14:56:12 +02:00
|
|
|
return "Check format string input/output operations.\n"
|
|
|
|
"- Bad usage of the function 'sprintf' (overlapping data)\n"
|
|
|
|
"- Missing or wrong width specifiers in 'scanf' format string\n"
|
|
|
|
"- 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"
|
|
|
|
"- Repositioning operation on a file opened in append mode\n"
|
2021-07-10 13:59:47 +02:00
|
|
|
"- The same file can't be open for read and write at the same time on different streams\n"
|
2014-09-30 14:56:12 +02:00
|
|
|
"- Using fflush() on an input stream\n"
|
|
|
|
"- Invalid usage of output stream. For example: 'std::cout << std::cout;'\n"
|
|
|
|
"- 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
|