This is an archive of the discontinued LLVM Phabricator instance.

[clang] Check `expr` inside `InitListChecker::UpdateStructuredListElement()`
ClosedPublic

Authored by ArcsinX on Aug 4 2020, 4:49 AM.

Details

Summary
  • 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.

Diff Detail

Event Timeline

ArcsinX created this revision.Aug 4 2020, 4:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 4 2020, 4:49 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
ArcsinX requested review of this revision.Aug 4 2020, 4:49 AM

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.

Should the StructuredIndex still be incremented even in the case of an error, as done around line 1589 and 1647?

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?

Should the StructuredIndex still be incremented even in the case of an error, as done around line 1589 and 1647?

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?

ArcsinX updated this revision to Diff 284612.Aug 11 2020, 1:56 AM

Check for expr == nullptr inside InitListChecker::UpdateStructuredListElement()

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

aaron.ballman added inline comments.Aug 11 2020, 9:40 AM
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).

ArcsinX added inline comments.Aug 11 2020, 9:48 AM
clang/lib/Sema/SemaInit.cpp
3088

Could we add expr check after return? As far as we need to increment StructuredIndex in that case.

aaron.ballman accepted this revision.Aug 11 2020, 9:58 AM

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.

This revision is now accepted and ready to land.Aug 11 2020, 9:58 AM
ArcsinX updated this revision to Diff 284987.Aug 12 2020, 12:10 AM

Update comment

ArcsinX retitled this revision from [clang] Do not use an invalid expression to update the initializer. to [clang] Check `expr` inside `InitListChecker::UpdateStructuredListElement()`.Aug 12 2020, 3:17 AM
ArcsinX edited the summary of this revision. (Show Details)

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.

ArcsinX updated this revision to Diff 285101.Aug 12 2020, 9:14 AM

Fix comment

I have a suggestion for the added comment,

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!

aaron.ballman accepted this revision.Aug 12 2020, 10:13 AM

I have a suggestion for the added comment,

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!

Excellent, this LGTM, thank you for the fix!