Skip to content

Commit a831c54

Browse files
committed
[clang][Interp] Avoid calling invalid functions
Check if the non-null function pointer is even valid before calling the function.
1 parent 31424be commit a831c54

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

clang/lib/AST/Interp/FunctionPointer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class FunctionPointer final {
3232

3333
const Function *getFunction() const { return Func; }
3434
bool isZero() const { return !Func; }
35+
bool isValid() const { return Valid; }
3536
bool isWeak() const {
3637
if (!Func || !Valid)
3738
return false;

clang/lib/AST/Interp/Interp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,10 @@ inline bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
22362236
<< const_cast<Expr *>(E) << E->getSourceRange();
22372237
return false;
22382238
}
2239+
2240+
if (!FuncPtr.isValid())
2241+
return false;
2242+
22392243
assert(F);
22402244

22412245
// Check argument nullability state.

clang/test/AST/Interp/functions.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,20 @@ namespace VariadicOperator {
584584
namespace WeakCompare {
585585
[[gnu::weak]]void weak_method();
586586
static_assert(weak_method != nullptr, ""); // both-error {{not an integral constant expression}} \
587-
// both-note {{comparison against address of weak declaration '&weak_method' can only be performed at runtim}}
587+
// both-note {{comparison against address of weak declaration '&weak_method' can only be performed at runtim}}
588588

589589
constexpr auto A = &weak_method;
590590
static_assert(A != nullptr, ""); // both-error {{not an integral constant expression}} \
591-
// both-note {{comparison against address of weak declaration '&weak_method' can only be performed at runtim}}
591+
// both-note {{comparison against address of weak declaration '&weak_method' can only be performed at runtim}}
592+
}
593+
594+
namespace FromIntegral {
595+
#if __cplusplus >= 202002L
596+
typedef double (*DoubleFn)();
597+
int a[(int)DoubleFn((void*)-1)()]; // both-error {{not allowed at file scope}} \
598+
// both-warning {{variable length arrays}}
599+
int b[(int)DoubleFn((void*)(-1 + 1))()]; // both-error {{not allowed at file scope}} \
600+
// expected-note {{evaluates to a null function pointer}} \
601+
// both-warning {{variable length arrays}}
602+
#endif
592603
}

0 commit comments

Comments
 (0)