[clang] b200dfc - [clang][Interp] Fix calling invalid function pointers

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 14 22:24:59 PST 2024


Author: Timm Bäder
Date: 2024-02-15T07:23:35+01:00
New Revision: b200dfc15904f0f7f19443fd5a399242c80213dc

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

LOG: [clang][Interp] Fix calling invalid function pointers

Checking for isConstexpr() is wrong; we need to (try to) call
the function and let later code diagnose the failure accordingly.

Added: 
    

Modified: 
    clang/lib/AST/Interp/Interp.h
    clang/test/AST/Interp/functions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 77c724f08e8eef..5bbb9f169a800e 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -2071,8 +2071,7 @@ inline bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize) {
   const FunctionPointer &FuncPtr = S.Stk.pop<FunctionPointer>();
 
   const Function *F = FuncPtr.getFunction();
-  if (!F || !F->isConstexpr())
-    return false;
+  assert(F);
 
   assert(ArgSize >= F->getWrittenArgSize());
   uint32_t VarArgSize = ArgSize - F->getWrittenArgSize();

diff  --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp
index 7b8278cf13aa88..34a832c794c75d 100644
--- a/clang/test/AST/Interp/functions.cpp
+++ b/clang/test/AST/Interp/functions.cpp
@@ -187,6 +187,14 @@ namespace FunctionReturnType {
   static_assert(!!op, "");
   constexpr int (*op2)(int, int) = nullptr;
   static_assert(!op2, "");
+
+  int m() { return 5;} // ref-note {{declared here}} \
+                       // expected-note {{declared here}}
+  constexpr int (*invalidFnPtr)() = m;
+  static_assert(invalidFnPtr() == 5, ""); // ref-error {{not an integral constant expression}} \
+                                 // ref-note {{non-constexpr function 'm'}} \
+                                 // expected-error {{not an integral constant expression}} \
+                                 // expected-note {{non-constexpr function 'm'}}
 }
 
 namespace Comparison {


        


More information about the cfe-commits mailing list