[clang] c15e583 - [clang][Interp] Fix nullptr array dereferencing (#75798)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 7 23:33:19 PST 2024
Author: Timm Baeder
Date: 2024-01-08T08:33:15+01:00
New Revision: c15e5836d49763e43736d13eb4b873e01dcc9ef0
URL: https://github.com/llvm/llvm-project/commit/c15e5836d49763e43736d13eb4b873e01dcc9ef0
DIFF: https://github.com/llvm/llvm-project/commit/c15e5836d49763e43736d13eb4b873e01dcc9ef0.diff
LOG: [clang][Interp] Fix nullptr array dereferencing (#75798)
The attached test case would cause an assertion failure in Pointer.h
when operating on a null pointer.
Added:
Modified:
clang/lib/AST/Interp/Interp.cpp
clang/lib/AST/Interp/Interp.h
clang/test/AST/Interp/arrays.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/Interp.cpp b/clang/lib/AST/Interp/Interp.cpp
index a82d1c3c7c622a..21ea2503b94bff 100644
--- a/clang/lib/AST/Interp/Interp.cpp
+++ b/clang/lib/AST/Interp/Interp.cpp
@@ -290,10 +290,10 @@ bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
}
bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
- if (!CheckDummy(S, OpPC, Ptr))
- return false;
if (!CheckLive(S, OpPC, Ptr, AK_Read))
return false;
+ if (!CheckDummy(S, OpPC, Ptr))
+ return false;
if (!CheckExtern(S, OpPC, Ptr))
return false;
if (!CheckRange(S, OpPC, Ptr, AK_Read))
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 828d4ea35526d6..c05dea0cc55d3c 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -1813,9 +1813,6 @@ inline bool ArrayElemPtr(InterpState &S, CodePtr OpPC) {
const T &Offset = S.Stk.pop<T>();
const Pointer &Ptr = S.Stk.peek<Pointer>();
- if (!CheckArray(S, OpPC, Ptr))
- return false;
-
if (!OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr))
return false;
@@ -1843,9 +1840,6 @@ inline bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC) {
const T &Offset = S.Stk.pop<T>();
const Pointer &Ptr = S.Stk.pop<Pointer>();
- if (!CheckArray(S, OpPC, Ptr))
- return false;
-
if (!OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr))
return false;
diff --git a/clang/test/AST/Interp/arrays.cpp b/clang/test/AST/Interp/arrays.cpp
index c455731e76699f..4aa10da55dd3ae 100644
--- a/clang/test/AST/Interp/arrays.cpp
+++ b/clang/test/AST/Interp/arrays.cpp
@@ -72,6 +72,14 @@ constexpr int getElementFromEnd(const int *Arr, int size, int index) {
static_assert(getElementFromEnd(data, 5, 0) == 1, "");
static_assert(getElementFromEnd(data, 5, 4) == 5, "");
+constexpr int getFirstElem(const int *a) {
+ return a[0]; // expected-note {{read of dereferenced null pointer}} \
+ // ref-note {{read of dereferenced null pointer}}
+}
+static_assert(getFirstElem(nullptr) == 1, ""); // expected-error {{not an integral constant expression}} \
+ // expected-note {{in call to}} \
+ // ref-error {{not an integral constant expression}} \
+ // ref-note {{in call to}}
constexpr static int arr[2] = {1,2};
constexpr static int arr2[2] = {3,4};
More information about the cfe-commits
mailing list