Skip to content

Commit f1fa374

Browse files
authored
fix #13881: Stack overflow in findTokensSkipDeadCodeImpl() (danmar#7545)
This ``` using A::a; int x = B<T>::a; ``` would previously be simplified to ``` int x = B<T>A::a; ``` in `simplifyUsing`.
1 parent 6f337e8 commit f1fa374

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/tokenize.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,6 +2650,10 @@ namespace {
26502650
return false;
26512651
}
26522652

2653+
if (Token::simpleMatch(tok1->tokAt(-2), "> ::")) {
2654+
return false;
2655+
}
2656+
26532657
if (Token::Match(tok1, "%name% (") && TokenList::isFunctionHead(tok1->next(), "{;:")) {
26542658
if (Token::Match(tok1->previous(), "%name%") && !tok1->previous()->isControlFlowKeyword())
26552659
return false;

test/testnullpointer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class TestNullPointer : public TestFixture {
140140
TEST_CASE(nullpointer101); // #11382
141141
TEST_CASE(nullpointer102);
142142
TEST_CASE(nullpointer103);
143+
TEST_CASE(nullpointer104); // #13881
143144
TEST_CASE(nullpointer_addressOf); // address of
144145
TEST_CASE(nullpointerSwitch); // #2626
145146
TEST_CASE(nullpointer_cast); // #4692
@@ -2919,6 +2920,15 @@ class TestNullPointer : public TestFixture {
29192920
TODO_ASSERT_EQUALS("", "[test.cpp:3:10]: (warning) Possible null pointer dereference: p [nullPointer]\n", errout_str());
29202921
}
29212922

2923+
void nullpointer104() // #13881
2924+
{
2925+
check("using std::max;\n"
2926+
"void f(int i) {\n"
2927+
" const size_t maxlen = i == 1 ? 8 : (std::numeric_limits<std::size_t>::max());\n"
2928+
"}\n");
2929+
ASSERT_EQUALS("", errout_str());
2930+
}
2931+
29222932
void nullpointer_addressOf() { // address of
29232933
check("void f() {\n"
29242934
" struct X *x = 0;\n"

test/testsimplifyusing.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class TestSimplifyUsing : public TestFixture {
7171
TEST_CASE(simplifyUsing33);
7272
TEST_CASE(simplifyUsing34);
7373
TEST_CASE(simplifyUsing35);
74+
TEST_CASE(simplifyUsing36);
7475

7576
TEST_CASE(simplifyUsing8970);
7677
TEST_CASE(simplifyUsing8971);
@@ -873,6 +874,14 @@ class TestSimplifyUsing : public TestFixture {
873874
ASSERT_EQUALS("", errout_str());
874875
}
875876

877+
void simplifyUsing36() {
878+
const char code[] = "using A::a;\n"
879+
"int c = B<int>::a;\n";
880+
const char expected[] = "int c ; c = B < int > :: a ;";
881+
ASSERT_EQUALS(expected, tok(code));
882+
ASSERT_EQUALS("", errout_str());
883+
}
884+
876885
void simplifyUsing8970() {
877886
const char code[] = "using V = std::vector<int>;\n"
878887
"struct A {\n"

0 commit comments

Comments
 (0)