Files
codeql/cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 79/QObject.cpp
Jonas Jensen 552842346c 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.
2019-03-21 13:37:18 +01:00

26 lines
743 B
C++

struct QObject {
QObject(QObject *parent);
void setParent(QObject *parent);
};
struct DerivedFromQObject : public QObject {
DerivedFromQObject(QObject *parent);
};
class MyQtUser {
DerivedFromQObject *noParent, *constructorParent, *laterParent;
MyQtUser(QObject *parent) {
// This object sets its parent pointer to null and thus must be deleted
// manually.
noParent = new DerivedFromQObject(nullptr); // BAD [NOT DETECTED]
// This object does not need to be deleted because it will be deleted by
// its parent object when the time is right.
constructorParent = new DerivedFromQObject(parent); // GOOD
laterParent = new DerivedFromQObject(nullptr); // GOOD
laterParent->setParent(parent);
}
};