Skip to content

Commit d5908f0

Browse files
IOBYTEdanmar
authored andcommitted
Fixed danmar#6226 (false negative: printf format check for user defined array type)
1 parent e2f72b4 commit d5908f0

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/checkio.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,20 @@ bool CheckIO::ArgumentInfo::isStdVectorOrString()
15491549
return true;
15501550
}
15511551
}
1552+
} else if (variableInfo->type()) {
1553+
const Scope * classScope = variableInfo->type()->classScope;
1554+
if (classScope) {
1555+
std::list<Function>::const_iterator functions;
1556+
for (functions = classScope->functionList.begin();
1557+
functions != classScope->functionList.end(); ++functions) {
1558+
if (functions->name() == "operator[]") {
1559+
if (Token::Match(functions->retDef, "%type% &")) {
1560+
typeToken = functions->retDef;
1561+
return true;
1562+
}
1563+
}
1564+
}
1565+
}
15521566
}
15531567

15541568
return false;

test/testio.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,6 +3100,19 @@ class TestIO : public TestFixture {
31003100
ASSERT_EQUALS("[test.cpp:4]: (warning) %s in format string (no. 1) requires 'char *' but the argument type is 'std::string'.\n"
31013101
"[test.cpp:4]: (warning) %s in format string (no. 2) requires 'char *' but the argument type is 'int'.\n", errout.str());
31023102

3103+
check("template <class T, size_t S>\n"
3104+
"struct Array {\n"
3105+
" T data[S];\n"
3106+
" T & operator [] (size_t i) { return data[i]; }\n"
3107+
"};\n"
3108+
"void foo() {\n"
3109+
" Array<int, 10> array1;\n"
3110+
" Array<float, 10> array2;\n"
3111+
" printf(\"%u %u\", array1[0], array2[0]);\n"
3112+
"}\n");
3113+
ASSERT_EQUALS("[test.cpp:9]: (warning) %u in format string (no. 1) requires 'unsigned int' but the argument type is 'int'.\n"
3114+
"[test.cpp:9]: (warning) %u in format string (no. 2) requires 'unsigned int' but the argument type is 'float'.\n", errout.str());
3115+
31033116
}
31043117

31053118
void testPosixPrintfScanfParameterPosition() { // #4900 - No support for parameters in format strings

0 commit comments

Comments
 (0)