From a14515a3646cffc905915776f502ba7dd2d8c4c0 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 10 Sep 2016 18:43:22 +0300 Subject: [PATCH] Add negative pointer cast tests. --- include/boost/pointer_cast.hpp | 38 ++++++++++++++++++++-------------- test/Jamfile.v2 | 12 +++++++++++ test/pointer_cast_co_fail.cpp | 18 ++++++++++++++++ test/pointer_cast_co_fail2.cpp | 18 ++++++++++++++++ test/pointer_cast_co_fail3.cpp | 29 ++++++++++++++++++++++++++ test/pointer_cast_dy_fail.cpp | 25 ++++++++++++++++++++++ test/pointer_cast_dy_fail2.cpp | 25 ++++++++++++++++++++++ test/pointer_cast_dy_fail3.cpp | 29 ++++++++++++++++++++++++++ test/pointer_cast_st_fail.cpp | 18 ++++++++++++++++ test/pointer_cast_st_fail2.cpp | 18 ++++++++++++++++ test/pointer_cast_st_fail3.cpp | 29 ++++++++++++++++++++++++++ 11 files changed, 244 insertions(+), 15 deletions(-) create mode 100644 test/pointer_cast_co_fail.cpp create mode 100644 test/pointer_cast_co_fail2.cpp create mode 100644 test/pointer_cast_co_fail3.cpp create mode 100644 test/pointer_cast_dy_fail.cpp create mode 100644 test/pointer_cast_dy_fail2.cpp create mode 100644 test/pointer_cast_dy_fail3.cpp create mode 100644 test/pointer_cast_st_fail.cpp create mode 100644 test/pointer_cast_st_fail2.cpp create mode 100644 test/pointer_cast_st_fail3.cpp diff --git a/include/boost/pointer_cast.hpp b/include/boost/pointer_cast.hpp index e5cd41f..44e67e4 100644 --- a/include/boost/pointer_cast.hpp +++ b/include/boost/pointer_cast.hpp @@ -79,48 +79,56 @@ using std::const_pointer_cast; //reinterpret_pointer_cast overload for std::shared_ptr template std::shared_ptr reinterpret_pointer_cast(const std::shared_ptr & r ) BOOST_NOEXCEPT { - (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) ); + (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) ); - typedef typename std::shared_ptr::element_type E; + typedef typename std::shared_ptr::element_type E; - E * p = reinterpret_cast< E* >( r.get() ); - return std::shared_ptr( r, p ); + E * p = reinterpret_cast< E* >( r.get() ); + return std::shared_ptr( r, p ); } //static_pointer_cast overload for std::unique_ptr template std::unique_ptr static_pointer_cast( std::unique_ptr && r ) BOOST_NOEXCEPT { - typedef typename std::unique_ptr::element_type E; + (void) static_cast< T* >( static_cast< U* >( 0 ) ); - detail::assert_safe_moving_upcast(); + typedef typename std::unique_ptr::element_type E; - return std::unique_ptr( static_cast( r.release() ) ); + detail::assert_safe_moving_upcast(); + + return std::unique_ptr( static_cast( r.release() ) ); } //dynamic_pointer_cast overload for std::unique_ptr template std::unique_ptr dynamic_pointer_cast( std::unique_ptr && r ) BOOST_NOEXCEPT { - detail::assert_safe_moving_upcast(); + (void) dynamic_cast< T* >( static_cast< U* >( 0 ) ); - T * p = dynamic_cast( r.get() ); - if( p ) r.release(); - return std::unique_ptr( p ); + detail::assert_safe_moving_upcast(); + + T * p = dynamic_cast( r.get() ); + if( p ) r.release(); + return std::unique_ptr( p ); } //const_pointer_cast overload for std::unique_ptr template std::unique_ptr const_pointer_cast( std::unique_ptr && r ) BOOST_NOEXCEPT { - typedef typename std::unique_ptr::element_type E; + (void) const_cast< T* >( static_cast< U* >( 0 ) ); - return std::unique_ptr( const_cast( r.release() ) ); + typedef typename std::unique_ptr::element_type E; + + return std::unique_ptr( const_cast( r.release() ) ); } //reinterpret_pointer_cast overload for std::unique_ptr template std::unique_ptr reinterpret_pointer_cast( std::unique_ptr && r ) BOOST_NOEXCEPT { - typedef typename std::unique_ptr::element_type E; + (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) ); - return std::unique_ptr( reinterpret_cast( r.release() ) ); + typedef typename std::unique_ptr::element_type E; + + return std::unique_ptr( reinterpret_cast( r.release() ) ); } } // namespace boost diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 596e167..bf38dae 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -189,5 +189,17 @@ import testing ; [ run pointer_cast_test2.cpp ] [ run pointer_cast_test3.cpp ] + + [ compile-fail pointer_cast_st_fail.cpp ] + [ compile-fail pointer_cast_st_fail2.cpp ] + [ compile-fail pointer_cast_st_fail3.cpp ] + + [ compile-fail pointer_cast_co_fail.cpp ] + [ compile-fail pointer_cast_co_fail2.cpp ] + [ compile-fail pointer_cast_co_fail3.cpp ] + + [ compile-fail pointer_cast_dy_fail.cpp ] + [ compile-fail pointer_cast_dy_fail2.cpp ] + [ compile-fail pointer_cast_dy_fail3.cpp ] ; } diff --git a/test/pointer_cast_co_fail.cpp b/test/pointer_cast_co_fail.cpp new file mode 100644 index 0000000..d8960b2 --- /dev/null +++ b/test/pointer_cast_co_fail.cpp @@ -0,0 +1,18 @@ +// +// A negative test for unique_ptr const_cast +// +// Copyright 2016 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// + +#include +#include + +int main() +{ + std::unique_ptr p1( new int ); + std::unique_ptr p2 = boost::const_pointer_cast( std::move( p1 ) ); +} diff --git a/test/pointer_cast_co_fail2.cpp b/test/pointer_cast_co_fail2.cpp new file mode 100644 index 0000000..7fa8e18 --- /dev/null +++ b/test/pointer_cast_co_fail2.cpp @@ -0,0 +1,18 @@ +// +// A negative test for unique_ptr const_cast +// +// Copyright 2016 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// + +#include +#include + +int main() +{ + std::unique_ptr p1( new int[ 1 ] ); + std::unique_ptr p2 = boost::const_pointer_cast( std::move( p1 ) ); +} diff --git a/test/pointer_cast_co_fail3.cpp b/test/pointer_cast_co_fail3.cpp new file mode 100644 index 0000000..d8872c8 --- /dev/null +++ b/test/pointer_cast_co_fail3.cpp @@ -0,0 +1,29 @@ +// +// A negative test for unique_ptr const_cast +// +// Copyright 2016 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// + +#include +#include + +struct B +{ + virtual ~B() + { + } +}; + +struct D: B +{ +}; + +int main() +{ + std::unique_ptr p1( new D[ 1 ] ); + std::unique_ptr p2 = boost::const_pointer_cast( std::move( p1 ) ); +} diff --git a/test/pointer_cast_dy_fail.cpp b/test/pointer_cast_dy_fail.cpp new file mode 100644 index 0000000..c6df69d --- /dev/null +++ b/test/pointer_cast_dy_fail.cpp @@ -0,0 +1,25 @@ +// +// A negative test for unique_ptr dynamic_cast +// +// Copyright 2016 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// + +#include +#include + +struct B +{ + virtual ~B() + { + } +}; + +int main() +{ + std::unique_ptr p1( new B ); + std::unique_ptr p2 = boost::dynamic_pointer_cast( std::move( p1 ) ); +} diff --git a/test/pointer_cast_dy_fail2.cpp b/test/pointer_cast_dy_fail2.cpp new file mode 100644 index 0000000..223977a --- /dev/null +++ b/test/pointer_cast_dy_fail2.cpp @@ -0,0 +1,25 @@ +// +// A negative test for unique_ptr dynamic_cast +// +// Copyright 2016 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// + +#include +#include + +struct B +{ + virtual ~B() + { + } +}; + +int main() +{ + std::unique_ptr p1( new B[ 1 ] ); + std::unique_ptr p2 = boost::dynamic_pointer_cast( std::move( p1 ) ); +} diff --git a/test/pointer_cast_dy_fail3.cpp b/test/pointer_cast_dy_fail3.cpp new file mode 100644 index 0000000..1ac52e7 --- /dev/null +++ b/test/pointer_cast_dy_fail3.cpp @@ -0,0 +1,29 @@ +// +// A negative test for unique_ptr dynamic_cast +// +// Copyright 2016 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// + +#include +#include + +struct B +{ + virtual ~B() + { + } +}; + +struct D: B +{ +}; + +int main() +{ + std::unique_ptr p1( new D[ 1 ] ); + std::unique_ptr p2 = boost::dynamic_pointer_cast( std::move( p1 ) ); +} diff --git a/test/pointer_cast_st_fail.cpp b/test/pointer_cast_st_fail.cpp new file mode 100644 index 0000000..0fcf8c1 --- /dev/null +++ b/test/pointer_cast_st_fail.cpp @@ -0,0 +1,18 @@ +// +// A negative test for unique_ptr static_cast +// +// Copyright 2016 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// + +#include +#include + +int main() +{ + std::unique_ptr p1( new int ); + std::unique_ptr p2 = boost::static_pointer_cast( std::move( p1 ) ); +} diff --git a/test/pointer_cast_st_fail2.cpp b/test/pointer_cast_st_fail2.cpp new file mode 100644 index 0000000..3b227b9 --- /dev/null +++ b/test/pointer_cast_st_fail2.cpp @@ -0,0 +1,18 @@ +// +// A negative test for unique_ptr static_cast +// +// Copyright 2016 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// + +#include +#include + +int main() +{ + std::unique_ptr p1( new int[ 1 ] ); + std::unique_ptr p2 = boost::static_pointer_cast( std::move( p1 ) ); +} diff --git a/test/pointer_cast_st_fail3.cpp b/test/pointer_cast_st_fail3.cpp new file mode 100644 index 0000000..4ca6a89 --- /dev/null +++ b/test/pointer_cast_st_fail3.cpp @@ -0,0 +1,29 @@ +// +// A negative test for unique_ptr static_cast +// +// Copyright 2016 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// + +#include +#include + +struct B +{ + virtual ~B() + { + } +}; + +struct D: B +{ +}; + +int main() +{ + std::unique_ptr p1( new D[ 1 ] ); + std::unique_ptr p2 = boost::static_pointer_cast( std::move( p1 ) ); +}