[PATCH] D135920: [clang][Sema] Use correct array size for diagnostic

Bill Wendling via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 13 14:30:30 PDT 2022


void created this revision.
void added reviewers: kees, serge-sans-paille, rsmith, efriedma.
Herald added a project: All.
void requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The diagnostic was confusing and reporting that an array contains far
more elements than it is defined to have, due to casting.

For example, this code:

  double foo[4096];
  ((char*)foo)[sizeof(foo)];

warns that the "index 32768 is past the end of the array (which contains
32768 elements)."


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135920

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/Sema/array-bounds-ptr-arith.c
  clang/test/SemaCXX/array-bounds.cpp


Index: clang/test/SemaCXX/array-bounds.cpp
===================================================================
--- clang/test/SemaCXX/array-bounds.cpp
+++ clang/test/SemaCXX/array-bounds.cpp
@@ -237,7 +237,7 @@
     ((char*)foo)[sizeof(foo) - 1] = '\0';  // no-warning
     *(((char*)foo) + sizeof(foo) - 1) = '\0';  // no-warning
 
-    ((char*)foo)[sizeof(foo)] = '\0';  // expected-warning {{array index 32768 is past the end of the array (which contains 32768 elements)}}
+    ((char*)foo)[sizeof(foo)] = '\0';  // expected-warning {{array index 32768 is past the end of the array (which contains 4096 elements)}}
 
     // TODO: This should probably warn, too.
     *(((char*)foo) + sizeof(foo)) = '\0';  // no-warning
Index: clang/test/Sema/array-bounds-ptr-arith.c
===================================================================
--- clang/test/Sema/array-bounds-ptr-arith.c
+++ clang/test/Sema/array-bounds-ptr-arith.c
@@ -12,7 +12,7 @@
 }
 void* broken (struct ext2_super_block *es,int a)
 {
-	 return (void *)es->s_uuid + 80; // expected-warning {{refers past the end of the array}}
+	 return (void *)es->s_uuid + 80; // expected-warning {{refers past the end of the array (that contains 8 elements)}}
 }
 
 // Test case reduced from PR11594
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -16066,7 +16066,7 @@
 
     DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr,
                         PDiag(DiagID) << toString(index, 10, true)
-                                      << toString(size, 10, true)
+                                      << toString(ArrayTy->getSize(), 10, true)
                                       << (unsigned)size.getLimitedValue(~0U)
                                       << IndexExpr->getSourceRange());
   } else {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135920.467607.patch
Type: text/x-patch
Size: 1898 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221013/5352af67/attachment.bin>


More information about the cfe-commits mailing list