Coverage Report

Created: 2025-02-17 06:38

/src/cppcheck/lib/checkfunctions.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- C++ -*-
2
 * Cppcheck - A tool for static C/C++ code analysis
3
 * Copyright (C) 2007-2024 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 checkfunctionsH
22
#define checkfunctionsH
23
//---------------------------------------------------------------------------
24
25
#include "check.h"
26
#include "config.h"
27
28
#include <string>
29
30
class Token;
31
class ErrorLogger;
32
class Tokenizer;
33
class Settings;
34
35
namespace ValueFlow {
36
    class Value;
37
}  // namespace ValueFlow
38
39
40
/// @addtogroup Checks
41
/// @{
42
43
/**
44
 * @brief Check for bad function usage
45
 */
46
47
class CPPCHECKLIB CheckFunctions : public Check {
48
public:
49
    /** This constructor is used when registering the CheckFunctions */
50
2
    CheckFunctions() : Check(myName()) {}
51
52
private:
53
    /** This constructor is used when running checks. */
54
    CheckFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
55
764
        : Check(myName(), tokenizer, settings, errorLogger) {}
56
57
    /** @brief Run checks against the normal token list */
58
    void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override;
59
60
    /** Check for functions that should not be used */
61
    void checkProhibitedFunctions();
62
63
    /**
64
     * @brief Invalid function usage (invalid input value / overlapping data)
65
     *
66
     * %Check that given function parameters are valid according to the standard
67
     * - wrong radix given for strtol/strtoul
68
     * - overlapping data when using sprintf/snprintf
69
     * - wrong input value according to library
70
     */
71
    void invalidFunctionUsage();
72
73
    /** @brief %Check for ignored return values. */
74
    void checkIgnoredReturnValue();
75
76
    /** @brief %Check for parameters given to math function that do not make sense*/
77
    void checkMathFunctions();
78
79
    /** @brief %Check for filling zero bytes with memset() */
80
    void memsetZeroBytes();
81
82
    /** @brief %Check for invalid 2nd parameter of memset() */
83
    void memsetInvalid2ndParam();
84
85
    /** @brief %Check for copy elision by RVO|NRVO */
86
    void returnLocalStdMove();
87
88
    void useStandardLibrary();
89
90
    /** @brief --check-library: warn for unconfigured function calls */
91
    void checkLibraryMatchFunctions();
92
93
    /** @brief %Check for missing "return" */
94
    void checkMissingReturn();
95
96
    void invalidFunctionArgError(const Token *tok, const std::string &functionName, int argnr, const ValueFlow::Value *invalidValue, const std::string &validstr);
97
    void invalidFunctionArgBoolError(const Token *tok, const std::string &functionName, int argnr);
98
    void invalidFunctionArgStrError(const Token *tok, const std::string &functionName, nonneg int argnr);
99
    void ignoredReturnValueError(const Token* tok, const std::string& function);
100
    void ignoredReturnErrorCode(const Token* tok, const std::string& function);
101
    void mathfunctionCallWarning(const Token *tok, nonneg int numParam = 1);
102
    void mathfunctionCallWarning(const Token *tok, const std::string& oldexp, const std::string& newexp);
103
    void memsetZeroBytesError(const Token *tok);
104
    void memsetFloatError(const Token *tok, const std::string &var_value);
105
    void memsetValueOutOfRangeError(const Token *tok, const std::string &value);
106
    void missingReturnError(const Token *tok);
107
    void copyElisionError(const Token *tok);
108
    void useStandardLibraryError(const Token *tok, const std::string& expected);
109
110
    void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override;
111
112
766
    static std::string myName() {
113
766
        return "Check function usage";
114
766
    }
115
116
0
    std::string classInfo() const override {
117
0
        return "Check function usage:\n"
118
0
               "- missing 'return' in non-void function\n"
119
0
               "- return value of certain functions not used\n"
120
0
               "- invalid input values for functions\n"
121
0
               "- Warn if a function is called whose usage is discouraged\n"
122
0
               "- memset() third argument is zero\n"
123
0
               "- memset() with a value out of range as the 2nd parameter\n"
124
0
               "- memset() with a float as the 2nd parameter\n"
125
0
               "- copy elision optimization for returning value affected by std::move\n"
126
0
               "- use memcpy()/memset() instead of for loop\n";
127
0
    }
128
};
129
/// @}
130
//---------------------------------------------------------------------------
131
#endif // checkfunctionsH