-
Notifications
You must be signed in to change notification settings - Fork 341
Require SwiftASTContextReader to be valid at all times #1440
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
Prior to this patch, it was possible to construct an invalid SwiftASTContextReader (e.g. via default-init). This led to subtle bugs: in rdar://65276251 for example, an invalid SwiftASTContextReader was assigned into an llvm::Optional, and subsequent code crashed because it assumed that the loaded llvm::Optional contained a valid instance. Make it so that SwiftASTContextReader is valid at all times. rdar://65276251
@swift-ci test |
lldb::TargetSP target_sp(GetTargetSP()); | ||
if (!target_sp) | ||
return {}; | ||
return llvm::None; |
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.
I guess we could leave this as is, too.
It's not quite clear to me how this change improves the code, but I'm probably missing something. Previously SwiftASTContextReader had an What's an example of a bad use the this prevents? |
Looking at the radar, could we have fixed this by changing
to
? |
My point is, I don't think we can prevent that kind of stupidity at the call site ;-) |
Hmm, you are right, this patch does protect against this misuse, because assigning an Optional to an Optional won't auto-wrap the Optional in another Optional! |
Yep, we certainly could have fixed the bug that way. In general I think In this case, it looks like we'd make_unique a copy of a SwiftASTContextReader, even if the original was invalid. We no longer do that. For a different kind of example, note how tons of functions have to repeatedly validity-check CompilerType in lldb. The same code smell doesn't exist in llvm, where there is simply no notion of an invalid Type. |
@swift-ci test Linux platform |
I like the argument that everything following an "optional" pattern should literally be an llvm::Optional, that's a good point! |
I agree it's a bit weird. I left 'get' as-is for convenience, since a large number of APIs expect |
What do you think about a tiny patch for the swift-5.3 branch that just fixes the programming error, and the idealized refactoring for master/master-next? |
Since all APIs taking a SwiftASTContext * probably return an error if it is null, we might even be able to delete some code by converting it to a reference. |
I'll spin off a more targeted bugfix for 5.3. Thanks! |
If you haven't done so already, can you please also cherry-pick this to swift/master-next? |
Just finished the merge. |
This is a narrower version of swiftlang#1440 spun off for 5.3. I've kept 'scratch_ctx' wrapped in an Optional to minimize the necessary source changes. Simply removing the Optional triggers a subsequent assert because the context lock is not taken (even though the context reader is invalid). rdar://65276251
Prior to this patch, it was possible to construct an invalid
SwiftASTContextReader (e.g. via default-init). This led to subtle bugs:
in rdar://65276251 for example, an invalid SwiftASTContextReader was
assigned into an llvm::Optional, and subsequent code crashed because it
assumed that the loaded llvm::Optional contained a valid instance.
Make it so that SwiftASTContextReader is valid at all times.
rdar://65276251