Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
Disallow conversions from function pointers to void*.
Browse files Browse the repository at this point in the history
Function pointers and member function pointers cannot be converted to void*.
libc++abi incorrectly allows this conversion for function pointers.

Review URL: http://reviews.llvm.org/D8811


git-svn-id: https://llvm.org/svn/llvm-project/libcxxabi/trunk@236299 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
EricWF committed May 1, 2015
1 parent 3641403 commit b979db1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/private_typeinfo.cpp
Expand Up @@ -387,9 +387,13 @@ __pointer_type_info::can_catch(const __shim_type_info* thrown_type,
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);
Expand Down
16 changes: 16 additions & 0 deletions test/catch_function_01.pass.cpp
Expand Up @@ -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 (*)()
Expand All @@ -25,7 +33,15 @@ int main()
{
assert(false);
}
catch (void*) // can't catch as void*
{
assert(false);
}
catch(Function*)
{
}
catch (...)
{
assert(false);
}
}

0 comments on commit b979db1

Please sign in to comment.