Merge pull request #11091 from JarLob/assign

Fix AV Rule 76
This commit is contained in:
Mathias Vorreiter Pedersen
2022-11-03 13:06:10 +00:00
committed by GitHub
5 changed files with 107 additions and 3 deletions

View File

@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* Fixed a bug in `cpp/jsf/av-rule-76` that caused the query to miss results when an implicitly-defined copy constructor or copy assignment operator was generated.

View File

@@ -38,9 +38,9 @@ predicate hasNontrivialDestructor(Class c) {
from Class c from Class c
where where
(hasPointerMember(c) or hasNontrivialDestructor(c)) and (hasPointerMember(c) or hasNontrivialDestructor(c)) and
not ( (
c.getAMemberFunction() instanceof CopyConstructor and c.hasImplicitCopyAssignmentOperator() or
c.getAMemberFunction() instanceof CopyAssignmentOperator c.hasImplicitCopyConstructor()
) and ) and
not c instanceof Struct not c instanceof Struct
select c, select c,

View File

@@ -0,0 +1,3 @@
| test.cpp:5:7:5:12 | Class2 | AV Rule 76: A copy constructor and an assignment operator shall be declared for classes that contain pointers to data items or nontrivial destructors. |
| test.cpp:16:7:16:12 | Class3 | AV Rule 76: A copy constructor and an assignment operator shall be declared for classes that contain pointers to data items or nontrivial destructors. |
| test.cpp:33:7:33:12 | Class4 | AV Rule 76: A copy constructor and an assignment operator shall be declared for classes that contain pointers to data items or nontrivial destructors. |

View File

@@ -0,0 +1 @@
jsf/4.10 Classes/AV Rule 76.ql

View File

@@ -0,0 +1,96 @@
class Class1 // good: no pointer members, default assignment operator and copy constructor
{
};
class Class2 // bad: pointer members, default assignment operator and copy constructor
{
private:
int* _a;
public:
Class2(int* a):_a(a)
{
}
};
class Class3 // bad: pointer members, custom assignment operator and default copy constructor
{
private:
int* _a;
public:
Class3(int* a) :_a(a)
{
}
Class3& operator=(const Class3& rhs)
{
this->_a = rhs._a;
return *this;
}
};
class Class4 // bad: pointer members, default assignment operator and custom copy constructor
{
private:
int* _a;
public:
Class4(int* a) :_a(a)
{
}
Class4(const Class4& rhs):_a(rhs._a)
{
}
};
class Class5 // good: pointer members, custom assignment operator and copy constructor
{
private:
int* _a;
public:
Class5(int* a) :_a(a)
{
}
Class5(const Class5& rhs) :_a(rhs._a)
{
}
Class5& operator=(const Class5& rhs)
{
this->_a = rhs._a;
return *this;
}
};
class Class6 // good: pointer members, deleted assignment operator and copy constructor
{
private:
int* _a;
public:
Class6(int* a) :_a(a)
{
}
Class6& operator=(const Class6& rhs) = delete;
Class6(const Class6& rhs) = delete;
};
class Class7 // good: pointer members, disallowed assignment operator and copy constructor
{
private:
int* _a;
public:
Class7(int* a) :_a(a)
{
}
private:
Class7& operator=(const Class7& rhs); // no implementation to get linker error!
Class7(const Class7& rhs); // no implementation to get linker error!
};