fixed a bug in RefAnyCast causing a crash if the cast is invalid

This commit is contained in:
Günter Obiltschnig
2020-02-13 21:56:59 +01:00
parent 0b59315443
commit 23da8c21c9

View File

@@ -45,8 +45,8 @@ template <typename PlaceholderT, unsigned int SizeV = POCO_SMALL_OBJECT_SIZE>
union Placeholder union Placeholder
/// ValueHolder union (used by Poco::Any and Poco::Dynamic::Var for small /// ValueHolder union (used by Poco::Any and Poco::Dynamic::Var for small
/// object optimization, when enabled). /// object optimization, when enabled).
/// ///
/// If Holder<Type> fits into POCO_SMALL_OBJECT_SIZE bytes of storage, /// If Holder<Type> fits into POCO_SMALL_OBJECT_SIZE bytes of storage,
/// it will be placement-new-allocated into the local buffer /// it will be placement-new-allocated into the local buffer
/// (i.e. there will be no heap-allocation). The local buffer size is one byte /// (i.e. there will be no heap-allocation). The local buffer size is one byte
/// larger - [POCO_SMALL_OBJECT_SIZE + 1], additional byte value indicating /// larger - [POCO_SMALL_OBJECT_SIZE + 1], additional byte value indicating
@@ -91,7 +91,7 @@ public:
private: private:
#endif #endif
typedef typename std::aligned_storage<SizeV + 1>::type AlignerType; typedef typename std::aligned_storage<SizeV + 1>::type AlignerType;
PlaceholderT* pHolder; PlaceholderT* pHolder;
mutable char holder[SizeV + 1]; mutable char holder[SizeV + 1];
AlignerType aligner; AlignerType aligner;
@@ -110,8 +110,8 @@ template <typename PlaceholderT>
union Placeholder union Placeholder
/// ValueHolder union (used by Poco::Any and Poco::Dynamic::Var for small /// ValueHolder union (used by Poco::Any and Poco::Dynamic::Var for small
/// object optimization, when enabled). /// object optimization, when enabled).
/// ///
/// If Holder<Type> fits into POCO_SMALL_OBJECT_SIZE bytes of storage, /// If Holder<Type> fits into POCO_SMALL_OBJECT_SIZE bytes of storage,
/// it will be placement-new-allocated into the local buffer /// it will be placement-new-allocated into the local buffer
/// (i.e. there will be no heap-allocation). The local buffer size is one byte /// (i.e. there will be no heap-allocation). The local buffer size is one byte
/// larger - [POCO_SMALL_OBJECT_SIZE + 1], additional byte value indicating /// larger - [POCO_SMALL_OBJECT_SIZE + 1], additional byte value indicating
@@ -131,7 +131,7 @@ public:
#if !defined(POCO_MSVC_VERSION) || (defined(POCO_MSVC_VERSION) && (POCO_MSVC_VERSION > 80)) #if !defined(POCO_MSVC_VERSION) || (defined(POCO_MSVC_VERSION) && (POCO_MSVC_VERSION > 80))
private: private:
#endif #endif
PlaceholderT* pHolder; PlaceholderT* pHolder;
friend class Any; friend class Any;
@@ -167,8 +167,8 @@ public:
Any(const ValueType & value) Any(const ValueType & value)
/// Creates an any which stores the init parameter inside. /// Creates an any which stores the init parameter inside.
/// ///
/// Example: /// Example:
/// Any a(13); /// Any a(13);
/// Any a(string("12345")); /// Any a(string("12345"));
{ {
construct(value); construct(value);
@@ -196,7 +196,7 @@ public:
Any& swap(Any& other) Any& swap(Any& other)
/// Swaps the content of the two Anys. /// Swaps the content of the two Anys.
/// ///
/// When small object optimization is enabled, swap only /// When small object optimization is enabled, swap only
/// has no-throw guarantee when both (*this and other) /// has no-throw guarantee when both (*this and other)
/// objects are allocated on the heap. /// objects are allocated on the heap.
@@ -230,14 +230,14 @@ public:
Any& operator = (const ValueType& rhs) Any& operator = (const ValueType& rhs)
/// Assignment operator for all types != Any. /// Assignment operator for all types != Any.
/// ///
/// Example: /// Example:
/// Any a = 13; /// Any a = 13;
/// Any a = string("12345"); /// Any a = string("12345");
{ {
construct(rhs); construct(rhs);
return *this; return *this;
} }
Any& operator = (const Any& rhs) Any& operator = (const Any& rhs)
/// Assignment operator for Any. /// Assignment operator for Any.
{ {
@@ -248,14 +248,14 @@ public:
return *this; return *this;
} }
bool empty() const bool empty() const
/// Returns true if the Any is empty. /// Returns true if the Any is empty.
{ {
char buf[POCO_SMALL_OBJECT_SIZE] = { 0 }; char buf[POCO_SMALL_OBJECT_SIZE] = { 0 };
return 0 == std::memcmp(_valueHolder.holder, buf, POCO_SMALL_OBJECT_SIZE); return 0 == std::memcmp(_valueHolder.holder, buf, POCO_SMALL_OBJECT_SIZE);
} }
const std::type_info & type() const const std::type_info & type() const
/// Returns the type information of the stored content. /// Returns the type information of the stored content.
/// If the Any is empty typeid(void) is returned. /// If the Any is empty typeid(void) is returned.
@@ -335,7 +335,7 @@ private:
else else
_valueHolder.erase(); _valueHolder.erase();
} }
void destruct() void destruct()
{ {
content()->~ValueHolder(); content()->~ValueHolder();
@@ -357,8 +357,8 @@ private:
_pHolder(new Holder<ValueType>(value)) _pHolder(new Holder<ValueType>(value))
/// Creates an any which stores the init parameter inside. /// Creates an any which stores the init parameter inside.
/// ///
/// Example: /// Example:
/// Any a(13); /// Any a(13);
/// Any a(string("12345")); /// Any a(string("12345"));
{ {
} }
@@ -385,8 +385,8 @@ private:
Any& operator = (const ValueType& rhs) Any& operator = (const ValueType& rhs)
/// Assignment operator for all types != Any. /// Assignment operator for all types != Any.
/// ///
/// Example: /// Example:
/// Any a = 13; /// Any a = 13;
/// Any a = string("12345"); /// Any a = string("12345");
{ {
Any(rhs).swap(*this); Any(rhs).swap(*this);
@@ -428,7 +428,7 @@ private:
template <typename ValueType> template <typename ValueType>
class Holder: public ValueHolder class Holder: public ValueHolder
{ {
public: public:
Holder(const ValueType& value): Holder(const ValueType& value):
_held(value) _held(value)
{ {
@@ -480,9 +480,9 @@ private:
template <typename ValueType> template <typename ValueType>
ValueType* AnyCast(Any* operand) ValueType* AnyCast(Any* operand)
/// AnyCast operator used to extract the ValueType from an Any*. Will return a pointer /// AnyCast operator used to extract the ValueType from an Any*. Will return a pointer
/// to the stored value. /// to the stored value.
/// ///
/// Example Usage: /// Example Usage:
/// MyType* pTmp = AnyCast<MyType*>(pAny). /// MyType* pTmp = AnyCast<MyType*>(pAny).
/// Will return NULL if the cast fails, i.e. types don't match. /// Will return NULL if the cast fails, i.e. types don't match.
{ {
@@ -495,7 +495,7 @@ ValueType* AnyCast(Any* operand)
template <typename ValueType> template <typename ValueType>
const ValueType* AnyCast(const Any* operand) const ValueType* AnyCast(const Any* operand)
/// AnyCast operator used to extract a const ValueType pointer from an const Any*. Will return a const pointer /// AnyCast operator used to extract a const ValueType pointer from an const Any*. Will return a const pointer
/// to the stored value. /// to the stored value.
/// ///
/// Example Usage: /// Example Usage:
/// const MyType* pTmp = AnyCast<MyType*>(pAny). /// const MyType* pTmp = AnyCast<MyType*>(pAny).
@@ -509,7 +509,7 @@ template <typename ValueType>
ValueType AnyCast(Any& operand) ValueType AnyCast(Any& operand)
/// AnyCast operator used to extract a copy of the ValueType from an Any&. /// AnyCast operator used to extract a copy of the ValueType from an Any&.
/// ///
/// Example Usage: /// Example Usage:
/// MyType tmp = AnyCast<MyType>(anAny). /// MyType tmp = AnyCast<MyType>(anAny).
/// Will throw a BadCastException if the cast fails. /// Will throw a BadCastException if the cast fails.
/// Do not use an AnyCast in combination with references, i.e. MyType& tmp = ... or const MyType& tmp = ... /// Do not use an AnyCast in combination with references, i.e. MyType& tmp = ... or const MyType& tmp = ...
@@ -540,7 +540,7 @@ template <typename ValueType>
ValueType AnyCast(const Any& operand) ValueType AnyCast(const Any& operand)
/// AnyCast operator used to extract a copy of the ValueType from an const Any&. /// AnyCast operator used to extract a copy of the ValueType from an const Any&.
/// ///
/// Example Usage: /// Example Usage:
/// MyType tmp = AnyCast<MyType>(anAny). /// MyType tmp = AnyCast<MyType>(anAny).
/// Will throw a BadCastException if the cast fails. /// Will throw a BadCastException if the cast fails.
/// Do not use an AnyCast in combination with references, i.e. MyType& tmp = ... or const MyType& = ... /// Do not use an AnyCast in combination with references, i.e. MyType& tmp = ... or const MyType& = ...
@@ -555,20 +555,24 @@ ValueType AnyCast(const Any& operand)
template <typename ValueType> template <typename ValueType>
const ValueType& RefAnyCast(const Any & operand) const ValueType& RefAnyCast(const Any & operand)
/// AnyCast operator used to return a const reference to the internal data. /// AnyCast operator used to return a const reference to the internal data.
/// ///
/// Example Usage: /// Example Usage:
/// const MyType& tmp = RefAnyCast<MyType>(anAny); /// const MyType& tmp = RefAnyCast<MyType>(anAny);
{ {
ValueType* result = AnyCast<ValueType>(const_cast<Any*>(&operand)); ValueType* result = AnyCast<ValueType>(const_cast<Any*>(&operand));
std::string s = "RefAnyCast: Failed to convert between Any types "; if (!result)
if (operand._pHolder)
{ {
s.append(1, '('); std::string s = "RefAnyCast: Failed to convert between Any types ";
s.append(operand._pHolder->type().name()); if (operand._pHolder)
s.append(" => "); {
s.append(typeid(ValueType).name()); s.append(1, '(');
s.append(1, ')'); s.append(operand._pHolder->type().name());
s.append(" => ");
s.append(typeid(ValueType).name());
s.append(1, ')');
}
throw BadCastException(s);
} }
return *result; return *result;
} }
@@ -578,7 +582,7 @@ template <typename ValueType>
ValueType& RefAnyCast(Any& operand) ValueType& RefAnyCast(Any& operand)
/// AnyCast operator used to return a reference to the internal data. /// AnyCast operator used to return a reference to the internal data.
/// ///
/// Example Usage: /// Example Usage:
/// MyType& tmp = RefAnyCast<MyType>(anAny); /// MyType& tmp = RefAnyCast<MyType>(anAny);
{ {
ValueType* result = AnyCast<ValueType>(&operand); ValueType* result = AnyCast<ValueType>(&operand);