[PATCH] D157252: [clang][ExprConst] Handle 0 type size in builtin_memcpy etc.

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 7 00:08:09 PDT 2023


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

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157252

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/test/Sema/builtin-memcpy.c


Index: clang/test/Sema/builtin-memcpy.c
===================================================================
--- /dev/null
+++ clang/test/Sema/builtin-memcpy.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify
+
+/// Zero-sized structs should not crash.
+int b() {
+  struct {      } a[10];
+  __builtin_memcpy(&a[2], a, 2); // expected-warning {{buffer has size 0, but size argument is 2}}
+  return 0;
+}
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -9509,6 +9509,8 @@
 
     // Figure out how many T's we're copying.
     uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity();
+    if (TSize == 0)
+      return false;
     if (!WChar) {
       uint64_t Remainder;
       llvm::APInt OrigN = N;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157252.547655.patch
Type: text/x-patch
Size: 850 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230807/ec0144be/attachment.bin>


More information about the cfe-commits mailing list