- Prevent nullptr-deference at try to emit warning for invalid expr
- Simplify InitListChecker::UpdateStructuredListElement() usages. We do not need to check expr and increment StructuredIndex (for invalid expr) before the call anymore.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
Should the StructuredIndex still be incremented even in the case of an error, as done around line 1589 and 1647? Do we need a similar change around line 2405?
I sort of wonder if the correct change is to make UpdateStructuredListElement() resilient to being passed a null Expr * and then removing the manual increments when the expression is an error.
Yes, it looks like we need to increment StructuredIndex.
Do we need a similar change around line 2405?
Think so. But I could not find a test case for this.
I sort of wonder if the correct change is to make UpdateStructuredListElement() resilient to being passed a null Expr * and then removing the manual increments when the expression is an error.
Should it be in this patch?
Given that we have at least four uses of this pattern in the file, I'd say it should be this patch. WDYT?
Agree, updated patch.
But I am not sure about correct place for expr check. Seems nullptr dereference possible only here: diagnoseInitOverride(PrevInit, expr->getSourceRange());.
clang/lib/Sema/SemaInit.cpp | ||
---|---|---|
3088 | I would move the check up to here as the only time we should get a null expression is if something else has gone wrong (and update the comment as well). |
clang/lib/Sema/SemaInit.cpp | ||
---|---|---|
3088 | Could we add expr check after return? As far as we need to increment StructuredIndex in that case. |
LGTM!
clang/lib/Sema/SemaInit.cpp | ||
---|---|---|
3088 | Ack, sorry about my think-o, you're absolutely correct! From what I can tell, updateInit() is resilient to a null Expr*, so your code is correct as-is. It looked odd to my eyes because it made me wonder if we need to still issue the diagnostic given the comment. Leaving your code as-is and updating the adjacent comment would also solve my concern. |
I have a suggestion for the added comment, but I also forgot to ask, do you need someone to commit on your behalf?
clang/lib/Sema/SemaInit.cpp | ||
---|---|---|
3094 | I was hoping for something that describes the "why" more than the "what". How about: No need to diagnose when expr is null because a more relevant diagnostic has already been issued and this diagnostic is potentially noise. |
Fixed comment according to your suggestion.
but I also forgot to ask, do you need someone to commit on your behalf?
I already have commit access.
Thanks for review!
I would move the check up to here as the only time we should get a null expression is if something else has gone wrong (and update the comment as well).