[clang] [clang][ExprConst] Fix crash on uninitialized array subobject (PR #67817)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 29 07:58:51 PDT 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
<details>
<summary>Changes</summary>
https://reviews.llvm.org/D146358 was assuming that all subobjects have
their own name (`SubobjectDecl`), but it was not true for array
elements.
Fixes https://github.com/llvm/llvm-project/issues/67317
---
Full diff: https://github.com/llvm/llvm-project/pull/67817.diff
4 Files Affected:
- (modified) clang/include/clang/Basic/DiagnosticASTKinds.td (+1-1)
- (modified) clang/lib/AST/ExprConstant.cpp (+9-4)
- (modified) clang/lib/AST/Interp/Interp.cpp (+1-1)
- (modified) clang/test/SemaCXX/eval-crashes.cpp (+7)
``````````diff
diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td b/clang/include/clang/Basic/DiagnosticASTKinds.td
index d2656310e79c9b8..71dabac2d32402a 100644
--- a/clang/include/clang/Basic/DiagnosticASTKinds.td
+++ b/clang/include/clang/Basic/DiagnosticASTKinds.td
@@ -69,7 +69,7 @@ def note_consteval_address_accessible : Note<
"%select{pointer|reference}0 to a consteval declaration "
"is not a constant expression">;
def note_constexpr_uninitialized : Note<
- "subobject %0 is not initialized">;
+ "subobject %select{of type |}0%1 is not initialized">;
def note_constexpr_uninitialized_base : Note<
"constructor of base class %0 is not called">;
def note_constexpr_static_local : Note<
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index f0d53d6ae18e804..acf4b33242c4a27 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -2411,10 +2411,15 @@ static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
const FieldDecl *SubobjectDecl,
CheckedTemporaries &CheckedTemps) {
if (!Value.hasValue()) {
- assert(SubobjectDecl && "SubobjectDecl shall be non-null");
- Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized) << SubobjectDecl;
- Info.Note(SubobjectDecl->getLocation(),
- diag::note_constexpr_subobject_declared_here);
+ if (SubobjectDecl) {
+ Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized)
+ << true << SubobjectDecl;
+ Info.Note(SubobjectDecl->getLocation(),
+ diag::note_constexpr_subobject_declared_here);
+ } else {
+ // FIXME: We should add a test to check the output of this case.
+ Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized) << false << Type;
+ }
return false;
}
diff --git a/clang/lib/AST/Interp/Interp.cpp b/clang/lib/AST/Interp/Interp.cpp
index e1951574edb6288..0e7e9bac1c8079a 100644
--- a/clang/lib/AST/Interp/Interp.cpp
+++ b/clang/lib/AST/Interp/Interp.cpp
@@ -404,7 +404,7 @@ bool CheckPure(InterpState &S, CodePtr OpPC, const CXXMethodDecl *MD) {
static void DiagnoseUninitializedSubobject(InterpState &S, const SourceInfo &SI,
const FieldDecl *SubObjDecl) {
assert(SubObjDecl && "Subobject declaration does not exist");
- S.FFDiag(SI, diag::note_constexpr_uninitialized) << SubObjDecl;
+ S.FFDiag(SI, diag::note_constexpr_uninitialized) << true << SubObjDecl;
S.Note(SubObjDecl->getLocation(),
diag::note_constexpr_subobject_declared_here);
}
diff --git a/clang/test/SemaCXX/eval-crashes.cpp b/clang/test/SemaCXX/eval-crashes.cpp
index 3e59ad31c559da8..ac04b113f99b7aa 100644
--- a/clang/test/SemaCXX/eval-crashes.cpp
+++ b/clang/test/SemaCXX/eval-crashes.cpp
@@ -54,3 +54,10 @@ namespace pr33140_10 {
int a(const int &n = 0);
bool b() { return a() == a(); }
}
+
+namespace GH67317 {
+struct array {
+ int (&data)[2];
+ array() : data(*new int[1][2]) {}
+};
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/67817
More information about the cfe-commits
mailing list