Skip to content

Commit 6d6bb31

Browse files
IOBYTEdanmar
authored andcommitted
fix crash in daca gcc-avr from intentional bad instantiation test (cppcheck-opensource#1994)
* fix crash in daca gcc-avr from intentional bad instantiation test * fix cppcheck warning
1 parent c2ccfd5 commit 6d6bb31

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

lib/errorlogger.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ InternalError::InternalError(const Token *tok, const std::string &errorMsg, Type
5252
case LIMIT:
5353
id = "cppcheckLimit";
5454
break;
55+
case INSTANTIATION:
56+
id = "instantiationError";
57+
break;
5558
}
5659
}
5760

lib/errorlogger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace tinyxml2 {
5454

5555
/** @brief Simple container to be thrown when internal error is detected. */
5656
struct InternalError {
57-
enum Type {AST, SYNTAX, UNKNOWN_MACRO, INTERNAL, LIMIT};
57+
enum Type {AST, SYNTAX, UNKNOWN_MACRO, INTERNAL, LIMIT, INSTANTIATION};
5858
InternalError(const Token *tok, const std::string &errorMsg, Type type = INTERNAL);
5959
const Token *token;
6060
std::string errorMessage;

lib/templatesimplifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2180,7 +2180,7 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
21802180

21812181
// Don't simplify "%num% / 0"
21822182
if (Token::Match(op, "[/%] 0"))
2183-
break;
2183+
throw InternalError(op, "Instantiation error: Divide by zero in template instantiation.", InternalError::INSTANTIATION);
21842184

21852185
// Integer operations
21862186
if (Token::Match(op, ">>|<<|&|^|%or%")) {

test/testsimplifytemplate.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ class TestSimplifyTemplate : public TestFixture {
162162
TEST_CASE(template122); // #9147
163163
TEST_CASE(template123); // #9183
164164
TEST_CASE(template124); // #9197
165+
TEST_CASE(template125);
165166
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
166167
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
167168
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
@@ -2923,6 +2924,17 @@ class TestSimplifyTemplate : public TestFixture {
29232924
ASSERT_EQUALS(exp, tok(code));
29242925
}
29252926

2927+
void template125() {
2928+
ASSERT_THROW(tok("template<int M, int N>\n"
2929+
"class GCD {\n"
2930+
"public:\n"
2931+
" enum { val = (N == 0) ? M : GCD<N, M % N>::val };\n"
2932+
"};\n"
2933+
"int main() {\n"
2934+
" GCD< 1, 0 >::val;\n"
2935+
"}"), InternalError);
2936+
}
2937+
29262938
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
29272939
const char code[] = "template <typename T> struct C {};\n"
29282940
"template <typename T> struct S {a};\n"

0 commit comments

Comments
 (0)