[PATCH] D157584: [clang][Sema] Skip access check on arrays of zero-length element

Ding Fei via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 9 23:39:40 PDT 2023


danix800 created this revision.
danix800 added a project: clang.
Herald added a project: All.
danix800 requested review of this revision.
Herald added a subscriber: cfe-commits.

Bound check on array of zero-sized element isn't meaningful.

Fixes https://github.com/llvm/llvm-project/issues/64564


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157584

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaChecking.cpp
  clang/test/Sema/array-bounds-zero-length-elem-gh64564.c


Index: clang/test/Sema/array-bounds-zero-length-elem-gh64564.c
===================================================================
--- /dev/null
+++ 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];
+}
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ 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
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -181,6 +181,8 @@
   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
 -----------------------


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157584.548884.patch
Type: text/x-patch
Size: 1777 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230810/8dab8df1/attachment.bin>


More information about the cfe-commits mailing list