Author: dingfei Date: 2023-08-11T22:05:04+08:00 New Revision: 0f73a2406a16f3a69dd6e62f0faa3a4aa05c5d84
URL: https://github.com/llvm/llvm-project/commit/0f73a2406a16f3a69dd6e62f0faa3a4aa05c5d84 DIFF: https://github.com/llvm/llvm-project/commit/0f73a2406a16f3a69dd6e62f0faa3a4aa05c5d84.diff LOG: [clang][Sema] Skip access check on arrays of zero-length element Bound check on array of zero-sized element isn't meaningful. Fixes https://github.com/llvm/llvm-project/issues/64564 Reviewed By: jacquesguan Differential Revision: https://reviews.llvm.org/D157584 Added: clang/test/Sema/array-bounds-zero-length-elem-gh64564.c Modified: clang/docs/ReleaseNotes.rst clang/lib/Sema/SemaChecking.cpp Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index c438db074a19b7..a80f57d9bb71ac 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -185,6 +185,8 @@ Miscellaneous Clang Crashes Fixed terminated. Clang should now also recover better when an @end is missing between blocks. `Issue 64065 <https://github.com/llvm/llvm-project/issues/64065>`_ +- Fixed a crash when check array access on zero-length element. + `Issue 64564 <https://github.com/llvm/llvm-project/issues/64564>`_ Target Specific Changes ----------------------- diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 09696ee73ca38c..984e43c1fcfad5 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -17146,7 +17146,7 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, 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 index 00000000000000..1aaf01d82011bc --- /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]; +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits