Skip to content

Commit 411c5dd

Browse files
committed
[clang][Interp] Handle null function pointers
We were instead asserting that they are non-null before.
1 parent d6ded91 commit 411c5dd

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

clang/lib/AST/Interp/FunctionPointer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class FunctionPointer final {
2626
FunctionPointer(const Function *Func) : Func(Func) { assert(Func); }
2727

2828
const Function *getFunction() const { return Func; }
29+
bool isZero() const { return !Func; }
2930

3031
APValue toAPValue() const {
3132
if (!Func)

clang/lib/AST/Interp/Interp.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,12 @@ inline bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize) {
20872087
const FunctionPointer &FuncPtr = S.Stk.pop<FunctionPointer>();
20882088

20892089
const Function *F = FuncPtr.getFunction();
2090+
if (!F) {
2091+
const Expr *E = S.Current->getExpr(OpPC);
2092+
S.FFDiag(E, diag::note_constexpr_null_callee)
2093+
<< const_cast<Expr *>(E) << E->getSourceRange();
2094+
return false;
2095+
}
20902096
assert(F);
20912097

20922098
assert(ArgSize >= F->getWrittenArgSize());

clang/test/AST/Interp/functions.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,12 @@ namespace FunctionPointers {
123123
}
124124

125125
constexpr int applyBinOp(int a, int b, int (*op)(int, int)) {
126-
return op(a, b);
126+
return op(a, b); // both-note {{evaluates to a null function pointer}}
127127
}
128128
static_assert(applyBinOp(1, 2, add) == 3, "");
129+
static_assert(applyBinOp(1, 2, nullptr) == 3, ""); // both-error {{not an integral constant expression}} \
130+
// both-note {{in call to}}
131+
129132

130133
constexpr int ignoreReturnValue() {
131134
int (*foo)(int, int) = add;

0 commit comments

Comments
 (0)