Skip to content

[clang][Interp] Diagnose pointer subtraction on zero-size arrays #103015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2024

Conversation

tbaederr
Copy link
Contributor

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 13, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 13, 2024

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/103015.diff

3 Files Affected:

  • (modified) clang/lib/AST/Interp/Interp.h (+16)
  • (modified) clang/lib/AST/Interp/Pointer.h (+5-1)
  • (modified) clang/test/AST/Interp/arrays.cpp (+13)
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 67b3fc5064509..3eab0cfd87138 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -1987,6 +1987,22 @@ inline bool SubPtr(InterpState &S, CodePtr OpPC) {
   const Pointer &LHS = S.Stk.pop<Pointer>();
   const Pointer &RHS = S.Stk.pop<Pointer>();
 
+  for (const Pointer &P : {LHS, RHS}) {
+    if (P.isZeroSizeArray()) {
+      QualType PtrT = P.getType();
+      while (auto *AT = dyn_cast<ArrayType>(PtrT))
+        PtrT = AT->getElementType();
+
+      QualType ArrayTy = S.getCtx().getConstantArrayType(
+          PtrT, APInt::getZero(1), nullptr, ArraySizeModifier::Normal, 0);
+      S.FFDiag(S.Current->getSource(OpPC),
+               diag::note_constexpr_pointer_subtraction_zero_size)
+          << ArrayTy;
+
+      return false;
+    }
+  }
+
   if (RHS.isZero()) {
     S.Stk.push<T>(T::from(LHS.getIndex()));
     return true;
diff --git a/clang/lib/AST/Interp/Pointer.h b/clang/lib/AST/Interp/Pointer.h
index 07ff8025ba954..01ccb88ec03b2 100644
--- a/clang/lib/AST/Interp/Pointer.h
+++ b/clang/lib/AST/Interp/Pointer.h
@@ -613,7 +613,11 @@ class Pointer {
   bool isElementPastEnd() const { return Offset == PastEndMark; }
 
   /// Checks if the pointer is pointing to a zero-size array.
-  bool isZeroSizeArray() const { return getFieldDesc()->isZeroSizeArray(); }
+  bool isZeroSizeArray() const {
+    if (const auto *Desc = getFieldDesc())
+      return Desc->isZeroSizeArray();
+    return false;
+  }
 
   /// Dereferences the pointer, if it's live.
   template <typename T> T &deref() const {
diff --git a/clang/test/AST/Interp/arrays.cpp b/clang/test/AST/Interp/arrays.cpp
index 98cd17276e0a0..47706bedcf3f0 100644
--- a/clang/test/AST/Interp/arrays.cpp
+++ b/clang/test/AST/Interp/arrays.cpp
@@ -632,3 +632,16 @@ constexpr int fail(const int &p) {
 }
 static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // both-error {{not an integral constant expression}} \
                                                                                     // both-note {{in call to}}
+
+namespace ZeroSizeTypes {
+  constexpr int (*p1)[0] = 0, (*p2)[0] = 0;
+  constexpr int k = p2 - p1; // both-error {{constexpr variable 'k' must be initialized by a constant expression}} \
+                             // both-note {{subtraction of pointers to type 'int[0]' of zero size}} \
+                             // both-warning {{subtraction of pointers to type 'int[0]' of zero size has undefined behavior}}
+
+  int arr[5][0];
+  constexpr int f() { // both-error {{never produces a constant expression}}
+    return &arr[3] - &arr[0]; // both-note {{subtraction of pointers to type 'int[0]' of zero size}} \
+                              // both-warning {{subtraction of pointers to type 'int[0]' of zero size has undefined behavior}}
+  }
+}

@tbaederr tbaederr merged commit 13008aa into llvm:main Aug 14, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants