diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 8ec538f45..a701f1150 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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; } diff --git a/test/testbool.cpp b/test/testbool.cpp index 0d09f6597..c854b6971 100644 --- a/test/testbool.cpp +++ b/test/testbool.cpp @@ -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 */ diff --git a/test/testother.cpp b/test/testother.cpp index a8507cbc1..9d06dc254 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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 */