Skip to content

Commit 5528423

Browse files
committed
C++: Fix special-casing of Qt library
The `Expr.getType` predicate returns a pointer type since that's the type of the `new`-expression as a whole. To find the class type, we use `NewExpr.getAllocatedType`. This commit reduces the number of alerts in a Qt snapshot from 229 to 51, and it removes the two false positives in https://github.com/Subsurface-divelog/subsurface.
1 parent a59a9f6 commit 5528423

File tree

3 files changed

+4
-7
lines changed

3 files changed

+4
-7
lines changed

cpp/ql/src/jsf/4.10 Classes/AV Rule 79.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ predicate automaticallyReleased(Assignment acquire)
267267
{
268268
// sub-types of the Qt type QObject are released by their parent (if they have one)
269269
exists(NewExpr alloc |
270-
alloc.getType() = qtObject() and
270+
alloc.getAllocatedType() = qtObject() and
271271
acquire.getRValue() = alloc and
272272
alloc.getInitializer() = qtParentConstructor().getACallToThisFunction()
273273
)

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
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. |
2017
| SelfRegistering.cpp:25:3:25:24 | ... = ... | Resource side is acquired by class MyOwner but not released anywhere in this class. |
2118
| Variants.cpp:25:3:25:13 | ... = ... | Resource f is acquired by class MyClass4 but not released anywhere in this class. |
2219
| Variants.cpp:65:3:65:17 | ... = ... | Resource a is acquired by class MyClass6 but not released anywhere in this class. |

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ class MyQtUser {
1313
MyQtUser(QObject *parent) {
1414
// This object sets its parent pointer to null and thus must be deleted
1515
// manually.
16-
noParent = new DerivedFromQObject(nullptr); // BAD
16+
noParent = new DerivedFromQObject(nullptr); // BAD [NOT DETECTED]
1717

1818
// This object does not need to be deleted because it will be deleted by
1919
// its parent object when the time is right.
20-
constructorParent = new DerivedFromQObject(parent); // GOOD [FALSE POSITIVE]
20+
constructorParent = new DerivedFromQObject(parent); // GOOD
2121

22-
laterParent = new DerivedFromQObject(nullptr); // GOOD [FALSE POSITIVE]
22+
laterParent = new DerivedFromQObject(nullptr); // GOOD
2323
laterParent->setParent(parent);
2424
}
2525
};

0 commit comments

Comments
 (0)