[PATCH] [libc++abi] Disallow conversions from function pointers to void*.

Eric Fiselier eric at efcs.ca
Thu Apr 30 11:26:36 PDT 2015


Urgent ping! I think this fix should go into 3.6.1 so I would like to land it.

I addressed @majnemer's comments in another patch. I removed the (incorrect) tests that were not related to this fix.


http://reviews.llvm.org/D8811

Files:
  src/private_typeinfo.cpp
  test/catch_function_01.pass.cpp

Index: src/private_typeinfo.cpp
===================================================================
--- src/private_typeinfo.cpp
+++ src/private_typeinfo.cpp
@@ -387,9 +387,13 @@
     if (is_equal(__pointee, thrown_pointer_type->__pointee, false))
         return true;
     // bullet 3A
-    if (is_equal(__pointee, &typeid(void), false))
-        return true;
-
+    if (is_equal(__pointee, &typeid(void), false)) {
+        // pointers to functions cannot be converted to void*.
+        // pointers to member functions are not handled here.
+        const __function_type_info* thrown_function =
+            dynamic_cast<const __function_type_info*>(thrown_pointer_type->__pointee);
+        return (thrown_function == nullptr);
+    }
     // Handle pointer to pointer
     const __pointer_type_info* nested_pointer_type =
         dynamic_cast<const __pointer_type_info*>(__pointee);
Index: test/catch_function_01.pass.cpp
===================================================================
--- test/catch_function_01.pass.cpp
+++ test/catch_function_01.pass.cpp
@@ -11,11 +11,19 @@
 
 #include <cassert>
 
+template <class Tp>
+bool can_convert(Tp) { return true; }
+
+template <class>
+bool can_convert(...) { return false; }
+
 void f() {}
 
 int main()
 {
     typedef void Function();
+    assert(!can_convert<Function&>(&f));
+    assert(!can_convert<void*>(&f));
     try
     {
         throw f;     // converts to void (*)()
@@ -25,7 +33,15 @@
     {
         assert(false);
     }
+    catch (void*) // can't catch as void*
+    {
+        assert(false);
+    }
+    catch(Function*)
+    {
+    }
     catch (...)
     {
+        assert(false);
     }
 }

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8811.24752.patch
Type: text/x-patch
Size: 1673 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150430/00bb18ed/attachment.bin>


More information about the cfe-commits mailing list