[clang] 7bc793a - [clang][Interp] Check pointer inc/dec ops for null (#69168)

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 16 21:53:37 PDT 2023


Author: Timm Baeder
Date: 2023-10-17T06:53:33+02:00
New Revision: 7bc793a6925ccebbe21f1c98a79d6dc89a615c01

URL: https://github.com/llvm/llvm-project/commit/7bc793a6925ccebbe21f1c98a79d6dc89a615c01
DIFF: https://github.com/llvm/llvm-project/commit/7bc793a6925ccebbe21f1c98a79d6dc89a615c01.diff

LOG: [clang][Interp] Check pointer inc/dec ops for null (#69168)

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 e3e6a4cec63b194..3d226a40f9cf608 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -1488,11 +1488,14 @@ static inline bool IncDecPtrHelper(InterpState &S, CodePtr OpPC,
                                    const Pointer &Ptr) {
   using OneT = Integral<8, false>;
 
+  const Pointer &P = Ptr.deref<Pointer>();
+  if (!CheckNull(S, OpPC, P, CSK_ArrayIndex))
+    return false;
+
   // Get the current value on the stack.
-  S.Stk.push<Pointer>(Ptr.deref<Pointer>());
+  S.Stk.push<Pointer>(P);
 
   // Now the current Ptr again and a constant 1.
-  Pointer P = Ptr.deref<Pointer>();
   OneT One = OneT::from(1);
   if (!OffsetHelper<OneT, Op>(S, OpPC, One, P))
     return false;

diff  --git a/clang/test/AST/Interp/arrays.cpp b/clang/test/AST/Interp/arrays.cpp
index 281835f828bbd7c..18c4ae4354f54a0 100644
--- a/clang/test/AST/Interp/arrays.cpp
+++ b/clang/test/AST/Interp/arrays.cpp
@@ -333,6 +333,26 @@ namespace IncDec {
                                    // expected-note {{in call to}} \
                                    // ref-error {{not an integral constant expression}} \
                                   // ref-note {{in call to}}
+
+  constexpr int nullptr1(bool Pre) {
+    int *a = nullptr;
+    if (Pre)
+      ++a; // ref-note {{arithmetic on null pointer}} \
+           // expected-note {{arithmetic on null pointer}}
+    else
+      a++; // ref-note {{arithmetic on null pointer}} \
+           // expected-note {{arithmetic on null pointer}}
+    return 1;
+  }
+  static_assert(nullptr1(true) == 1, ""); // ref-error {{not an integral constant expression}} \
+                                          // ref-note {{in call to}} \
+                                          // expected-error {{not an integral constant expression}} \
+                                          // expected-note {{in call to}}
+
+  static_assert(nullptr1(false) == 1, ""); // ref-error {{not an integral constant expression}} \
+                                           // ref-note {{in call to}} \
+                                           // expected-error {{not an integral constant expression}} \
+                                           // expected-note {{in call to}}
 };
 
 namespace ZeroInit {


        


More information about the cfe-commits mailing list