From 252870d4a85f942107e7b51f9bdf5fae4e88f728 Mon Sep 17 00:00:00 2001 From: Martijn Otto Date: Fri, 20 May 2016 13:38:56 +0200 Subject: Fix exception catching and properly handle casting functions returning something other than a Php::Value --- include/class.h | 8 ++++---- include/exception.h | 4 ++-- zend/opcodes.h | 3 +-- zend/origexception.h | 11 +++++------ zend/value.cpp | 5 ++--- 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/include/class.h b/include/class.h index 371595e..26cc610 100644 --- a/include/class.h +++ b/include/class.h @@ -462,7 +462,7 @@ private: T *obj = (T *)base; // retrieve the casted value and convert it if necessary - auto result = obj->__toString(); + Value result{ obj->__toString() }; result.setType(Type::String); // return the converted result @@ -480,7 +480,7 @@ private: T *obj = (T *)base; // retrieve the casted value and convert it if necessary - auto result = obj->__toInteger(); + Value result{ obj->__toInteger() }; result.setType(Type::Numeric); // return the converted result @@ -498,7 +498,7 @@ private: T *obj = (T *)base; // retrieve the casted value and convert it if necessary - auto result = obj->__toFloat(); + Value result{ obj->__toFloat() }; result.setType(Type::Float); // return the converted result @@ -516,7 +516,7 @@ private: T *obj = (T *)base; // retrieve the casted value and convert it if necessary - auto result = obj->__toBool(); + Value result{ obj->__toBool() }; result.setType(Type::Bool); // return the converted result diff --git a/include/exception.h b/include/exception.h index 55601c2..235529f 100644 --- a/include/exception.h +++ b/include/exception.h @@ -41,12 +41,12 @@ public: * Constructor * @param &string */ - Exception(const std::string &message, int code = 0) : std::exception(), _message(message), _code(code) {} + Exception(std::string message, int code = 0) : std::exception(), _message(std::move(message)), _code(code) {} /** * Destructor */ - virtual ~Exception() throw() {} + virtual ~Exception() = default; /** * Overridden what method diff --git a/zend/opcodes.h b/zend/opcodes.h index 1901f1f..0a79b97 100644 --- a/zend/opcodes.h +++ b/zend/opcodes.h @@ -93,8 +93,7 @@ public: // was an exception thrown inside the eval()'ed code? In that case we // throw a C++ new exception to give the C++ code the chance to catch it - // todo: OrigException with constructor for zend_object - // if (oldException != EG(exception) && EG(exception)) throw OrigException(EG(exception) TSRMLS_CC); + if (oldException != EG(exception) && EG(exception)) throw OrigException(EG(exception) TSRMLS_CC); // we're ready if there is no return value if (ZVAL_IS_NULL(&retval)) return nullptr; diff --git a/zend/origexception.h b/zend/origexception.h index f997608..fb873bd 100644 --- a/zend/origexception.h +++ b/zend/origexception.h @@ -16,7 +16,7 @@ namespace Php { /** * Class definition */ -class OrigException : public Value, public Exception +class OrigException : public Exception { private: /** @@ -42,10 +42,9 @@ private: public: /** * Constructor - * @param val + * @param object The object that was thrown */ - OrigException(zval *val TSRMLS_DC) : - Value(val), Exception("OrigException") + OrigException(zend_object *object) : Exception(std::string{ ZSTR_VAL(object->ce->name), ZSTR_LEN(object->ce->name) }) { #ifdef ZTS // copy tsrm_ls @@ -58,7 +57,7 @@ public: * @param exception */ OrigException(const OrigException &exception) : - Value(exception), Exception("OrigException"), _handled(exception._handled) + Exception("OrigException"), _handled(exception._handled) { #ifdef ZTS // copy tsrm_ls @@ -71,7 +70,7 @@ public: * @param exception */ OrigException(OrigException &&exception) : - Value(std::move(exception)), Exception("OrigException"), _handled(exception._handled) + Exception("OrigException"), _handled(exception._handled) { // set other exception to handled so that it wont do anything on destruction exception._handled = true; diff --git a/zend/value.cpp b/zend/value.cpp index 4aaec4c..d87e576 100644 --- a/zend/value.cpp +++ b/zend/value.cpp @@ -846,7 +846,7 @@ static Value do_exec(const zval *object, zval *method, int argc, zval *argv) TSRMLS_FETCH(); // the current exception - // zend_object *oldException = EG(exception); + zend_object *oldException = EG(exception); // call the function // we're casting the const away here, object is only const so we can call this method @@ -863,8 +863,7 @@ static Value do_exec(const zval *object, zval *method, int argc, zval *argv) { // was an exception thrown inside the function? In that case we throw a C++ new exception // to give the C++ code the chance to catch it - // @todo: make OrigException except a zend_object instance - // if (oldException != EG(exception) && EG(exception)) throw OrigException(EG(exception) TSRMLS_CC); + if (oldException != EG(exception) && EG(exception)) throw OrigException(EG(exception) TSRMLS_CC); // leap out if nothing was returned if (Z_ISUNDEF(retval)) return nullptr; -- cgit v1.2.3