Skip to content

Commit 53e2cab

Browse files
committed
Properly support "break" in CheckVaarg::va_list_usage() (#7533)
Ran AStyle
1 parent 321d2ae commit 53e2cab

4 files changed

Lines changed: 61 additions & 9 deletions

File tree

lib/checksizeof.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace {
3232
}
3333

3434
// CWE IDs used:
35-
static const struct CWE CWE398(398U); // Indicator of Poor Code Quality
35+
static const struct CWE CWE398(398U); // Indicator of Poor Code Quality
3636

3737
//---------------------------------------------------------------------------
3838
//---------------------------------------------------------------------------

lib/checkstl.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ namespace {
2828
}
2929

3030
// CWE IDs used:
31-
static const struct CWE CWE398(398U); // Indicator of Poor Code Quality
32-
static const struct CWE CWE597(597U); // Use of Wrong Operator in String Comparison
33-
static const struct CWE CWE664(664U); // Improper Control of a Resource Through its Lifetime
34-
static const struct CWE CWE704(704U); // Incorrect Type Conversion or Cast
35-
static const struct CWE CWE788(788U); // Access of Memory Location After End of Buffer
36-
static const struct CWE CWE834(834U); // Excessive Iteration
31+
static const struct CWE CWE398(398U); // Indicator of Poor Code Quality
32+
static const struct CWE CWE597(597U); // Use of Wrong Operator in String Comparison
33+
static const struct CWE CWE664(664U); // Improper Control of a Resource Through its Lifetime
34+
static const struct CWE CWE704(704U); // Incorrect Type Conversion or Cast
35+
static const struct CWE CWE788(788U); // Access of Memory Location After End of Buffer
36+
static const struct CWE CWE834(834U); // Excessive Iteration
3737

3838
// Error message for bad iterator usage..
3939
void CheckStl::invalidIteratorError(const Token *tok, const std::string &iteratorName)

lib/checkvaarg.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,14 @@ void CheckVaarg::va_list_usage()
122122
}
123123
open = nopen;
124124
tok = tok->linkAt(1);
125-
} else if (Token::Match(tok, "throw|return|break"))
125+
} else if (Token::Match(tok, "throw|return"))
126126
exitOnEndOfStatement = true;
127-
else if (_tokenizer->isCPP() && tok->str() == "try") {
127+
else if (tok->str() == "break") {
128+
const Scope* scope = tok->scope();
129+
while (scope->nestedIn && scope->type != Scope::eFor && scope->type != Scope::eWhile && scope->type != Scope::eDo && scope->type != Scope::eSwitch)
130+
scope = scope->nestedIn;
131+
tok = scope->classEnd;
132+
} else if (_tokenizer->isCPP() && tok->str() == "try") {
128133
open = false;
129134
break;
130135
} else if (!open && tok->varId() == var->declarationId())

test/testvaarg.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,53 @@ class TestVaarg : public TestFixture {
216216
" }\n"
217217
"}");
218218
ASSERT_EQUALS("", errout.str());
219+
220+
// #7533
221+
check("void action_push(int type, ...) {\n"
222+
" va_list args;\n"
223+
" va_start(args, type);\n"
224+
" switch (push_mode) {\n"
225+
" case UNDO:\n"
226+
" list_add(&act->node, &to_redo);\n"
227+
" break;\n"
228+
" case REDO:\n"
229+
" list_add(&act->node, &to_undo);\n"
230+
" break;\n"
231+
" }\n"
232+
" va_end(args);\n"
233+
"}");
234+
ASSERT_EQUALS("", errout.str());
235+
236+
check("void action_push(int type, ...) {\n"
237+
" va_list args;\n"
238+
" va_start(args, type);\n"
239+
" switch (push_mode) {\n"
240+
" case UNDO:\n"
241+
" list_add(&act->node, &to_redo);\n"
242+
" va_end(args);\n"
243+
" break;\n"
244+
" case REDO:\n"
245+
" list_add(&act->node, &to_undo);\n"
246+
" va_end(args);\n"
247+
" break;\n"
248+
" }\n"
249+
"}");
250+
ASSERT_EQUALS("", errout.str());
251+
252+
check("void action_push(int type, ...) {\n"
253+
" va_list args;\n"
254+
" va_start(args, type);\n"
255+
" switch (push_mode) {\n"
256+
" case UNDO:\n"
257+
" list_add(&act->node, &to_redo);\n"
258+
" break;\n"
259+
" case REDO:\n"
260+
" list_add(&act->node, &to_undo);\n"
261+
" va_end(args);\n"
262+
" break;\n"
263+
" }\n"
264+
"}");
265+
ASSERT_EQUALS("[test.cpp:13]: (error) va_list 'args' was opened but not closed by va_end().\n", errout.str());
219266
}
220267

221268
void va_start_subsequentCalls() {

0 commit comments

Comments
 (0)