Skip to content

Commit a59a9f6

Browse files
committed
C++: Add test cases for Qt's QObject
The Qt library requires client code to call `new` but not `delete`.
1 parent 5a56740 commit a59a9f6

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 79/AV Rule 79.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
| ListDelete.cpp:21:3:21:21 | ... = ... | Resource first is acquired by class MyThingColection but not released anywhere in this class. |
1515
| NoDestructor.cpp:23:3:23:20 | ... = ... | Resource n is acquired by class MyClass5 but not released anywhere in this class. |
1616
| PlacementNew.cpp:36:3:36:36 | ... = ... | Resource p1 is acquired by class MyTestForPlacementNew but not released anywhere in this class. |
17+
| QObject.cpp:16:5:16:46 | ... = ... | Resource noParent is acquired by class MyQtUser but not released anywhere in this class. |
18+
| QObject.cpp:20:5:20:54 | ... = ... | Resource constructorParent is acquired by class MyQtUser but not released anywhere in this class. |
19+
| QObject.cpp:22:5:22:49 | ... = ... | Resource laterParent is acquired by class MyQtUser but not released anywhere in this class. |
1720
| SelfRegistering.cpp:25:3:25:24 | ... = ... | Resource side is acquired by class MyOwner but not released anywhere in this class. |
1821
| Variants.cpp:25:3:25:13 | ... = ... | Resource f is acquired by class MyClass4 but not released anywhere in this class. |
1922
| Variants.cpp:65:3:65:17 | ... = ... | Resource a is acquired by class MyClass6 but not released anywhere in this class. |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
struct QObject {
2+
QObject(QObject *parent);
3+
void setParent(QObject *parent);
4+
};
5+
6+
struct DerivedFromQObject : public QObject {
7+
DerivedFromQObject(QObject *parent);
8+
};
9+
10+
class MyQtUser {
11+
DerivedFromQObject *noParent, *constructorParent, *laterParent;
12+
13+
MyQtUser(QObject *parent) {
14+
// This object sets its parent pointer to null and thus must be deleted
15+
// manually.
16+
noParent = new DerivedFromQObject(nullptr); // BAD
17+
18+
// This object does not need to be deleted because it will be deleted by
19+
// its parent object when the time is right.
20+
constructorParent = new DerivedFromQObject(parent); // GOOD [FALSE POSITIVE]
21+
22+
laterParent = new DerivedFromQObject(nullptr); // GOOD [FALSE POSITIVE]
23+
laterParent->setParent(parent);
24+
}
25+
};

0 commit comments

Comments
 (0)