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

Conversation

valeriyvan
Copy link
Contributor

Condition following fix suggests pointer is expected sometimes to be NULL.

Comment on lines 2487 to 2488
AbstractClosureExpr *CE = CL->getClosureBody();
BraceStmt *BS = CE->getBody();
BraceStmt *BS = CE ? CE->getBody() : NULL;
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

@valeriyvan valeriyvan Jan 4, 2023

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure I should remove hasBody() completely?

At least I’d vote for it. @etcwilde What was the motivation for adding it in c57b80a?

@AnthonyLatsis AnthonyLatsis added code health compiler The Swift compiler itself type checker Area → compiler: Semantic analysis labels Dec 21, 2022
@AnthonyLatsis AnthonyLatsis requested a review from xedin January 8, 2023 17:53
@@ -2485,8 +2485,9 @@ class FormatWalker : public ASTWalker {
getIndentContextFrom(CaptureListExpr *CL,
SourceLoc ContextLoc = SourceLoc()) {
AbstractClosureExpr *CE = CL->getClosureBody();
assert(CE);
Copy link
Collaborator

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.

Copy link
Contributor Author

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);?

Copy link
Collaborator

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@etcwilde
Copy link
Member

etcwilde commented Jan 9, 2023

are you sure I should remove hasBody() completely?

At least I’d vote for it. @etcwilde What was the motivation for adding it in c57b80a?

I don't remember exactly why I added it. I think it was to mirror another API? Maybe the FuncDecl APIs?
Anyway, yeah, it doesn't look terribly useful.

IIRC, the calls underlying getBody at least used to be able to return nullptr if you were querying it during type checking since they bodies hadn't necessarily been set yet. If something failed to type check, you will probably still want to be defensive here.

@xedin
Copy link
Contributor

xedin commented Jan 10, 2023

@AnthonyLatsis's suggestions make sense to me.

@AnthonyLatsis
Copy link
Collaborator

IIRC, the calls underlying getBody at least used to be able to return nullptr if you were querying it during type checking since they bodies hadn't necessarily been set yet. If something failed to type check, you will probably still want to be defensive here.

Good point.

@AnthonyLatsis
Copy link
Collaborator

@swift-ci please smoke test

@AnthonyLatsis AnthonyLatsis self-assigned this Jan 25, 2023
@xedin
Copy link
Contributor

xedin commented Jan 25, 2023

@swift-ci please test source compatibility

@xedin
Copy link
Contributor

xedin commented Jan 26, 2023

RxSwift failure is unrelated, merging.

@xedin xedin merged commit 7e0f02a into swiftlang:main Jan 26, 2023
@AnthonyLatsis AnthonyLatsis removed their assignment Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code health compiler The Swift compiler itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants