-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
Conversation
lib/IDE/Formatting.cpp
Outdated
AbstractClosureExpr *CE = CL->getClosureBody(); | ||
BraceStmt *BS = CE->getBody(); | ||
BraceStmt *BS = CE ? CE->getBody() : NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of these can ever be a null pointer, but the invariants are neglected. Can we assert this for the closure upon CaptureListExpr
construction, and remove the always true hasBody()
check from AbstractClosureExpr::getBody
? This also seems to be the only remaining use of hasBody()
, so we may as well just delete it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lib/SIL/IR/SILProfiler.cpp
still uses hasBody()
in assert. But if hasBody()
always returns true
this assert could also be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AnthonyLatsis, are you sure I should remove hasBody()
completely?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lib/IDE/Formatting.cpp
Outdated
@@ -2485,8 +2485,9 @@ class FormatWalker : public ASTWalker { | |||
getIndentContextFrom(CaptureListExpr *CL, | |||
SourceLoc ContextLoc = SourceLoc()) { | |||
AbstractClosureExpr *CE = CL->getClosureBody(); | |||
assert(CE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not here, in the CaptureListExpr
constructor; sorry if I wasn’t clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I get lost. By constructor do you mean CaptureListExpr::create
? If so what should I assert there? assert(mem);
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By constructor do you mean
CaptureListExpr::create
?
Yes, but preferably the actual constructor, CaptureListExpr::CaptureListExpr
. It’s defined in the header.
If so what should I assert there?
assert(closureBody)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
I don't remember exactly why I added it. I think it was to mirror another API? Maybe the IIRC, the calls underlying |
@AnthonyLatsis's suggestions make sense to me. |
Good point. |
@swift-ci please smoke test |
@swift-ci please test source compatibility |
RxSwift failure is unrelated, merging. |
Condition following fix suggests pointer is expected sometimes to be NULL.