From: Bryan Duxbury Date: Thu, 12 Aug 2010 15:00:41 +0000 (+0000) Subject: THRIFT-780. php: Prevent aborts due to simultaneous exceptions X-Git-Tag: 0.4.0~17 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=7c74d9bfb131ee4486a04c5405173234cfa06f28;p=common%2Fthrift.git THRIFT-780. php: Prevent aborts due to simultaneous exceptions The bug was triggered when flush() threw an exception in the PHPOutputTransport destructor. The PHPOutputTransport in thrift_protocol_write_binary() wasn't constructed inside of the try block, so exceptions thrown when it was auto-flushing in the destructor were calling terminate(). Move the transport construction inside of the try block, and add an explicit flush before the transport is destroyed (since throwing an exception from a destructor is generally a bad thing). Patch: David Reiss git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@984815 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/php/src/ext/thrift_protocol/php_thrift_protocol.cpp b/lib/php/src/ext/thrift_protocol/php_thrift_protocol.cpp index e6ba94a9..2d2dc325 100644 --- a/lib/php/src/ext/thrift_protocol/php_thrift_protocol.cpp +++ b/lib/php/src/ext/thrift_protocol/php_thrift_protocol.cpp @@ -152,7 +152,6 @@ public: ~PHPOutputTransport() { flush(); - directFlush(); } void write(const char* data, size_t len) { @@ -203,6 +202,7 @@ public: buffer_ptr = buffer; buffer_used = 0; } + directFlush(); } protected: @@ -901,19 +901,19 @@ PHP_FUNCTION(thrift_protocol_write_binary) { RETURN_NULL(); } - PHPOutputTransport transport(*args[0]); - const char* method_name = Z_STRVAL_PP(args[1]); - convert_to_long(*args[2]); - int32_t msgtype = Z_LVAL_PP(args[2]); - zval* request_struct = *args[3]; - convert_to_long(*args[4]); - int32_t seqID = Z_LVAL_PP(args[4]); - convert_to_boolean(*args[5]); - bool strictWrite = Z_BVAL_PP(args[5]); - efree(args); - args = NULL; - try { + PHPOutputTransport transport(*args[0]); + const char* method_name = Z_STRVAL_PP(args[1]); + convert_to_long(*args[2]); + int32_t msgtype = Z_LVAL_PP(args[2]); + zval* request_struct = *args[3]; + convert_to_long(*args[4]); + int32_t seqID = Z_LVAL_PP(args[4]); + convert_to_boolean(*args[5]); + bool strictWrite = Z_BVAL_PP(args[5]); + efree(args); + args = NULL; + if (strictWrite) { int32_t version = VERSION_1 | msgtype; transport.writeI32(version); @@ -930,6 +930,7 @@ PHP_FUNCTION(thrift_protocol_write_binary) { throw_tprotocolexception("Attempt to send non-Thrift object", INVALID_DATA); } binary_serialize_spec(request_struct, transport, Z_ARRVAL_P(spec)); + transport.flush(); } catch (const PHPExceptionWrapper& ex) { zend_throw_exception_object(ex TSRMLS_CC); RETURN_NULL(); @@ -962,14 +963,14 @@ PHP_FUNCTION(thrift_protocol_read_binary) { RETURN_NULL(); } - PHPInputTransport transport(*args[0]); - char* obj_typename = Z_STRVAL_PP(args[1]); - convert_to_boolean(*args[2]); - bool strict_read = Z_BVAL_PP(args[2]); - efree(args); - args = NULL; - try { + PHPInputTransport transport(*args[0]); + char* obj_typename = Z_STRVAL_PP(args[1]); + convert_to_boolean(*args[2]); + bool strict_read = Z_BVAL_PP(args[2]); + efree(args); + args = NULL; + int8_t messageType = 0; int32_t sz = transport.readI32();