Skip to content

Commit

Permalink
Fix zephir-lang#1885: Check return value before creating dynamic class.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidofferman committed Jul 16, 2019
1 parent 878faa4 commit 9a3ec64
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Added initial bash completion support (see zephir-autocomplete file)

### Fixed
- Fixed segfault when auto-loading class with syntax error
[#1885](https://github.com/phalcon/zephir/issues/1885)

## [0.12.0] - 2019-06-20
### Added
- Added initial support of "use" keyword in closures
Expand Down
6 changes: 6 additions & 0 deletions Library/Operators/Other/NewInstanceOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ public function compile(array $expression, CompilationContext $compilationContex
$classNameToFetch = 'Z_STRVAL_P('.$safeSymbol.'), Z_STRLEN_P('.$safeSymbol.')';
$zendClassEntry = $compilationContext->cacheManager->getClassEntryCache()->get($classNameToFetch, true, $compilationContext);
$classEntry = $zendClassEntry->getName();

$compilationContext->codePrinter->output('if(!'.$classEntry.') {');
$compilationContext->codePrinter->increaseLevel();
$compilationContext->codePrinter->output('RETURN_MM_NULL();');
$compilationContext->codePrinter->decreaseLevel();
$compilationContext->codePrinter->output('}');
} else {
if (!class_exists($className, false)) {
$compilationContext->logger->warning(
Expand Down
5 changes: 5 additions & 0 deletions test/operator.zep
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ class Operator
varFalse !== var1
];
}

public function testNewInstanceOperator(className)
{
return new {className}();
}
}
55 changes: 55 additions & 0 deletions unit-tests/Extension/NewInstanceOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Zephir.
*
* (c) Zephir Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Extension;

use PHPUnit\Framework\TestCase;

class NewInstanceOperatorTest extends TestCase
{
protected $autoloadMap = [
'Fixture\ParseErrorClass' => ZEPHIRPATH.'/unit-tests/fixtures/class-parse-error.php',
'Fixture\EmptyClass' => ZEPHIRPATH.'/unit-tests/fixtures/class-empty.php'
];

public function setUp()
{
spl_autoload_register([$this, 'autoload']);
}

public function tearDown()
{
spl_autoload_unregister([$this, 'autoload']);
}

public function autoload($className)
{
if (isset($this->autoloadMap[$className])) {
include $this->autoloadMap[$className];
}
}

public function testThrowableException()
{
$this->expectException(\ParseError::class);

$t = new \Test\Operator();
$obj = $t->testNewInstanceOperator('Fixture\ParseErrorClass');
}

public function testNewInstance()
{
$t = new \Test\Operator();
$object = $t->testNewInstanceOperator('Fixture\EmptyClass');

$this->assertInstanceOf('Fixture\EmptyClass', $object);
}
}
16 changes: 16 additions & 0 deletions unit-tests/fixtures/class-empty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* This file is part of the Zephir.
*
* (c) Zephir Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fixture;

class EmptyClass
{
}
20 changes: 20 additions & 0 deletions unit-tests/fixtures/class-parse-error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* This file is part of the Zephir.
*
* (c) Zephir Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fixture;

class ParseErrorClass
{
public function syntaxError()
{
echo ';
}
}

0 comments on commit 9a3ec64

Please sign in to comment.