Ticket #6347: Fix use after delete when simplifying template instantiations.

This commit is contained in:
Simon Martin 2015-03-14 18:46:17 +01:00
parent 42c4aa1c49
commit 4c916641cc
2 changed files with 13 additions and 3 deletions

View File

@ -1052,8 +1052,8 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens)
ret = true; ret = true;
} else if (Token::Match(tok->previous(), "[=([,] 0 [+|]") || } else if (Token::Match(tok->previous(), "[=([,] 0 [+|]") ||
Token::Match(tok->previous(), "return|case 0 [+|]")) { Token::Match(tok->previous(), "return|case 0 [+|]")) {
tok->deleteNext(); tok = tok->previous();
tok->deleteThis(); tok->deleteNext(2);
ret = true; ret = true;
} else if (Token::Match(tok->previous(), "[=[(,] 0 * %name% ,|]|)|;|=|%cop%") || } else if (Token::Match(tok->previous(), "[=[(,] 0 * %name% ,|]|)|;|=|%cop%") ||
Token::Match(tok->previous(), "[=[(,] 0 * %num% ,|]|)|;|%op%") || Token::Match(tok->previous(), "[=[(,] 0 * %num% ,|]|)|;|%op%") ||
@ -1232,7 +1232,6 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
break; break;
} }
} }
Token * const tok2 = *iter2; Token * const tok2 = *iter2;
if (tok2->str() != name) if (tok2->str() != name)
continue; continue;

View File

@ -82,6 +82,7 @@ private:
TEST_CASE(template49); // #6237 - template instantiation TEST_CASE(template49); // #6237 - template instantiation
TEST_CASE(template50); // #4272 - simple partial specialization TEST_CASE(template50); // #4272 - simple partial specialization
TEST_CASE(template51); // #6172 - crash upon valid code TEST_CASE(template51); // #6172 - crash upon valid code
TEST_CASE(template52); // #6437 - crash upon valid code
TEST_CASE(template_unhandled); TEST_CASE(template_unhandled);
TEST_CASE(template_default_parameter); TEST_CASE(template_default_parameter);
TEST_CASE(template_default_type); TEST_CASE(template_default_type);
@ -927,6 +928,16 @@ private:
"}"); "}");
} }
void template52() { // #6437
tok("template <int value> int sum() { "
" return value + sum<value/2>(); "
"} "
"template<int x, int y> int calculate_value() { "
" return sum<x - y>(); "
"} "
"int value = calculate_value<1,1>();");
}
void template_default_parameter() { void template_default_parameter() {
{ {
const char code[] = "template <class T, int n=3>\n" const char code[] = "template <class T, int n=3>\n"