1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2021 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 //---------------------------------------------------------------------------
21 #ifndef checkvaargtH
22 #define checkvaargtH
23 //---------------------------------------------------------------------------
24 
25 #include "check.h"
26 #include "config.h"
27 
28 #include <string>
29 
30 class ErrorLogger;
31 class Settings;
32 class Token;
33 class Tokenizer;
34 
35 /// @addtogroup Checks
36 /// @{
37 
38 /**
39  * @brief Checking for misusage of variable argument lists
40  */
41 
42 class CPPCHECKLIB CheckVaarg : public Check {
43 public:
CheckVaarg()44     CheckVaarg() : Check(myName()) {}
45 
CheckVaarg(const Tokenizer * tokenizer,const Settings * settings,ErrorLogger * errorLogger)46     CheckVaarg(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
47         : Check(myName(), tokenizer, settings, errorLogger) {}
48 
runChecks(const Tokenizer * tokenizer,const Settings * settings,ErrorLogger * errorLogger)49     void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) OVERRIDE {
50         CheckVaarg check(tokenizer, settings, errorLogger);
51         check.va_start_argument();
52         check.va_list_usage();
53     }
54 
55     void va_start_argument();
56     void va_list_usage();
57 
58 private:
59     void wrongParameterTo_va_start_error(const Token *tok, const std::string& paramIsName, const std::string& paramShouldName);
60     void referenceAs_va_start_error(const Token *tok, const std::string& paramName);
61     void va_end_missingError(const Token *tok, const std::string& varname);
62     void va_list_usedBeforeStartedError(const Token *tok, const std::string& varname);
63     void va_start_subsequentCallsError(const Token *tok, const std::string& varname);
64 
getErrorMessages(ErrorLogger * errorLogger,const Settings * settings)65     void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const OVERRIDE {
66         CheckVaarg c(nullptr, settings, errorLogger);
67         c.wrongParameterTo_va_start_error(nullptr, "arg1", "arg2");
68         c.referenceAs_va_start_error(nullptr, "arg1");
69         c.va_end_missingError(nullptr, "vl");
70         c.va_list_usedBeforeStartedError(nullptr, "vl");
71         c.va_start_subsequentCallsError(nullptr, "vl");
72     }
73 
myName()74     static std::string myName() {
75         return "Vaarg";
76     }
77 
classInfo()78     std::string classInfo() const OVERRIDE {
79         return "Check for misusage of variable argument lists:\n"
80                "- Wrong parameter passed to va_start()\n"
81                "- Reference passed to va_start()\n"
82                "- Missing va_end()\n"
83                "- Using va_list before it is opened\n"
84                "- Subsequent calls to va_start/va_copy()\n";
85     }
86 };
87 
88 /// @}
89 
90 //---------------------------------------------------------------------------
91 #endif // checkvaargtH
92