Skip to content

Commit

Permalink
No free when construct was not done (#6483)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
TeBoring committed Aug 7, 2019
1 parent d2d6ff5 commit 56988be
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
10 changes: 6 additions & 4 deletions php/ext/google/protobuf/message.c
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions php/tests/generated_class_test.php
Expand Up @@ -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);
}
}

0 comments on commit 56988be

Please sign in to comment.