From 94c5d92b2b26a037e5ab9add7103ae5ddc6b59a2 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 29 Jan 2010 16:04:27 +0100 Subject: [PATCH] Fixed #1323 (segfault with style in CheckClass::operatorEqRetRefThis()) --- lib/checkclass.cpp | 8 ++++---- test/testclass.cpp | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 04bfdf591..37eee7fbc 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -878,8 +878,8 @@ void CheckClass::operatorEqRetRefThis() if (tok1 && tok1->next() && tok1->next()->str() == "{") { - const Token *last = tok1->next()->link()->tokAt(-2); - for (tok1 = tok1->tokAt(2); tok1 != last; tok1 = tok1->next()) + const Token *last = tok1->next()->link(); + for (tok1 = tok1->tokAt(2); tok1 && tok1 != last; tok1 = tok1->next()) { // check for return of reference to this if (tok1->str() == "return") @@ -930,8 +930,8 @@ void CheckClass::operatorEqRetRefThis() if (tok1 && tok1->next() && tok1->next()->str() == "{") { - const Token *last = tok1->next()->link()->tokAt(-2); - for (tok1 = tok1->tokAt(2); tok1 != last; tok1 = tok1->next()) + const Token *last = tok1->next()->link(); + for (tok1 = tok1->tokAt(2); tok1 && tok1 != last; tok1 = tok1->next()) { // check for return of reference to this if (tok1->str() == "return") diff --git a/test/testclass.cpp b/test/testclass.cpp index d3b0167ba..58551c2d7 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -68,7 +68,8 @@ private: TEST_CASE(noConstructor4); TEST_CASE(operatorEq1); - TEST_CASE(operatorEqRetRefThis); + TEST_CASE(operatorEqRetRefThis1); + TEST_CASE(operatorEqRetRefThis2); // ticket #1323 TEST_CASE(operatorEqToSelf1); // single class TEST_CASE(operatorEqToSelf2); // nested class TEST_CASE(operatorEqToSelf3); // multiple inheritance @@ -175,7 +176,7 @@ private: checkClass.operatorEqRetRefThis(); } - void operatorEqRetRefThis() + void operatorEqRetRefThis1() { checkOpertorEqRetRefThis( "class A\n" @@ -280,6 +281,17 @@ private: ASSERT_EQUALS("[test.cpp:10]: (style) 'operator=' should return reference to self\n", errout.str()); } + void operatorEqRetRefThis2() + { + // ticket # 1323 + checkOpertorEqRetRefThis( + "class szp\n" + "{\n" + " szp &operator =(int *other) {};\n" + "};"); + ASSERT_EQUALS("", errout.str()); + } + // Check that operator Equal checks for assignment to self void checkOpertorEqToSelf(const char code[]) {