[clang] [clang][Interp] Check pointer inc/dec ops for null (PR #69168)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 16 01:47:05 PDT 2023
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/69168
None
>From b20da36444adfbdbddd6d0dddf06535815ffe0c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 3 Sep 2023 07:03:04 +0200
Subject: [PATCH] [clang][Interp] Check pointer inc/dec ops for null
---
clang/lib/AST/Interp/Interp.h | 7 +++++--
clang/test/AST/Interp/arrays.cpp | 20 ++++++++++++++++++++
2 files changed, 25 insertions(+), 2 deletions(-)
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