diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -185,6 +185,8 @@ terminated. Clang should now also recover better when an @end is missing between blocks. `Issue 64065 `_ +- Fixed a crash when check array access on zero-length element. + `Issue 64564 `_ Target Specific Changes ----------------------- diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -17146,7 +17146,7 @@ ASTC.getTypeSizeInCharsIfKnown(EffectiveType); // PR50741 - If EffectiveType has unknown size (e.g., if it's a void // pointer) bounds-checking isn't meaningful. - if (!ElemCharUnits) + if (!ElemCharUnits || ElemCharUnits->isZero()) return; llvm::APInt ElemBytes(index.getBitWidth(), ElemCharUnits->getQuantity()); // If index has more active bits than address space, we already know diff --git a/clang/test/Sema/array-bounds-zero-length-elem-gh64564.c b/clang/test/Sema/array-bounds-zero-length-elem-gh64564.c new file mode 100644 --- /dev/null +++ b/clang/test/Sema/array-bounds-zero-length-elem-gh64564.c @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -triple i686-apple-darwin -verify %s + +int a[][0]; // expected-warning {{tentative array definition assumed to have one element}} +void gh64564_1(void) { + int b = a[0x100000000][0]; +} + +typedef struct {} S; +S s[]; // expected-warning {{tentative array definition assumed to have one element}} +void gh64564_2(void) { + S t = s[0x100000000]; +}