This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Fix crash when emitting diagnostic for out of order designated initializers in C++
ClosedPublic

Authored by shafik on Jul 6 2023, 7:05 PM.

Details

Summary

In C++ we are not allowed to use designated initializers to initialize fields out of order. In some cases when diagnosing this we are crashing because we are not indexing correctly and therefore going out of bounds.

This fixes: https://github.com/llvm/llvm-project/issues/63605

Diff Detail

Event Timeline

shafik created this revision.Jul 6 2023, 7:05 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 6 2023, 7:05 PM
shafik requested review of this revision.Jul 6 2023, 7:05 PM
shafik added inline comments.
clang/test/SemaCXX/cxx2a-initializer-aggregates.cpp
195

Note the e in the diagnostic in this line and the next line should be b. I have not managed to figure out how to fix this yet but this feels relatively minor compared to the crash.

Thanks Shafik.

I have some concern about the diagnostics being wonky because, if the init and field don't match, it's hard to be sure we won't run into simar issues (although i agree this fixes the crash.)

Also, I'm not saying you should do this but... maybe this thing could be improved by a sort of field_iterator that skips over unnamed bitfield.

clang/lib/Sema/SemaInit.cpp
2839–2840

I think this is probably where it fails produce something sensible to diagnostics.
in the *NextField == RD->field_end() case we just go straight to the end.

In that case, we probably want to break when the previous element is the one under test, that way you might get to b.

clang/test/SemaCXX/cxx2a-initializer-aggregates.cpp
182

Should we add bases?

shafik updated this revision to Diff 538323.Jul 7 2023, 10:47 PM
shafik marked an inline comment as done.

-Add fix for wrong field in diagnostic

I can't really see any details on the libcxx failure :-(

shafik added inline comments.Jul 8 2023, 10:36 AM
clang/test/SemaCXX/cxx2a-initializer-aggregates.cpp
182

Good catch b/c of course bases would cause problems 🤣

shafik updated this revision to Diff 538456.Jul 9 2023, 3:16 PM
  • Stopped using NumBases when calculating OldIndex
  • Added bases classes to the test and added more test cases
shafik marked an inline comment as done.Jul 9 2023, 3:18 PM

Found a new issue https://github.com/llvm/llvm-project/issues/63759 but this feels different enough that I will deal with it separately.

clang/test/SemaCXX/cxx2a-initializer-aggregates.cpp
182

Problems due to bases now fixed as well.

195

This is now fixed

shafik added a comment.Jul 9 2023, 5:54 PM

@aaron.ballman I can't figure out why the libcxx bot failed, can you understand why?

Thanks, this looks good! (the libc++ test seems completely unrelated, it happens before compilation starts).
Can you add re release note though? Thanks!

Thanks, this looks good! (the libc++ test seems completely unrelated, it happens before compilation starts).
Can you add re release note though? Thanks!

+1 to both of these sentences. :-)

clang/lib/Sema/SemaInit.cpp
2850

Should we be asserting that StructuredIndex is not 0? The logic in this function makes it tough to see, but the fact that we're in a block checking IsFirstDesignator makes me think this is dangerous.

clang/test/SemaCXX/cxx2a-initializer-aggregates.cpp
66

A few questions: 1) what is this new note attached to? The only reorder-* diagnostic I see in the test is for the initialization of x. 2) is the note actually on the correct previous use? I would have expected this to be on the assignment to y one line above.

shafik added inline comments.Jul 10 2023, 2:10 PM
clang/test/SemaCXX/cxx2a-initializer-aggregates.cpp
66

It is attached to :

.x = 1, // reorder-error {{declaration order}}

We were not issuing it before b/c it was broken, in this case it was just not crashing but it was still broken.

We could argue the note should go on the first .y although I could see both sides since the override is a warning and not an error I think using the second is defensible.

aaron.ballman accepted this revision.Jul 11 2023, 6:22 AM

LGTM but please land with a release note.

clang/test/SemaCXX/cxx2a-initializer-aggregates.cpp
66

Oh! I had a brain fart and thought this was about *re*initialization of y. I see what's going on now and yeah, this makes sense. I'm not worried about which y the note attaches to.

This revision is now accepted and ready to land.Jul 11 2023, 6:22 AM
shafik updated this revision to Diff 540587.Jul 14 2023, 3:49 PM
shafik marked 2 inline comments as done.
  • Add release notes.
This revision was landed with ongoing or failed builds.Jul 14 2023, 4:00 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptJul 14 2023, 4:00 PM