Skip to content

[WebKit checkers] Treat passing of a member variable which is capable of CheckedPtr as safe. #142485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
20 changes: 20 additions & 0 deletions clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,26 @@ bool isConstOwnerPtrMemberExpr(const clang::Expr *E) {
return isOwnerPtrType(T) && T.isConstQualified();
}

bool isExprToGetCheckedPtrCapableMember(const clang::Expr *E) {
auto *ME = dyn_cast<MemberExpr>(E);
if (!ME)
return false;
auto *Base = ME->getBase();
if (!Base)
return false;
if (!isa<CXXThisExpr>(Base->IgnoreParenCasts()))
return false;
auto *D = ME->getMemberDecl();
if (!D)
return false;
auto T = D->getType();
auto *CXXRD = T->getAsCXXRecordDecl();
if (!CXXRD)
return false;
auto result = isCheckedPtrCapable(CXXRD);
return result && *result;
}

class EnsureFunctionVisitor
: public ConstStmtVisitor<EnsureFunctionVisitor, bool> {
public:
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ bool isASafeCallArg(const clang::Expr *E);
/// \returns true if E is a MemberExpr accessing a const smart pointer type.
bool isConstOwnerPtrMemberExpr(const clang::Expr *E);

/// \returns true if E is a MemberExpr accessing a member variable which
/// supports CheckedPtr.
bool isExprToGetCheckedPtrCapableMember(const clang::Expr *E);

/// \returns true if E is a CXXMemberCallExpr which returns a const smart
/// pointer type.
class EnsureFunctionAnalysis {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ class UncheckedCallArgsChecker final : public RawPtrRefCallArgsChecker {
return isRefOrCheckedPtrType(type);
}

bool isSafeExpr(const Expr *E) const final {
return isExprToGetCheckedPtrCapableMember(E);
}

const char *ptrKind() const final { return "unchecked"; }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ class UncheckedLocalVarsChecker final : public RawPtrRefLocalVarsChecker {
bool isSafePtrType(const QualType type) const final {
return isRefOrCheckedPtrType(type);
}
bool isSafeExpr(const Expr *E) const final {
return isExprToGetCheckedPtrCapableMember(E);
}
const char *ptrKind() const final { return "unchecked"; }
};

Expand Down
20 changes: 20 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/call-args-checked.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ static void baz() {

} // namespace call_args_checked

namespace call_args_member {

void consume(CheckedObj&);

struct WrapperObj {
CheckedObj checked;
CheckedObj& checkedRef;
void foo() {
consume(checked);
consume(checkedRef);
// expected-warning@-1{{Call argument is unchecked and unsafe [alpha.webkit.UncheckedCallArgsChecker]}}
}
void bar(WrapperObj& other) {
consume(other.checked);
// expected-warning@-1{{Call argument is unchecked and unsafe [alpha.webkit.UncheckedCallArgsChecker]}}
}
};

} // namespace call_args_checked

namespace call_args_default {

void someFunction(RefCountableAndCheckable* = makeObj());
Expand Down
22 changes: 22 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/unchecked-local-vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,28 @@ void foo() {

} // namespace local_assignment_to_global

namespace member_var {

struct WrapperObj {
CheckedObj checked;
CheckedObj& checkedRef;
void foo() {
auto *a = &checked;
a->method();
auto *b = &checkedRef;
// expected-warning@-1{{Local variable 'b' is unchecked and unsafe [alpha.webkit.UncheckedLocalVarsChecker]}}
b->method();
}

void bar(WrapperObj& wrapper) {
CheckedObj* ptr = &wrapper.checked;
// expected-warning@-1{{Local variable 'ptr' is unchecked and unsafe [alpha.webkit.UncheckedLocalVarsChecker]}}
ptr->method();
}
};

}

namespace local_refcountable_checkable_object {

RefCountableAndCheckable* provide_obj();
Expand Down
Loading