[clang] 026fbdf - [clang][Interp] Handle one-past-the-end pointers in SubPtr

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 6 02:48:44 PDT 2024


Author: Timm Bäder
Date: 2024-06-06T11:48:31+02:00
New Revision: 026fbdf934d4adc8b6abe544ba1bcfa3b64293ac

URL: https://github.com/llvm/llvm-project/commit/026fbdf934d4adc8b6abe544ba1bcfa3b64293ac
DIFF: https://github.com/llvm/llvm-project/commit/026fbdf934d4adc8b6abe544ba1bcfa3b64293ac.diff

LOG: [clang][Interp] Handle one-past-the-end pointers in SubPtr

Added: 
    

Modified: 
    clang/lib/AST/Interp/Interp.h
    clang/test/AST/Interp/arrays.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 17f05548a190..1248eeb79cbf 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -1773,8 +1773,10 @@ inline bool SubPtr(InterpState &S, CodePtr OpPC) {
     return true;
   }
 
-  T A = T::from(LHS.getIndex());
-  T B = T::from(RHS.getIndex());
+  T A = LHS.isElementPastEnd() ? T::from(LHS.getNumElems())
+                               : T::from(LHS.getIndex());
+  T B = RHS.isElementPastEnd() ? T::from(RHS.getNumElems())
+                               : T::from(RHS.getIndex());
   return AddSubMulHelper<T, T::sub, std::minus>(S, OpPC, A.bitWidth(), A, B);
 }
 

diff  --git a/clang/test/AST/Interp/arrays.cpp b/clang/test/AST/Interp/arrays.cpp
index dd5064d993e6..6146d41c5ff5 100644
--- a/clang/test/AST/Interp/arrays.cpp
+++ b/clang/test/AST/Interp/arrays.cpp
@@ -609,3 +609,9 @@ namespace ArrayMemberAccess {
     bool cond = a->x;
   }
 }
+
+namespace OnePastEndSub {
+  struct A {};
+  constexpr A a[3][3];
+  constexpr int 
diff 2 = &a[1][3] - &a[1][0]; /// Used to crash.
+}


        


More information about the cfe-commits mailing list