Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The `cpp/assignment-does-not-return-this` query now recognizes direct calls that pass `*this` to a non-virtual helper and return its result when the helper directly returns that same reference parameter.
46 changes: 45 additions & 1 deletion cpp/ql/src/jsf/4.10 Classes/AV Rule 82.ql
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,53 @@ predicate assignOperatorWithWrongType(Operator op, string msg) {
)
}

/** Gets the unqualified referent type of an lvalue reference. */
private Type referenceBaseType(Type t) {
result = t.getUnspecifiedType().(LValueReferenceType).getBaseType().getUnspecifiedType()
}

/** Holds if `e` has an explicit cast in its conversion chain. */
private predicate hasExplicitCast(Expr e) {
exists(Cast c | c = e.getUnconverted().getConversion*() and not c.isImplicit())
}

/** Holds if a visible, non-virtual function always returns the reference parameter `p`. */
private predicate returnsReferenceParameter(Function f, Parameter p) {
p.getFunction() = f and
exists(f.getBlock()) and
not f.isVirtual() and
referenceBaseType(f.getType()) = referenceBaseType(p.getType()) and
exists(ReturnStmt s | s.getEnclosingFunction() = f and reachable(s)) and
forall(ReturnStmt s | s.getEnclosingFunction() = f and reachable(s) |
s.getExpr().getUnconverted().(VariableAccess).getTarget() = p and
not hasExplicitCast(s.getExpr())
)
}

/** Holds if an assignment returns a helper call that passes through a direct `*this` argument. */
private predicate returnsThisThroughHelper(Operator op, Expr e) {
exists(FunctionCall call, Parameter p, Expr arg, PointerDereferenceExpr deref, Expr operand |
call = e.getUnconverted() and
returnsReferenceParameter(call.getTarget(), p) and
arg = call.getArgument(p.getIndex()) and
deref = arg.getUnconverted() and
operand = deref.getChild(0) and
operand.getUnconverted() instanceof ThisExpr and
deref.getType().getUnspecifiedType() = referenceBaseType(p.getType()) and
referenceBaseType(op.getType()) = referenceBaseType(p.getType()) and
not hasExplicitCast(e) and
not hasExplicitCast(arg) and
not hasExplicitCast(operand)
)
}

predicate assignOperatorWithWrongResult(Operator op, string msg) {
op.hasName("operator=") and
not returnsDereferenceThis(op) and
exists(ReturnStmt s |
s.getEnclosingFunction() = op and
reachable(s) and
not (dereferenceThis(s.getExpr()) or returnsThisThroughHelper(op, s.getExpr()))
) and
exists(op.getBlock()) and
not op.getType() instanceof VoidType and
not assignOperatorWithWrongType(op, _) and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,25 @@
| AV Rule 82.cpp:24:8:24:16 | operator= | Assignment operator in class Bad2 should have return type Bad2&. Otherwise a copy is created at each call. |
| AV Rule 82.cpp:63:29:63:29 | operator= | Assignment operator in class TemplateReturnAssignment<int> does not return a reference to *this. |
| AV Rule 82.cpp:199:52:199:52 | operator= | Assignment operator in class TemplatedAssignmentBad should have return type TemplatedAssignmentBad&. Otherwise a copy is created at each call. |
| casts.cpp:21:24:21:32 | operator= | Assignment operator in class BadHelperReturnCast does not return a reference to *this. |
| casts.cpp:27:30:27:38 | operator= | Assignment operator in class BadHelperReturnCStyleCast does not return a reference to *this. |
| casts.cpp:33:26:33:34 | operator= | Assignment operator in class BadHelperArgumentCast does not return a reference to *this. |
| casts.cpp:40:25:40:33 | operator= | Assignment operator in class BadHelperPointerCast does not return a reference to *this. |
| casts.cpp:47:29:47:37 | operator= | Assignment operator in class BadHelperConditionalCast does not return a reference to *this. |
| casts.cpp:54:23:54:31 | operator= | Assignment operator in class BadHelperCommaCast does not return a reference to *this. |
| casts.cpp:65:30:65:38 | operator= | Assignment operator in class BadHelperMemberReturnCast does not return a reference to *this. |
| casts.cpp:72:27:72:35 | operator= | Assignment operator in class BadHelperQualifierCast does not return a reference to *this. |
| casts.cpp:84:28:84:36 | operator= | Assignment operator in class GoodHelperArgumentPaths does not return a reference to *this. |
| casts.cpp:87:28:87:36 | operator= | Assignment operator in class GoodHelperArgumentPaths does not return a reference to *this. |
| casts.cpp:90:28:90:36 | operator= | Assignment operator in class GoodHelperArgumentPaths does not return a reference to *this. |
| casts.cpp:93:28:93:36 | operator= | Assignment operator in class GoodHelperArgumentPaths does not return a reference to *this. |
| casts.cpp:96:28:96:36 | operator= | Assignment operator in class GoodHelperArgumentPaths does not return a reference to *this. |
| casts.cpp:102:24:102:32 | operator= | Assignment operator in class BadHelperResultCast does not return a reference to *this. |
| helpers.cpp:93:13:93:21 | operator= | Assignment operator in class BadOther does not return a reference to *this. |
| helpers.cpp:104:16:104:24 | operator= | Assignment operator in class BadBranches does not return a reference to *this. |
| helpers.cpp:114:19:114:27 | operator= | Assignment operator in class BadConditional does not return a reference to *this. |
| helpers.cpp:122:22:122:30 | operator= | Assignment operator in class BadValueParameter does not return a reference to *this. |
| helpers.cpp:130:18:130:26 | operator= | Assignment operator in class BadValueResult should have return type BadValueResult&. Otherwise a copy is created at each call. |
| helpers.cpp:138:19:138:27 | operator= | Assignment operator in class BadMissingBody does not return a reference to *this. |
| helpers.cpp:148:15:148:23 | operator= | Assignment operator in class BadVirtual does not return a reference to *this. |
| helpers.cpp:165:18:165:26 | operator= | Assignment operator in class BadConversion does not return a reference to *this. |
106 changes: 106 additions & 0 deletions cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 82/casts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Explicit conversions can select a different subobject of the same base type.
template<class Base> struct LeftBase : Base {};
template<class Base> struct RightBase : Base {};
template<class Base> struct TwoBases : LeftBase<Base>, RightBase<Base> {};

template<class T>
T& identity(T& destination) { return (destination); }

template<class T>
T& otherBase(T& destination) {
return static_cast<T&>(static_cast<RightBase<T>&>(
static_cast<TwoBases<T>&>(static_cast<LeftBase<T>&>(destination))));
}

template<class T>
T& otherBaseCStyle(T& destination) {
return (T&)(RightBase<T>&)(TwoBases<T>&)(LeftBase<T>&)destination;
}

struct BadHelperReturnCast {
BadHelperReturnCast& operator=(int) { // $ Alert
return otherBase(*this);
}
};

struct BadHelperReturnCStyleCast {
BadHelperReturnCStyleCast& operator=(int) { // $ Alert
return otherBaseCStyle(*this);
}
};

struct BadHelperArgumentCast {
BadHelperArgumentCast& operator=(int) { // $ Alert
return identity(static_cast<BadHelperArgumentCast&>(static_cast<RightBase<BadHelperArgumentCast>&>(
static_cast<TwoBases<BadHelperArgumentCast>&>(static_cast<LeftBase<BadHelperArgumentCast>&>(*this)))));
}
};

struct BadHelperPointerCast {
BadHelperPointerCast& operator=(int) { // $ Alert
return identity(*static_cast<BadHelperPointerCast*>(static_cast<RightBase<BadHelperPointerCast>*>(
static_cast<TwoBases<BadHelperPointerCast>*>(static_cast<LeftBase<BadHelperPointerCast>*>(this)))));
}
};

struct BadHelperConditionalCast {
BadHelperConditionalCast& operator=(bool flag) { // $ Alert
return identity(flag ? *this : static_cast<BadHelperConditionalCast&>(static_cast<RightBase<BadHelperConditionalCast>&>(
static_cast<TwoBases<BadHelperConditionalCast>&>(static_cast<LeftBase<BadHelperConditionalCast>&>(*this)))));
}
};

struct BadHelperCommaCast {
BadHelperCommaCast& operator=(int) { // $ Alert
return identity((0, static_cast<BadHelperCommaCast&>(static_cast<RightBase<BadHelperCommaCast>&>(
static_cast<TwoBases<BadHelperCommaCast>&>(static_cast<LeftBase<BadHelperCommaCast>&>(*this))))));
}
};

struct BadHelperMemberReturnCast {
BadHelperMemberReturnCast* otherPointer() {
return static_cast<BadHelperMemberReturnCast*>(static_cast<RightBase<BadHelperMemberReturnCast>*>(
static_cast<TwoBases<BadHelperMemberReturnCast>*>(static_cast<LeftBase<BadHelperMemberReturnCast>*>(this))));
}
BadHelperMemberReturnCast& operator=(int) { // $ Alert
return identity(*this->otherPointer());
}
};

struct BadHelperQualifierCast {
BadHelperQualifierCast* selfPointer() { return this; }
BadHelperQualifierCast& operator=(int) { // $ Alert
return identity(*static_cast<BadHelperQualifierCast*>(static_cast<RightBase<BadHelperQualifierCast>*>(
static_cast<TwoBases<BadHelperQualifierCast>*>(static_cast<LeftBase<BadHelperQualifierCast>*>(this))))->selfPointer());
}
};

struct GoodHelperArgumentPaths {
GoodHelperArgumentPaths* selfPointer() { return this; }

GoodHelperArgumentPaths& operator=(int) { // GOOD: parentheses are not casts.
return identity(((*this)));
}
GoodHelperArgumentPaths& operator=(bool flag) { // $ Alert - the helper argument must be a direct *this.
return identity(flag ? *this : *this);
}
GoodHelperArgumentPaths& operator=(char) { // $ Alert
return identity((0, *this));
}
GoodHelperArgumentPaths& operator=(long flag) { // $ Alert
return identity(static_cast<bool>(flag) ? *this : *this);
}
GoodHelperArgumentPaths& operator=(unsigned int value) { // $ Alert
return identity((static_cast<void>(value), *this));
}
GoodHelperArgumentPaths& operator=(double) { // $ Alert
return identity(*this->selfPointer());
}
};

struct BadHelperResultCast {
BadHelperResultCast& operator=(int) { // $ Alert
return static_cast<BadHelperResultCast&>(static_cast<RightBase<BadHelperResultCast>&>(
static_cast<TwoBases<BadHelperResultCast>&>(static_cast<LeftBase<BadHelperResultCast>&>(identity(*this)))));
}
};
192 changes: 192 additions & 0 deletions cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 82/helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// Regression cases for github/codeql#7035.
template<class T>
T& assignFirst(T& destination, const T& source) {
destination.value = source.value;
return destination;
}

struct GoodTemplate {
int value;
GoodTemplate& operator=(const GoodTemplate& other) { // GOOD
return assignFirst(*this, other);
}
GoodTemplate& operator=(GoodTemplate&& other) { // GOOD
return assignFirst(*this, other);
}
};

struct GoodFree;
GoodFree& assignFree(GoodFree& destination, const GoodFree& source);
struct GoodFree {
int value;
GoodFree& operator=(const GoodFree& other) { // GOOD
return assignFree(*this, other);
}
};
GoodFree& assignFree(GoodFree& destination, const GoodFree& source) {
destination.value = source.value;
return destination;
}

struct StaticHelper {
template<class T>
static T& assign(const T& source, T& destination) {
destination.value = source.value;
return destination;
}
};
struct GoodSecondArgument {
int value;
GoodSecondArgument& operator=(const GoodSecondArgument& other) { // GOOD
return StaticHelper::assign(other, *this);
}
};

template<class T>
T& branching(T& destination, bool flag) {
if (flag) return destination;
destination.value = 0;
return destination;
}
struct GoodBranches {
int value;
GoodBranches& operator=(int other) { // GOOD
if (value == other) return *this;
return branching(*this, other != 0);
}
};

template<class T>
T& unreachableOther(T& destination, T& other) {
if (1) return destination;
else return other;
}
struct GoodUnreachable {
int value;
GoodUnreachable& operator=(GoodUnreachable& other) { // GOOD
return unreachableOther(*this, other);
}
};

struct GoodTypedef {
typedef GoodTypedef& Reference;
static Reference identity(Reference destination) { return (destination); }
GoodTypedef& operator=(int) { // GOOD
return identity(*this);
}
};

template<class T>
T& maybeThrow(T& destination, bool fail) {
if (fail) throw 1;
return destination;
}
struct GoodThrows {
GoodThrows& operator=(int other) { // GOOD
return maybeThrow(*this, other != 0);
}
};

template<class T>
T& returnOther(T& destination, T& other) { return other; }
struct BadOther {
BadOther& operator=(BadOther& other) { // $ Alert
return returnOther(*this, other);
}
};

template<class T>
T& mixedReturns(T& destination, T& other, bool flag) {
if (flag) return destination;
return other;
}
struct BadBranches {
BadBranches& operator=(BadBranches& other) { // $ Alert
return mixedReturns(*this, other, false);
}
};

template<class T>
T& conditionalReturn(T& destination, T& other, bool flag) {
return flag ? destination : other;
}
struct BadConditional {
BadConditional& operator=(BadConditional& other) { // $ Alert
return conditionalReturn(*this, other, false);
}
};

template<class T>
T& byValue(T destination) { return destination; }
struct BadValueParameter {
BadValueParameter& operator=(int) { // $ Alert
return byValue(*this);
}
};

template<class T>
T byValueResult(T& destination) { return destination; }
struct BadValueResult {
BadValueResult operator=(int) { // $ Alert
return byValueResult(*this);
}
};

struct BadMissingBody;
BadMissingBody& unknownIdentity(BadMissingBody& destination);
struct BadMissingBody {
BadMissingBody& operator=(int) { // $ Alert
return unknownIdentity(*this);
}
};

struct BadVirtual;
struct VirtualHelper {
virtual BadVirtual& select(BadVirtual& destination) { return destination; }
};
struct BadVirtual {
BadVirtual& operator=(VirtualHelper& helper) { // $ Alert
return helper.select(*this);
}
};
struct OtherVirtualHelper : VirtualHelper {
BadVirtual other;
BadVirtual& select(BadVirtual&) override { return other; }
};

struct BadConversion;
struct Proxy {
operator BadConversion&();
};
BadConversion& convertingHelper(Proxy& proxy) { return proxy; }
struct BadConversion {
Proxy proxy;
operator Proxy&() { return proxy; }
BadConversion& operator=(int) { // $ Alert
return convertingHelper(*this);
}
};

void instantiateHelpers() {
GoodTemplate first, second;
first = second;
first = static_cast<GoodTemplate&&>(second);
GoodSecondArgument a, b;
a = b;
GoodBranches branches;
branches = 1;
GoodUnreachable x, y;
x = y;
GoodThrows throwing;
throwing = 0;
BadOther o, p;
o = p;
BadBranches c, d;
c = d;
BadConditional e, f;
e = f;
BadValueParameter value;
value = 0;
BadValueResult result;
result = 0;
}