MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / getMinFormatStringOutputLength

Function getMinFormatStringOutputLength

lib/checkbufferoverrun.cpp:80–189  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

78}
79
80static int getMinFormatStringOutputLength(const std::vector<const Token*> &parameters, nonneg int formatStringArgNr, const Settings& settings)
81{
82 if (formatStringArgNr <= 0 || formatStringArgNr > parameters.size())
83 return 0;
84 if (parameters[formatStringArgNr - 1]->tokType() != Token::eString)
85 return 0;
86 const std::string &formatString = parameters[formatStringArgNr - 1]->str();
87 bool percentCharFound = false;
88 int outputStringSize = 0;
89 bool handleNextParameter = false;
90 std::string digits_string;
91 bool i_d_x_f_found = false;
92 int parameterLength = 0;
93 nonneg int inputArgNr = formatStringArgNr;
94 for (std::size_t i = 1; i + 1 < formatString.length(); ++i) {
95 if (formatString[i] == '\\') {
96 if (i < formatString.length() - 1 && formatString[i + 1] == '0')
97 break;
98
99 ++outputStringSize;
100 ++i;
101 continue;
102 }
103
104 if (percentCharFound) {
105 switch (formatString[i]) {
106 case 'f':
107 case 'x':
108 case 'X':
109 case 'i':
110 i_d_x_f_found = true;
111 handleNextParameter = true;
112 parameterLength = 1; // TODO
113 break;
114 case 'c':
115 case 'e':
116 case 'E':
117 case 'g':
118 case 'o':
119 case 'u':
120 case 'p':
121 case 'n':
122 handleNextParameter = true;
123 parameterLength = 1; // TODO
124 break;
125 case 'd':
126 i_d_x_f_found = true;
127 parameterLength = 1;
128 if (inputArgNr < parameters.size() && parameters[inputArgNr]->hasKnownIntValue())
129 parameterLength = MathLib::toString(parameters[inputArgNr]->getKnownIntValue()).length();
130
131 handleNextParameter = true;
132 break;
133 case 's':
134 parameterLength = 0;
135 if (inputArgNr < parameters.size())
136 parameterLength = ValueFlow::valueFlowGetStrLength(parameters[inputArgNr], settings);
137

Callers 1

checkBufferSizeFunction · 0.85

Calls 8

valueFlowGetStrLengthFunction · 0.85
getKnownIntValueMethod · 0.80
toStringFunction · 0.70
sizeMethod · 0.45
strMethod · 0.45
hasKnownIntValueMethod · 0.45
findMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected