From 20690528fbd608fbe8fb7bcdb36ec6983a67413a Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Tue, 26 Sep 2017 15:47:52 +0200 Subject: [PATCH] feat(c-wrapper): getCppPtrFromC is more secure, check types --- include/linphone/utils/general.h | 2 +- src/c-wrapper/c-wrapper.h | 3 ++- src/c-wrapper/internal/c-tools.h | 28 +++++++++++++++++++++++----- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/include/linphone/utils/general.h b/include/linphone/utils/general.h index 4ba9d8dc9..aabcf1ab4 100644 --- a/include/linphone/utils/general.h +++ b/include/linphone/utils/general.h @@ -74,7 +74,7 @@ void l_assert (const char *condition, const char *file, int line); // Allows access to private internal data. // Gives a control to C Wrapper. #define L_DECLARE_PRIVATE(CLASS) \ - inline CLASS ## Private * getPrivate() { \ + inline CLASS ## Private *getPrivate() { \ return reinterpret_cast(mPrivate); \ } \ inline const CLASS ## Private *getPrivate() const { \ diff --git a/src/c-wrapper/c-wrapper.h b/src/c-wrapper/c-wrapper.h index fde36fed1..fed96f8df 100644 --- a/src/c-wrapper/c-wrapper.h +++ b/src/c-wrapper/c-wrapper.h @@ -41,7 +41,8 @@ F(Participant, Participant) #define L_REGISTER_SUBTYPES(F) \ - F(ChatRoom, ClientGroupChatRoom) + F(ChatRoom, ClientGroupChatRoom) \ + F(ChatRoom, RealTimeTextChatRoom) // ============================================================================= // Register belle-sip ID. diff --git a/src/c-wrapper/internal/c-tools.h b/src/c-wrapper/internal/c-tools.h index 3316a93fc..788d090b3 100644 --- a/src/c-wrapper/internal/c-tools.h +++ b/src/c-wrapper/internal/c-tools.h @@ -37,11 +37,13 @@ LINPHONE_BEGIN_NAMESPACE template struct CppTypeToCType { enum { defined = false }; + typedef void type; }; template struct CTypeToCppType { enum { defined = false }; + typedef void type; }; template @@ -51,7 +53,23 @@ class Wrapper { private: template struct IsCppObject { - enum { value = std::is_base_of::value || std::is_base_of::value }; + enum { + value = std::is_base_of::value || std::is_base_of::value + }; + }; + + template + struct IsDefinedNotClonableCppObject { + enum { + value = CppTypeToCType::defined && std::is_base_of::value + }; + }; + + template + struct IsDefinedClonableCppObject { + enum { + value = CppTypeToCType::defined && std::is_base_of::value + }; }; template @@ -86,7 +104,7 @@ public: template< typename CType, typename CppType = typename CTypeToCppType::type, - typename = typename std::enable_if::value, CppType>::type + typename = typename std::enable_if::value, CppType>::type > static constexpr std::shared_ptr getCppPtrFromC (CType *cObject) { return reinterpret_cast *>(cObject)->cppPtr; @@ -95,7 +113,7 @@ public: template< typename CType, typename CppType = typename CTypeToCppType::type, - typename = typename std::enable_if::value, CppType>::type + typename = typename std::enable_if::value, CppType>::type > static constexpr std::shared_ptr getCppPtrFromC (const CType *cObject) { return reinterpret_cast *>(cObject)->cppPtr; @@ -104,7 +122,7 @@ public: template< typename CType, typename CppType = typename CTypeToCppType::type, - typename = typename std::enable_if::value, CppType>::type + typename = typename std::enable_if::value, CppType>::type > static constexpr CppType *getCppPtrFromC (CType *cObject) { return reinterpret_cast *>(cObject)->cppPtr; @@ -113,7 +131,7 @@ public: template< typename CType, typename CppType = typename CTypeToCppType::type, - typename = typename std::enable_if::value, CppType>::type + typename = typename std::enable_if::value, CppType>::type > static constexpr const CppType *getCppPtrFromC (const CType *cObject) { return reinterpret_cast *>(cObject)->cppPtr;