Ticket #6588: Properly detect whether the condition in a ternary operator is constant on C input.

This commit is contained in:
Simon Martin 2015-05-31 22:40:13 +02:00
parent bc2c172a1f
commit 519bd7007a
3 changed files with 24 additions and 6 deletions

View File

@ -4858,9 +4858,9 @@ bool Tokenizer::simplifyConstTernaryOp()
const int offset = (tok->previous()->str() == ")") ? 2 : 1;
bool inTemplateParameter = false;
if (!isC() && tok->strAt(-2*offset) == "<") {
if (!TemplateSimplifier::templateParameters(tok->tokAt(-2*offset)))
continue;
if (tok->strAt(-2*offset) == "<") {
if (isC() || !TemplateSimplifier::templateParameters(tok->tokAt(-2*offset)))
continue; // '<' is less than; the condition is not a constant
inTemplateParameter = true;
}

View File

@ -136,7 +136,17 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
// ticket #6588
// ticket #6588 (c mode)
check("struct MpegEncContext { int *q_intra_matrix, *q_chroma_intra_matrix; };\n"
"void dnxhd_10bit_dct_quantize(MpegEncContext *ctx, int n, int qscale) {\n"
" const int *qmat = n < 4;\n" /* KO */
" const int *rmat = n < 4 ? " /* OK */
" ctx->q_intra_matrix :"
" ctx->q_chroma_intra_matrix;\n"
"}", /*experimental=*/false, "test.c");
ASSERT_EQUALS("[test.c:3]: (error) Boolean value assigned to pointer.\n", errout.str());
// ticket #6588 (c++ mode)
check("struct MpegEncContext { int *q_intra_matrix, *q_chroma_intra_matrix; };\n"
"void dnxhd_10bit_dct_quantize(MpegEncContext *ctx, int n, int qscale) {\n"
" const int *qmat = n < 4;\n" /* KO */

View File

@ -555,7 +555,7 @@ private:
}
void invalidFunctionUsage(const char code[]) {
void invalidFunctionUsage(const char code[], bool isCPP = true) {
// Clear the error buffer..
errout.str("");
@ -573,7 +573,7 @@ private:
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.tokenize(istr, isCPP ? "test.cpp" : "test.c");
// Check for redundant code..
CheckOther checkOther(&tokenizer, &settings, this);
@ -587,6 +587,14 @@ private:
invalidFunctionUsage("int f() { memset(a,b,sizeof(a)!=0); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid memset() argument nr 3. A non-boolean value is required.\n", errout.str());
// Ticket #6588 (c mode)
invalidFunctionUsage("void record(char* buf, int n) {\n"
" memset(buf, 0, n < 255);\n" /* KO */
" memset(buf, 0, n < 255 ? n : 255);\n" /* OK */
"}", /*isCPP=*/false);
ASSERT_EQUALS("[test.c:2]: (error) Invalid memset() argument nr 3. A non-boolean value is required.\n", errout.str());
// Ticket #6588 (c++ mode)
invalidFunctionUsage("void record(char* buf, int n) {\n"
" memset(buf, 0, n < 255);\n" /* KO */
" memset(buf, 0, n < 255 ? n : 255);\n" /* OK */