Skip to content

Commit

Permalink
Merge pull request #2204 from zephir-lang/#2186-fix-seg-fault
Browse files Browse the repository at this point in the history
#2186 - Change empty string check from `IS_UNDEF` to `ZEPHIR_IS_EMPTY`
  • Loading branch information
AlexNDRmac authored Apr 10, 2021
2 parents 93ed12f + 1386bdc commit 3e6241b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Library/Backends/ZendEngine3/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ public function ifVariableValueUndefined(Variable $var, CompilationContext $cont
if ($var->isDoublePointer()) {
return parent::ifVariableValueUndefined($var, $context, $useBody, $useCodePrinter);
}
$body = 'Z_TYPE_P('.$this->getVariableCode($var).') == IS_UNDEF';
$body = 'ZEPHIR_IS_EMPTY('.$this->getVariableCode($var).')';
$output = 'if ('.$body.') {';
if ($useCodePrinter) {
$context->codePrinter->output($output);
Expand Down
6 changes: 2 additions & 4 deletions Library/Optimizers/EvalExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function optimize($exprRaw, CompilationContext $compilationContext)

$this->usedVariables[] = $variableRight->getName();

/*
/**
* Evaluate the variable
*/
switch ($variableRight->getType()) {
Expand All @@ -203,9 +203,7 @@ public function optimize($exprRaw, CompilationContext $compilationContext)
return $variableRight->getName();

case 'string':
$variableRightCode = $compilationContext->backend->getVariableCode($variableRight);

return '!('.$compilationContext->backend->ifVariableValueUndefined($variableRight, $compilationContext, true, false).') && Z_STRLEN_P('.$variableRightCode.')';
return '!('.$compilationContext->backend->ifVariableValueUndefined($variableRight, $compilationContext, true, false).')';

case 'bool':
return $variableRight->getName();
Expand Down
30 changes: 30 additions & 0 deletions stub/strings.zep
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,34 @@ class Strings
{
return val === null;
}

public function issue2186SegFault(string val = null) -> bool
{
if val {
return true;
}

return false;
}

public function issue2186SegFaultCall(string val = null) -> string
{
return this->issue2186Child1(val);
}

protected function issue2186Child1(string val = null) -> string
{
return this->issue2186Child2(val);
}

protected function issue2186Child2(string val = null) -> string
{
var output = "";

if val {
let output = val . " all ok";
}

return output;
}
}
21 changes: 21 additions & 0 deletions tests/Extension/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,27 @@ public function testIssue2186(): void
$this->assertFalse($this->test->issue2186('string'));
$this->assertFalse($this->test->issue2186('0'));
$this->assertFalse($this->test->issue2186('1'));

$this->assertFalse($this->test->issue2186SegFault());
$this->assertFalse($this->test->issue2186SegFault(null));
$this->assertFalse($this->test->issue2186SegFault(''));

/**
* Assert PHP and Zephir behavior
*/
$zeroString = false;
if ('0') {
$zeroString = true;
}

$this->assertFalse(!empty('0'));
$this->assertFalse($zeroString);
$this->assertFalse($this->test->issue2186SegFault('0'));

$this->assertTrue($this->test->issue2186SegFault('string'));

$this->assertSame('', $this->test->issue2186SegFaultCall());
$this->assertSame('ok all ok', $this->test->issue2186SegFaultCall('ok'));
}

public function providerHashEquals(): array
Expand Down

0 comments on commit 3e6241b

Please sign in to comment.