From c54e7c974beb59153ad90234e1ea7a9bdc2b2dc8 Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Tue, 2 Apr 2019 10:29:01 +0200 Subject: [PATCH 1/3] Fix AppleClang -Wnull-pointer-arithmetic warning --- STL_Extension/include/CGAL/Compact_container.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STL_Extension/include/CGAL/Compact_container.h b/STL_Extension/include/CGAL/Compact_container.h index b4960af00c4..c1927dd9c3f 100644 --- a/STL_Extension/include/CGAL/Compact_container.h +++ b/STL_Extension/include/CGAL/Compact_container.h @@ -815,7 +815,7 @@ private: static char * clean_pointer(char * p) { - return ((p - (char *) NULL) & ~ (std::ptrdiff_t) START_END) + (char *) NULL; + return reinterpret_cast((p - (char *) NULL) & ~ (std::ptrdiff_t) START_END); } // Returns the pointee, cleaned up from the squatted bits. From 68f6ebf42d74e99c0397803825d7909bbcd4ed42 Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Wed, 3 Apr 2019 14:52:12 +0200 Subject: [PATCH 2/3] More reinterpret_cast to avoid undefined behavior --- STL_Extension/include/CGAL/Compact_container.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/STL_Extension/include/CGAL/Compact_container.h b/STL_Extension/include/CGAL/Compact_container.h index c1927dd9c3f..1b7ea962e78 100644 --- a/STL_Extension/include/CGAL/Compact_container.h +++ b/STL_Extension/include/CGAL/Compact_container.h @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -815,7 +816,8 @@ private: static char * clean_pointer(char * p) { - return reinterpret_cast((p - (char *) NULL) & ~ (std::ptrdiff_t) START_END); + return reinterpret_cast(reinterpret_cast(p) & + ~ (std::uintptr_t) START_END); } // Returns the pointee, cleaned up from the squatted bits. @@ -828,7 +830,8 @@ private: static Type type(const_pointer ptr) { char * p = (char *) Traits::pointer(*ptr); - return (Type) (p - clean_pointer(p)); + return (Type) (reinterpret_cast(p) - + reinterpret_cast(clean_pointer(p))); } // Sets the pointer part and the type of the pointee. @@ -837,7 +840,8 @@ private: // This out of range compare is always true and causes lots of // unnecessary warnings. // CGAL_precondition(0 <= t && t < 4); - Traits::pointer(*ptr) = (void *) ((clean_pointer((char *) p)) + (int) t); + Traits::pointer(*ptr) = reinterpret_cast + (reinterpret_cast(clean_pointer((char *) p)) + (int) t); } public: From 34cf27b47e9d3218a2cdd240dea10338793049ba Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Wed, 3 Apr 2019 14:56:21 +0200 Subject: [PATCH 3/3] Use std::ptrdiff_t, because the target branch is not C++11 `std::uintptr_t` was introduced by C++11, but the target branch if CGAL-4.13-branch, does not require C++11. --- STL_Extension/include/CGAL/Compact_container.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/STL_Extension/include/CGAL/Compact_container.h b/STL_Extension/include/CGAL/Compact_container.h index 1b7ea962e78..6064a81c6d9 100644 --- a/STL_Extension/include/CGAL/Compact_container.h +++ b/STL_Extension/include/CGAL/Compact_container.h @@ -28,7 +28,7 @@ #include #include -#include +#include #include #include #include @@ -816,8 +816,8 @@ private: static char * clean_pointer(char * p) { - return reinterpret_cast(reinterpret_cast(p) & - ~ (std::uintptr_t) START_END); + return reinterpret_cast(reinterpret_cast(p) & + ~ (std::ptrdiff_t) START_END); } // Returns the pointee, cleaned up from the squatted bits. @@ -830,8 +830,8 @@ private: static Type type(const_pointer ptr) { char * p = (char *) Traits::pointer(*ptr); - return (Type) (reinterpret_cast(p) - - reinterpret_cast(clean_pointer(p))); + return (Type) (reinterpret_cast(p) - + reinterpret_cast(clean_pointer(p))); } // Sets the pointer part and the type of the pointee. @@ -841,7 +841,7 @@ private: // unnecessary warnings. // CGAL_precondition(0 <= t && t < 4); Traits::pointer(*ptr) = reinterpret_cast - (reinterpret_cast(clean_pointer((char *) p)) + (int) t); + (reinterpret_cast(clean_pointer((char *) p)) + (int) t); } public: