From 56988bee343dbcfe1cc4ea3f20a790f808ac4407 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Wed, 7 Aug 2019 13:17:20 -0700 Subject: [PATCH] No free when construct was not done (#6483) * No free when construct was not done This fix the segfault when some other error happens * Add more tests * Use Sub to avoid printing too much --- php/ext/google/protobuf/message.c | 10 ++++++---- php/tests/generated_class_test.php | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/php/ext/google/protobuf/message.c b/php/ext/google/protobuf/message.c index 43561551bde1..8197dbd12256 100644 --- a/php/ext/google/protobuf/message.c +++ b/php/ext/google/protobuf/message.c @@ -89,11 +89,13 @@ static HashTable* message_get_properties(zval* object TSRMLS_DC); // Define object free method. PHP_PROTO_OBJECT_FREE_START(MessageHeader, message) - if (*(void**)intern->data != NULL) { - stringsink_uninit_opaque(*(void**)intern->data); - FREE(*(void**)intern->data); + if (intern->data) { + if (*(void**)intern->data != NULL) { + stringsink_uninit_opaque(*(void**)intern->data); + FREE(*(void**)intern->data); + } + FREE(intern->data); } - FREE(intern->data); PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_START(MessageHeader, message) diff --git a/php/tests/generated_class_test.php b/php/tests/generated_class_test.php index a9ad793be315..a54c4e5b766f 100644 --- a/php/tests/generated_class_test.php +++ b/php/tests/generated_class_test.php @@ -1504,4 +1504,27 @@ public function testValueIsReference() $m = new TestMessage(); $m->setOptionalString($values[0]); } + + ######################################################### + # Test no segfault when error happens + ######################################################### + + function throwIntendedException() + { + throw new Exception('Intended'); + } + + /** + * @expectedException Exception + */ + public function testNoSegfaultWithError() + { + new TestMessage(['optional_int32' => $this->throwIntendedException()]); + } + + public function testNoExceptionWithVarDump() + { + $m = new Sub(['a' => 1]); + var_dump($m); + } }