This is an archive of the discontinued LLVM Phabricator instance.

[clang] Fix crash when array size is missing in initializer
ClosedPublic

Authored by tbaeder on Feb 10 2022, 11:38 PM.

Details

Summary

This fixes the crash from https://github.com/llvm/llvm-project/issues/53742

CXXNewExpr::getArraySize() may return a llvm::Optional<> pointing to a nullptr.

Diff Detail

Event Timeline

tbaeder requested review of this revision.Feb 10 2022, 11:38 PM
tbaeder created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 10 2022, 11:38 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
tbaeder updated this revision to Diff 407785.Feb 10 2022, 11:47 PM

This is a pretty big gotcha at best and I feel like getArraySize() should return None in that case instead... thoughts?

This is a pretty big gotcha at best and I feel like getArraySize() should return None in that case instead... thoughts?

I think it is a gotcha -- some places protect against a null pointer (https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/TreeTransform.h#L11923 and https://github.com/llvm/llvm-project/blob/main/clang/lib/AST/StmtPrinter.cpp#L2138) while other places assume the pointer isn't null (https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CGExprCXX.cpp#L731 and https://github.com/llvm/llvm-project/blob/main/clang/lib/AST/ExprConstant.cpp#L9431). So I think a better approach is to fix up the interface so it doesn't return nullptr rather than play whack-a-mole forever with the API (and fix up the places currently checking for nullptr explicitly).

tbaeder updated this revision to Diff 407885.Feb 11 2022, 8:12 AM
tbaeder added inline comments.
clang/lib/AST/StmtPrinter.cpp
2135

I changed this just for clarity, not doing it did not cause any tests to fail. Not sure if this is tested at all.

aaron.ballman accepted this revision.Feb 11 2022, 8:41 AM

LGTM aside from some minor nits. Can you also add a release note for the change?

clang/include/clang/AST/ExprCXX.h
2271
2285
clang/lib/AST/StmtPrinter.cpp
2135

-ast-print is... pretty terrible (I've had half a mind to put up an RFC asking if we should remove it entirely, that's how unmaintained it is), so I suspect it's not tested.

This revision is now accepted and ready to land.Feb 11 2022, 8:41 AM
tbaeder updated this revision to Diff 407912.Feb 11 2022, 9:26 AM
tbaeder marked 3 inline comments as done.
tbaeder added inline comments.
clang/lib/AST/StmtPrinter.cpp
2135

Alright, I'll leave it at that then. I briefly tried to test the code but didn't succeed.

tbaeder marked an inline comment as done.Feb 13 2022, 6:13 AM

Can you also add a release note for the change?

Would this go in the "libclang" section in clang/docs/ReleaseNotes.rst? Or somewhere else?

Can you also add a release note for the change?

Would this go in the "libclang" section in clang/docs/ReleaseNotes.rst? Or somewhere else?

Nope, I'd add a new section titled "Bug Fixes" after "Major New Features" and then add it there.

tbaeder updated this revision to Diff 409173.Feb 16 2022, 1:23 AM

Continues to LGTM, though I had a recommendation for the release note. Feel free to land when you'd like.

clang/docs/ReleaseNotes.rst
59

Just added a reference to the bug that was fixed.

tbaeder added inline comments.Feb 16 2022, 7:07 AM
clang/docs/ReleaseNotes.rst
59

I was wondering about this. Where does the "PRXXXXXX" syntax come from? Since the repo and issues are on github now, I don't see how it makes sense to call it a PR (it's an issue, not a pull request) and github doesn't linkify those, while it does when using the #xxxxxx syntax (which isn't relevant in this case, but it is when using it in git commit messages). I have seen other people use the same syntax to refer to issues. I'd probably just add an actual link to the github issue here if that's permitted. Thanks for the suggestion though, I'm on PTO this week so don't land this until Monday :)

aaron.ballman added inline comments.Feb 17 2022, 6:15 AM
clang/docs/ReleaseNotes.rst
59

I was wondering about this. Where does the "PRXXXXXX" syntax come from?

"Problem Report" -- ancient terminology.

I have seen other people use the same syntax to refer to issues. I'd probably just add an actual link to the github issue here if that's permitted.

TBH, I think that's an even better suggestion (linking to the issue). One concern I have is that it's really hard to tell whether the number is a bugzilla issue number or a GitHub issue number (I suppose we should all assume they're always github issue numbers these days though), so I wasn't keen on having a number with no prefix to it. But if we're linking to what's been fixed, then there's never a chance for confusion.

Thanks for the suggestion though, I'm on PTO this week so don't land this until Monday :)

Sounds good to me, enjoy your PTO!

tbaeder updated this revision to Diff 410233.Feb 20 2022, 11:58 PM
tbaeder marked an inline comment as done.
tbaeder added inline comments.
clang/docs/ReleaseNotes.rst
59

I added a link for the new entry and replaced the old PR mention with a link as well, so it's consistent at least.

This revision was landed with ongoing or failed builds.Feb 22 2022, 7:28 AM
This revision was automatically updated to reflect the committed changes.
tbaeder marked an inline comment as done.