Skip to content

Add check against NULL before dereferencing pointer #62724

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 4 commits into from
Jan 26, 2023
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
4 changes: 1 addition & 3 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3863,9 +3863,6 @@ class AbstractClosureExpr : public DeclContext, public Expr {
/// Only valid when \c hasSingleExpressionBody() is true.
Expr *getSingleExpressionBody() const;

/// Whether this closure has a body
bool hasBody() const;

/// Returns the body of closures that have a body
/// returns nullptr if the closure doesn't have a body
BraceStmt *getBody() const;
Expand Down Expand Up @@ -4273,6 +4270,7 @@ class CaptureListExpr final : public Expr,
AbstractClosureExpr *closureBody)
: Expr(ExprKind::CaptureList, /*Implicit=*/false, Type()),
closureBody(closureBody) {
assert(closureBody);
Bits.CaptureListExpr.NumCaptures = captureList.size();
std::uninitialized_copy(captureList.begin(), captureList.end(),
getTrailingObjects<CaptureListEntry>());
Expand Down
12 changes: 0 additions & 12 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1885,19 +1885,7 @@ void AbstractClosureExpr::setParameterList(ParameterList *P) {
P->setDeclContextOfParamDecls(this);
}

bool AbstractClosureExpr::hasBody() const {
switch (getKind()) {
case ExprKind::Closure:
case ExprKind::AutoClosure:
return true;
default:
return false;
}
}

BraceStmt * AbstractClosureExpr::getBody() const {
if (!hasBody())
return nullptr;
if (const AutoClosureExpr *autocls = dyn_cast<AutoClosureExpr>(this))
return autocls->getBody();
if (const ClosureExpr *cls = dyn_cast<ClosureExpr>(this))
Expand Down
2 changes: 1 addition & 1 deletion lib/IDE/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2486,7 +2486,7 @@ class FormatWalker : public ASTWalker {
SourceLoc ContextLoc = SourceLoc()) {
AbstractClosureExpr *CE = CL->getClosureBody();
BraceStmt *BS = CE->getBody();
if (!CE || !BS)
if (!BS)
return None;

if (ContextLoc.isValid()) {
Expand Down
2 changes: 0 additions & 2 deletions lib/SIL/IR/SILProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ shouldWalkIntoExpr(Expr *E, ASTWalker::ParentTy Parent, SILDeclRef Constant) {
// initializer instead.
if (!Parent.isNull() || !Constant || !Constant.getAbstractClosureExpr())
return Action::SkipChildren(E);

assert(CE->hasBody());
}
return Action::Continue(E);
}
Expand Down