Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Segmentation Faults in Phalcon\Validation\Message\Group::offsetSet() and appendMessage() #742

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions ext/validation/message/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,16 @@ PHP_METHOD(Phalcon_Validation_Message_Group, offsetSet){

zval *index, *message;

phalcon_fetch_params(0, 2, 0, &index, &message);
PHALCON_MM_GROW();

phalcon_fetch_params(1, 2, 0, &index, &message);

if (Z_TYPE_P(message) != IS_OBJECT) {
PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "The message must be an object");
if (Z_TYPE_P(message) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(message), phalcon_validation_message_ce TSRMLS_CC)) {
PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "The message must be an instance of Phalcon\\Validation\\Message class");
return;
}
phalcon_update_property_array(this_ptr, SL("_messages"), index, message TSRMLS_CC);

PHALCON_MM_RESTORE();
}

/**
Expand Down Expand Up @@ -189,20 +191,22 @@ PHP_METHOD(Phalcon_Validation_Message_Group, offsetUnset){
* $messages->appendMessage(new Phalcon\Validation\Message('This is a message'));
*</code>
*
* @param Phalcon\Validation\MessageInterface $message
* @param Phalcon\Validation\Message $message
*/
PHP_METHOD(Phalcon_Validation_Message_Group, appendMessage){

zval *message;

phalcon_fetch_params(0, 1, 0, &message);
PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 0, &message);

if (Z_TYPE_P(message) != IS_OBJECT) {
PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "The message must be an object");
if (Z_TYPE_P(message) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(message), phalcon_validation_message_ce TSRMLS_CC)) {
PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "The message must be an instance of Phalcon\\Validation\\Message class");
return;
}
phalcon_update_property_array_append(this_ptr, SL("_messages"), message TSRMLS_CC);

PHALCON_MM_RESTORE();
}

/**
Expand All @@ -224,11 +228,9 @@ PHP_METHOD(Phalcon_Validation_Message_Group, appendMessages){

phalcon_fetch_params(1, 1, 0, &messages);

if (Z_TYPE_P(messages) != IS_ARRAY) {
if (Z_TYPE_P(messages) != IS_OBJECT) {
PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "The messages must be array or object");
return;
}
if (Z_TYPE_P(messages) != IS_ARRAY && Z_TYPE_P(messages) != IS_OBJECT) {
PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "The messages must be array or object");
return;
}

PHALCON_OBS_VAR(current_messages);
Expand Down
21 changes: 21 additions & 0 deletions unit-tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -841,4 +841,25 @@ public function testValidationFiltering()
$_POST = array();
}


public function testIssue441()
{
$group = new Phalcon\Validation\Message\Group;
try {
$group[0] = 'invalid';
$this->assertTrue(false);
}
catch (Exception $e) {
$this->assertTrue(true);
}

try {
$group->appendMessage('invalid');
$this->assertTrue(false);
}
catch (Exception $e) {
$this->assertTrue(true);
}
}

}