From 53e2cabdbbbfbbbf9013b687105b03c90b678226 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Tue, 7 Jun 2016 19:28:32 +0200 Subject: [PATCH] Properly support "break" in CheckVaarg::va_list_usage() (#7533) Ran AStyle --- lib/checksizeof.cpp | 2 +- lib/checkstl.cpp | 12 ++++++------ lib/checkvaarg.cpp | 9 +++++++-- test/testvaarg.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/lib/checksizeof.cpp b/lib/checksizeof.cpp index 3516fc3d7..3b0e938d1 100644 --- a/lib/checksizeof.cpp +++ b/lib/checksizeof.cpp @@ -32,7 +32,7 @@ namespace { } // CWE IDs used: -static const struct CWE CWE398(398U); // Indicator of Poor Code Quality +static const struct CWE CWE398(398U); // Indicator of Poor Code Quality //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index b561eb2a9..1b18e0150 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -28,12 +28,12 @@ namespace { } // CWE IDs used: -static const struct CWE CWE398(398U); // Indicator of Poor Code Quality -static const struct CWE CWE597(597U); // Use of Wrong Operator in String Comparison -static const struct CWE CWE664(664U); // Improper Control of a Resource Through its Lifetime -static const struct CWE CWE704(704U); // Incorrect Type Conversion or Cast -static const struct CWE CWE788(788U); // Access of Memory Location After End of Buffer -static const struct CWE CWE834(834U); // Excessive Iteration +static const struct CWE CWE398(398U); // Indicator of Poor Code Quality +static const struct CWE CWE597(597U); // Use of Wrong Operator in String Comparison +static const struct CWE CWE664(664U); // Improper Control of a Resource Through its Lifetime +static const struct CWE CWE704(704U); // Incorrect Type Conversion or Cast +static const struct CWE CWE788(788U); // Access of Memory Location After End of Buffer +static const struct CWE CWE834(834U); // Excessive Iteration // Error message for bad iterator usage.. void CheckStl::invalidIteratorError(const Token *tok, const std::string &iteratorName) diff --git a/lib/checkvaarg.cpp b/lib/checkvaarg.cpp index c8dd172e0..6f82643f5 100644 --- a/lib/checkvaarg.cpp +++ b/lib/checkvaarg.cpp @@ -122,9 +122,14 @@ void CheckVaarg::va_list_usage() } open = nopen; tok = tok->linkAt(1); - } else if (Token::Match(tok, "throw|return|break")) + } else if (Token::Match(tok, "throw|return")) exitOnEndOfStatement = true; - else if (_tokenizer->isCPP() && tok->str() == "try") { + else if (tok->str() == "break") { + const Scope* scope = tok->scope(); + while (scope->nestedIn && scope->type != Scope::eFor && scope->type != Scope::eWhile && scope->type != Scope::eDo && scope->type != Scope::eSwitch) + scope = scope->nestedIn; + tok = scope->classEnd; + } else if (_tokenizer->isCPP() && tok->str() == "try") { open = false; break; } else if (!open && tok->varId() == var->declarationId()) diff --git a/test/testvaarg.cpp b/test/testvaarg.cpp index eed7791fb..1ca905333 100644 --- a/test/testvaarg.cpp +++ b/test/testvaarg.cpp @@ -216,6 +216,53 @@ private: " }\n" "}"); ASSERT_EQUALS("", errout.str()); + + // #7533 + check("void action_push(int type, ...) {\n" + " va_list args;\n" + " va_start(args, type);\n" + " switch (push_mode) {\n" + " case UNDO:\n" + " list_add(&act->node, &to_redo);\n" + " break;\n" + " case REDO:\n" + " list_add(&act->node, &to_undo);\n" + " break;\n" + " }\n" + " va_end(args);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void action_push(int type, ...) {\n" + " va_list args;\n" + " va_start(args, type);\n" + " switch (push_mode) {\n" + " case UNDO:\n" + " list_add(&act->node, &to_redo);\n" + " va_end(args);\n" + " break;\n" + " case REDO:\n" + " list_add(&act->node, &to_undo);\n" + " va_end(args);\n" + " break;\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void action_push(int type, ...) {\n" + " va_list args;\n" + " va_start(args, type);\n" + " switch (push_mode) {\n" + " case UNDO:\n" + " list_add(&act->node, &to_redo);\n" + " break;\n" + " case REDO:\n" + " list_add(&act->node, &to_undo);\n" + " va_end(args);\n" + " break;\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:13]: (error) va_list 'args' was opened but not closed by va_end().\n", errout.str()); } void va_start_subsequentCalls() {