From: Dmitry Stogov Date: Mon, 2 Mar 2015 09:25:40 +0000 (+0300) Subject: Improved assert() according to expectations RFC. X-Git-Tag: PRE_PHP7_EREG_MYSQL_REMOVALS~40 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9a20323e1946dff57eae8cd054e0893aefe83092;p=php Improved assert() according to expectations RFC. See https://wiki.php.net/rfc/expectations Squashed commit of the following: commit 3f3651a7870738e35ec541e53b53069152135b24 Author: Dmitry Stogov Date: Mon Mar 2 11:56:33 2015 +0300 opcode 137 is used for ZEND_OP_DATA and con't be reused for ZEND_ASSERT_CHECK commit ca8ecabf2a5f97f9116839c33396c9a7037e4368 Merge: 24328ac 9dac923 Author: Dmitry Stogov Date: Mon Mar 2 10:49:23 2015 +0300 Merge branch 'master' into assert * master: Update NEWS Fixed bug #69139 (Crash in gc_zval_possible_root on unserialize) windows only test Align entries format Align entries format for 5.6.7 Align entries format for 5.5.23 Bump header year Fixed bug #69144 (strtr not replacing with partly matching replace pairs) Fixed test? Revert mktime()/gmmktime()'s arginfo Update NEWS Fixed bug #69141 Missing arguments in reflection info for some builtin functions Add NEWS entry Remove useless date warning Fix ARG_INFO for levenshtein Fix ARG_INFO for levenshtein fix dir separator in tests Update NEWS Fixed bug #69085 (SoapClient's __call() type confusion through unserialize()). commit 24328ac03f79a0f3b19be7411bf9e173f392abda Merge: 021fd94 1cdee9a Author: Dmitry Stogov Date: Fri Feb 27 15:57:13 2015 +0300 Merge branch 'master' into assert * master: Fixed C++ support Fixed bug #69115 crash in mail Reorder Update NEWs Fixed bug #69121 (Segfault in get_current_user when script owner is not in passwd with ZTS build) Update News Fixed bug #69125 (Array numeric string as key) fix bug#68942's patch Fixed ability to build unspecialized executor Fixed bug #69124 (method name could not be used when by ref) Fixed a bug that header value is not terminated by '\0' when accessed through getenv(). Fixed a bug that header value is not terminated by '\0' when accessed through getenv(). commit 021fd94ed1d692d212e6e30c6c1a9767c3f16f7f Merge: 49963eb ace1f82 Author: Dmitry Stogov Date: Thu Feb 26 11:26:03 2015 +0300 Merge branch 'master' into assert * master: (59 commits) Improved ASSIGN_DIM handler Don't inline slow path Revert a part committted by mistake Fixed compilation warnings Fixed a bug that header value is not terminated by '\0' when accessed through getenv(). better name Improve fix for #69038 Update NEWs Fixed bug #69108 ("Segmentation fault" when (de)serializing SplObjectStorage) Added specialized versions of DO_FCALL handler: DO_ICALL - for internal functions DO_UCALL - for user functions DO_FCALL_BY_NAME - plain, most probably user, funcstions (not methods) Use cache_slot offsets instead of indexes (simplify run-time instructions) Split INIT_FCALL_BY_NAME inti INIT_FCALL_BY_NAME(CONST+STRING) and INIT_DYNAMIC_CALL(CONST-STRING|TMPVAR|CV) Support list($a, $b) = $a Avoid unnecassary check Make zend_array_destroy() to free the corresponding zend_array Eliminate check on the fast path Make current() and key() receive argument by value. Use Firebird default home folder, replace Interbase with Firebird Updated NEWS updated NEWS ... Conflicts: Zend/zend_vm_execute.h Zend/zend_vm_opcodes.c Zend/zend_vm_opcodes.h commit 49963ebf9d2bcd6d2670203dd72884f6ba6c8a4b Merge: 07b1f92 6b77959 Author: Dmitry Stogov Date: Thu Feb 19 11:13:08 2015 +0300 Merge branch 'master' into assert * master: Implemented AST pretty-printer update NEWS to match the actual stuff in 5.6.6 update NEWS to match the actual stuff in 5.5.22 update NEWS(add missing entry for the enchant fix, and reorder the entries a bit) fix typo in bug# update NEWS fix email format update NEWS update 5.6.6 release date in NEWS Fix bug #69033 (Request may get env. variables from previous requests if PHP works as FastCGI) BFN fix test fix test fix test Fixed bug #65593 (Segfault when calling ob_start from output buffering callback) Updated NEWS add CVE 5.4.39 next Fix associativity to match Perl Blast off to space. Conflicts: Zend/zend_ast.c commit 07b1f92ed662f6fa9309e679b83aff328362c98b Author: Dmitry Stogov Date: Wed Feb 18 23:06:32 2015 +0300 fixed pretty-printer (support for "elseif") commit 5a976c8d85078502b48379996ab066e57533a0c3 Author: Dmitry Stogov Date: Wed Feb 18 19:50:08 2015 +0300 Fixed vaeious ptetty-printer issues commit 69491e8e8e692030b0585aab485146906c0fedaf Merge: 8473157 3ddc246 Author: Dmitry Stogov Date: Wed Feb 18 10:18:32 2015 +0300 Merge branch 'master' into assert * master: Set PHP_JSON_VERSION to 1.4.0 Remove unnecessary resource checks in openssl ext JSON is now maintained commit 8473157fbb12d03fff8d5b602865a4b667522a4d Author: Dmitry Stogov Date: Wed Feb 18 10:17:26 2015 +0300 Fixed typo and white spaces commit 96de5ffc8d604df9797d0141ae5ad9c15e1d6c32 Author: Dmitry Stogov Date: Wed Feb 18 00:28:39 2015 +0300 Fixed assert() in namesapaces commit 5eba069c28e7b6590618707e0b21cdb2dd62a192 Merge: 4a2d9c0 d428bf2 Author: Dmitry Stogov Date: Tue Feb 17 22:45:55 2015 +0300 Merge branch 'master' into assert * master: (25 commits) improve debugability in TS debug builds More UPGRADING, in particular on foreach Fixed bug #69038 (switch(SOMECONSTANT) misbehaves) for master Replace var is introduced abstain from using xmlCleanupParser fix TS build Fix bug #68942 (Use after free vulnerability in unserialize() with DateTimeZone) update news Fix bug #68942 (Use after free vulnerability in unserialize() with DateTimeZone) Port for for bug #68552 Fix bug #68942 (Use after free vulnerability in unserialize() with DateTimeZone) Update NEWS Fixed bug #69038 (switch(SOMECONSTANT) misbehaves) - BFN Don't read the local php.ini when Generating Phar When building phar shared, you can end up loading a previous phar.so that isn't compatible with the php cli being used to generate Phar here. - Fixed bug #67827 (broken detection of system crypt sha256/sha512 support) Delete json outdated package.xml made ZEND_TSRMLS_CACHE_* macros look like function calls - Fix merge - Fixed bug #67427 (SoapServer cannot handle large messages) patch by: brandt at docoloc dot de ... commit 4a2d9c0953dccd9e78ebee9291e1213419eb9136 Author: Dmitry Stogov Date: Tue Feb 17 22:45:10 2015 +0300 Implemented AST pretty-printer to capture expression passed to assert() commit 7a059b66d51a65159801bd826346721325b89fec Merge: 9973df7 3892eba Author: Dmitry Stogov Date: Mon Feb 16 18:42:28 2015 +0300 Merge branch 'expect' of github.com:krakjoe/php-src into assert * 'expect' of github.com:krakjoe/php-src: import expect Conflicts: Zend/zend_compile.c Zend/zend_execute_API.c Zend/zend_globals.h Zend/zend_vm_def.h Zend/zend_vm_execute.h Zend/zend_vm_opcodes.c Zend/zend_vm_opcodes.h ext/opcache/Optimizer/block_pass.c ext/opcache/Optimizer/pass1_5.c ext/standard/assert.c ext/standard/tests/assert/assert_error3.phpt commit 3892eba2bf56a7699453855c995404106322718d Author: krakjoe Date: Sun Feb 2 12:49:35 2014 +0000 import expect --- diff --git a/Zend/tests/assert/expect_001.phpt b/Zend/tests/assert/expect_001.phpt new file mode 100644 index 0000000000..667c55b4fc --- /dev/null +++ b/Zend/tests/assert/expect_001.phpt @@ -0,0 +1,12 @@ +--TEST-- +test passing assertion +--INI-- +zend.assertions=1 +assert.exception=1 +--FILE-- + +--EXPECTF-- +bool(true) diff --git a/Zend/tests/assert/expect_002.phpt b/Zend/tests/assert/expect_002.phpt new file mode 100644 index 0000000000..2e54b1a459 --- /dev/null +++ b/Zend/tests/assert/expect_002.phpt @@ -0,0 +1,16 @@ +--TEST-- +test failing assertion +--INI-- +zend.assertions=1 +assert.exception=1 +--FILE-- + +--EXPECTF-- +Fatal error: Uncaught exception 'AssertionException' with message 'assert(false)' in %sexpect_002.php:%d +Stack trace: +#0 %sexpect_002.php(%d): assert(false, 'assert(false)') +#1 {main} + thrown in %sexpect_002.php on line %d diff --git a/Zend/tests/assert/expect_003.phpt b/Zend/tests/assert/expect_003.phpt new file mode 100644 index 0000000000..9a35f36cdf --- /dev/null +++ b/Zend/tests/assert/expect_003.phpt @@ -0,0 +1,15 @@ +--TEST-- +test catching failed assertion +--INI-- +zend.assertions=1 +assert.exception=1 +--FILE-- +getMessage()); +} +?> +--EXPECT-- +string(13) "assert(false)" diff --git a/Zend/tests/assert/expect_004.phpt b/Zend/tests/assert/expect_004.phpt new file mode 100644 index 0000000000..8b1e648ac7 --- /dev/null +++ b/Zend/tests/assert/expect_004.phpt @@ -0,0 +1,15 @@ +--TEST-- +test providing reason (fail) +--INI-- +zend.assertions=1 +assert.exception=1 +--FILE-- +getMessage()); +} +?> +--EXPECT-- +string(25) "I require this to succeed" diff --git a/Zend/tests/assert/expect_005.phpt b/Zend/tests/assert/expect_005.phpt new file mode 100644 index 0000000000..0a296d66b1 --- /dev/null +++ b/Zend/tests/assert/expect_005.phpt @@ -0,0 +1,17 @@ +--TEST-- +test providing reason (pass) +--INI-- +zend.assertions=1 +assert.exception=1 +--FILE-- +getMessage()); +} +var_dump(true); +?> +--EXPECT-- +bool(true) diff --git a/Zend/tests/assert/expect_006.phpt b/Zend/tests/assert/expect_006.phpt new file mode 100644 index 0000000000..1154f6a6f8 --- /dev/null +++ b/Zend/tests/assert/expect_006.phpt @@ -0,0 +1,14 @@ +--TEST-- +test looping assert (pass) +--INI-- +zend.assertions=1 +assert.exception=1 +--FILE-- + +--EXPECT-- +bool(true) diff --git a/Zend/tests/assert/expect_007.phpt b/Zend/tests/assert/expect_007.phpt new file mode 100644 index 0000000000..5b6aeffb4a --- /dev/null +++ b/Zend/tests/assert/expect_007.phpt @@ -0,0 +1,22 @@ +--TEST-- +test compiled reason +--INI-- +zend.assertions=1 +assert.exception=1 +--FILE-- + "X-HTTP ", + "value" => "testing" +); + +class HeaderMalfunctionException extends AssertionException {} + +assert (preg_match("~^([a-zA-Z0-9-]+)$~", $data["key"]), new HeaderMalfunctionException("malformed key found at {$next} \"{$data["key"]}\"")); +?> +--EXPECTF-- +Fatal error: Uncaught exception 'HeaderMalfunctionException' with message 'malformed key found at 1 "X-HTTP "' in %sexpect_007.php:10 +Stack trace: +#0 {main} + thrown in %sexpect_007.php on line 10 diff --git a/Zend/tests/assert/expect_008.phpt b/Zend/tests/assert/expect_008.phpt new file mode 100644 index 0000000000..64e524c352 --- /dev/null +++ b/Zend/tests/assert/expect_008.phpt @@ -0,0 +1,34 @@ +--TEST-- +test disabled expectations have no ill side effects +--INI-- +zend.assertions=0 +assert.exception=1 +--FILE-- + +--EXPECT-- +bool(true) diff --git a/Zend/tests/assert/expect_009.phpt b/Zend/tests/assert/expect_009.phpt new file mode 100644 index 0000000000..7e64c64bc7 --- /dev/null +++ b/Zend/tests/assert/expect_009.phpt @@ -0,0 +1,25 @@ +--TEST-- +test stack trace is correct from failed exception in extended class +--INI-- +zend.assertions=1 +assert.exception=1 +--FILE-- + +--EXPECTF-- +Fatal error: Uncaught exception 'AssertionException' with message 'assert(false)' in %sexpect_009.php:%d +Stack trace: +#0 %sexpect_009.php(%d): assert(false, 'assert(false)') +#1 %sexpect_009.php(%d): Two->__construct() +#2 {main} + thrown in %sexpect_009.php on line %d diff --git a/Zend/tests/assert/expect_010.phpt b/Zend/tests/assert/expect_010.phpt new file mode 100644 index 0000000000..8d52f6479b --- /dev/null +++ b/Zend/tests/assert/expect_010.phpt @@ -0,0 +1,23 @@ +--TEST-- +test stack trace is correct from failed exception in extended class (parent implementing constructor) +--INI-- +zend.assertions=1 +assert.exception=1 +--FILE-- + +--EXPECTF-- +Fatal error: Uncaught exception 'AssertionException' with message 'assert(false)' in %sexpect_010.php:%d +Stack trace: +#0 %sexpect_010.php(%d): assert(false, 'assert(false)') +#1 %sexpect_010.php(%d): One->__construct() +#2 {main} + thrown in %sexpect_010.php on line %d diff --git a/Zend/tests/assert/expect_011.phpt b/Zend/tests/assert/expect_011.phpt new file mode 100644 index 0000000000..0b5cf99b43 --- /dev/null +++ b/Zend/tests/assert/expect_011.phpt @@ -0,0 +1,30 @@ +--TEST-- +test overloaded __toString on custom exception +--INI-- +zend.assertions=1 +assert.exception=1 +--FILE-- + +--EXPECTF-- +Fatal error: Uncaught exception 'AssertionException' with message '[Message]: MyExpectations' in %sexpect_011.php:%d +Stack trace: +#0 %sexpect_011.php(%d): assert(false, '[Message]: MyEx...') +#1 %sexpect_011.php(%d): One->__construct() +#2 {main} + thrown in %sexpect_011.php on line %d diff --git a/Zend/tests/assert/expect_012.phpt b/Zend/tests/assert/expect_012.phpt new file mode 100644 index 0000000000..71e2f96e62 --- /dev/null +++ b/Zend/tests/assert/expect_012.phpt @@ -0,0 +1,21 @@ +--TEST-- +test enable/disable assertions at runtime +--INI-- +zend.assertions=1 +assert.exception=1 +--FILE-- + +--EXPECT-- +int(1) +int(0) +int(1) +bool(true) diff --git a/Zend/tests/assert/expect_013.phpt b/Zend/tests/assert/expect_013.phpt new file mode 100644 index 0000000000..cf8fa1cd02 --- /dev/null +++ b/Zend/tests/assert/expect_013.phpt @@ -0,0 +1,11 @@ +--TEST-- +test failing assertion when disabled (with return value) +--INI-- +zend.assertions=0 +assert.exception=1 +--FILE-- + +--EXPECT-- +bool(true) \ No newline at end of file diff --git a/Zend/tests/assert/expect_014.phpt b/Zend/tests/assert/expect_014.phpt new file mode 100644 index 0000000000..7cf4fb1931 --- /dev/null +++ b/Zend/tests/assert/expect_014.phpt @@ -0,0 +1,12 @@ +--TEST-- +test failing assertion when disabled +--INI-- +zend.assertions=0 +assert.exception=1 +--FILE-- + +--EXPECT-- +bool(true) \ No newline at end of file diff --git a/Zend/tests/assert/expect_015.phpt b/Zend/tests/assert/expect_015.phpt new file mode 100644 index 0000000000..942c480a7c --- /dev/null +++ b/Zend/tests/assert/expect_015.phpt @@ -0,0 +1,295 @@ +--TEST-- +AST pretty-peinter +--INI-- +zend.assertions=1 +assert.exception=0 +--FILE-- +'x', 'z'=>'c']; + @foo(); + $y = clone $x; + yield 1 => 2; +})); + +assert(0 && ($a = function &(array &$a, X $b = null) use ($c,&$d) : X { + abstract class A extends B implements C, D { + const X = 12; + const Y = self::X, Z = "aaa"; + + public $a = 1, $b; + protected $c; + static private $d = null; + + abstract function foo(); + + static private function f1() { + for ($i = 0, $j = 100; $i < $j; $i++, --$j) { + $s[$i] = $a[$j]; + } + foreach ($a as $key => &$val) { + print "$key => $val\n"; + } + while ($s[$i]) { + $i++; + } + do { + $i--; + } while ($s[$i]); + $x = foo($a + 1, 4, ...[1,2,3]); + $x = ${$a . "_1"}(); + $x = A::foo(); + $x = ${$a . "_1"}::foo(); + $x = A::${$a . "_1"}(); + $x = $x->foo(); + $x = ${$a . "_1"}->foo(); + $x = $x->{$a . "_1"}(); + $x->a = C::C; + ${$a . "_1"}->a = ${$a . "_1"}::C; + $x->{a . "_1"} = C::C; + $x = C::$z; + $x = ${$a . "_1"}::$z; + $x = C::${$z . "_1"}; + } + } +})); + +assert(0 && ($a = function &(array &$a, X $b = null) use ($c,&$d) : X { + final class A { + final protected function f2() { + if (!$x) { + return 0; + } + if ($x == 1) { + return 1; + } else if ($x == 2) { + return 2; + } else if ($x == 3) { + return 3; + } else { + if ($x == 9) { + return 9; + } +L0: + switch ($x) { + case 4: break; + case 5: continue; + case 6: break 2; + case 7: continue 2; + case 8: goto L0; + default: return; + } + } + } + } +})); + +assert(0 && ($a = function &(array &$a, X $b = null) use ($c,&$d) : X { + class A { + use T1, T2 { + T1::foo insteadof foo; + T2::foo as bar; + baz as public; + ops as protected x; + } + use T3; + } +})); + +assert(0 && ($a = function &(array &...$a) { + declare(A=1,B=2); + try { + $i++; + } catch (MyException $e) { + echo 1; + } catch (Exception $e) { + echo 2; + } finally { + echo 3; + } +})); + +assert(0 && ($a = function () { + declare(C=1) { echo 1; } + $x = '\'"`$a'; + $x = "'\"`$a"; + $x = `'"\`$a`; + $x = "{$a}b"; + $x = "${a}b"; + $x = " {$foo->bar} ${$foo->bar} "; + $x = " ${'---'} "; + foo(); + \foo(); + namespace\foo(); + $x = foo; + $x = \foo; + $x = namespace\foo; + $x = new foo(); + $x = new \foo(); + $x = new namespace\foo(); + if ($a) { + } elseif ($b) { + } +})); + +?> +--EXPECTF-- +Warning: Unsupported declare 'A' in %sexpect_015.php on line %d + +Warning: Unsupported declare 'B' in %sexpect_015.php on line %d + +Warning: Unsupported declare 'C' in %sexpect_015.php on line %d + +Warning: assert(): assert(0 && ($a = function () { + global $a; + global $$b; + static $c; + static $d = 0; + unset($e); + $x = isset($a) && !empty($b) || eval($c); + $x = $a ? $b : $c; + $x = $a ?: $c; + $x = $a ?? $b; + list($a, $b, $c) = [1, 2 => 'x', 'z' => 'c']; + @foo(); + $y = clone $x; + yield 1 => 2; +})) failed in %sexpect_015.php on line %d + +Warning: assert(): assert(0 && ($a = function &(array &$a, X $b = null) use($c, &$d): X { + abstract class A extends B implements C, D { + const X = 12; + const Y = self::X, Z = 'aaa'; + public $a = 1, $b; + protected $c; + private static $d = null; + public abstract function foo(); + + private static function f1() { + for ($i = 0, $j = 100; $i < $j; $i++, --$j) { + $s[$i] = $a[$j]; + } + foreach ($a as $key => &$val) { + print "$key => $val\n"; + } + while ($s[$i]) { + $i++; + } + do { + $i--; + } while ($s[$i]); + $x = foo($a + 1, 4, ...[1, 2, 3]); + $x = ${$a . '_1'}(); + $x = A::foo(); + $x = ${$a . '_1'}::foo(); + $x = A::${$a . '_1'}(); + $x = $x->foo(); + $x = ${$a . '_1'}->foo(); + $x = $x->{$a . '_1'}(); + $x->a = C::C; + ${$a . '_1'}->a = ${$a . '_1'}::C; + $x->{a . '_1'} = C::C; + $x = C::$z; + $x = ${$a . '_1'}::$z; + $x = C::${$z . '_1'}; + } + + } + +})) failed in %sexpect_015.php on line %d + +Warning: assert(): assert(0 && ($a = function &(array &$a, X $b = null) use($c, &$d): X { + final class A { + protected final function f2() { + if (!$x) { + return 0; + } + if ($x == 1) { + return 1; + } else if ($x == 2) { + return 2; + } else if ($x == 3) { + return 3; + } else { + if ($x == 9) { + return 9; + } + L0: + switch ($x) { + case 4: + break; + case 5: + continue; + case 6: + break 2; + case 7: + continue 2; + case 8: + goto L0; + default: + return; + } + } + } + + } + +})) failed in %sexpect_015.php on line %d + +Warning: assert(): assert(0 && ($a = function &(array &$a, X $b = null) use($c, &$d): X { + class A { + use T1, T2 { + T1::foo insteadof foo; + T2::foo as bar; + baz as public; + ops as protected x; + } + use T3; + } + +})) failed in %sexpect_015.php on line %d + +Warning: assert(): assert(0 && ($a = function &(array &...$a) { + declare(A = 1, B = 2); + try { + $i++; + } catch (MyException $e) { + echo 1; + } catch (Exception $e) { + echo 2; + } finally { + echo 3; + } +})) failed in %sexpect_015.php on line %d + +Warning: assert(): assert(0 && ($a = function () { + declare(C = 1) { + echo 1; + } + $x = '\'"`$a'; + $x = "'\"`$a"; + $x = `'"\`$a`; + $x = "{$a}b"; + $x = "{$a}b"; + $x = " {$foo->bar} {${$foo->bar}} "; + $x = " ${---} "; + foo(); + \foo(); + namespace\foo(); + $x = foo; + $x = \foo; + $x = namespace\foo; + $x = new foo(); + $x = new \foo(); + $x = new namespace\foo(); + if ($a) { + } elseif ($b) { + } +})) failed in %sexpect_015.php on line %d diff --git a/Zend/tests/assert/expect_016.phpt b/Zend/tests/assert/expect_016.phpt new file mode 100644 index 0000000000..84e71ff47b --- /dev/null +++ b/Zend/tests/assert/expect_016.phpt @@ -0,0 +1,24 @@ +--TEST-- +test enable/disable assertions at runtime (assertions not completely disabled) +--INI-- +zend.assertions=0 +assert.exception=0 +--FILE-- + +--EXPECTF-- +bool(true) +bool(true) + +Warning: assert(): assert(false) failed in %sexpect_016.php on line 6 +NULL +bool(true) + +Warning: zend.assertions may be completely enabled or disabled only in php.ini in %sexpect_016.php on line 8 diff --git a/Zend/tests/assert/expect_017.phpt b/Zend/tests/assert/expect_017.phpt new file mode 100644 index 0000000000..bc2c4d41a7 --- /dev/null +++ b/Zend/tests/assert/expect_017.phpt @@ -0,0 +1,19 @@ +--TEST-- +test enable/disable assertions at runtime (assertions completely disabled) +--INI-- +zend.assertions=-1 +assert.exception=0 +--FILE-- + +--EXPECTF-- +bool(true) +bool(true) + +Warning: zend.assertions may be completely enabled or disabled only in php.ini in %sexpect_017.php on line 4 + +Warning: zend.assertions may be completely enabled or disabled only in php.ini in %sexpect_017.php on line 5 diff --git a/Zend/tests/assert/expect_018.phpt b/Zend/tests/assert/expect_018.phpt new file mode 100644 index 0000000000..0c9b9fe40f --- /dev/null +++ b/Zend/tests/assert/expect_018.phpt @@ -0,0 +1,33 @@ +--TEST-- +test assertions in namespace +--INI-- +zend.assertions=1 +assert.exception=0 +--FILE-- + +--EXPECTF-- +bool(true) +bool(true) +bool(true) +bool(true) + +Warning: assert(): assert(false) failed in %sexpect_018.php on line 10 +NULL +bool(true) + +Warning: assert(): assert(false) failed in %sexpect_018.php on line 12 +NULL +bool(true) diff --git a/Zend/tests/assert/expect_019.phpt b/Zend/tests/assert/expect_019.phpt new file mode 100644 index 0000000000..38e9a39c33 --- /dev/null +++ b/Zend/tests/assert/expect_019.phpt @@ -0,0 +1,19 @@ +--TEST-- +test assertions in namespace (assertions completely disabled) +--INI-- +zend.assertions=-1 +assert.exception=0 +--FILE-- + +--EXPECTF-- +bool(true) +bool(true) +bool(true) +bool(true) diff --git a/Zend/zend.c b/Zend/zend.c index b1177eda5a..397fe879ba 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -99,9 +99,37 @@ static ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */ } /* }}} */ +static ZEND_INI_MH(OnUpdateAssertions) /* {{{ */ +{ + zend_long *p, val; +#ifndef ZTS + char *base = (char *) mh_arg2; +#else + char *base; + + base = (char *) ts_resource(*((int *) mh_arg2)); +#endif + + p = (zend_long *) (base+(size_t) mh_arg1); + + val = zend_atol(new_value->val, (int)new_value->len); + + if (stage != ZEND_INI_STAGE_STARTUP && + stage != ZEND_INI_STAGE_SHUTDOWN && + *p != val && + (*p < 0 || val < 0)) { + zend_error(E_WARNING, "zend.assertions may be completely enabled or disabled only in php.ini"); + return FAILURE; + } + + *p = val; + return SUCCESS; +} +/* }}} */ ZEND_INI_BEGIN() ZEND_INI_ENTRY("error_reporting", NULL, ZEND_INI_ALL, OnUpdateErrorReporting) + STD_ZEND_INI_BOOLEAN("zend.assertions", "1", ZEND_INI_ALL, OnUpdateAssertions, assertions, zend_executor_globals, executor_globals) STD_ZEND_INI_BOOLEAN("zend.enable_gc", "1", ZEND_INI_ALL, OnUpdateGCEnabled, gc_enabled, zend_gc_globals, gc_globals) STD_ZEND_INI_BOOLEAN("zend.multibyte", "0", ZEND_INI_PERDIR, OnUpdateBool, multibyte, zend_compiler_globals, compiler_globals) ZEND_INI_ENTRY("zend.script_encoding", NULL, ZEND_INI_ALL, OnUpdateScriptEncoding) diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 1a71608083..ae23d83f2a 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -2898,7 +2898,55 @@ int zend_compile_func_cuf(znode *result, zend_ast_list *args, zend_string *lcnam } /* }}} */ -int zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args) /* {{{ */ + + +static int zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, zend_function *fbc) /* {{{ */ +{ + if (EG(assertions) >= 0) { + znode name_node; + zend_op *opline; + uint32_t check_op_number = get_next_op_number(CG(active_op_array)); + + zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL); + + if (fbc) { + name_node.op_type = IS_CONST; + ZVAL_STR_COPY(&name_node.u.constant, name); + + opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node); + } else { + opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL); + opline->op2_type = IS_CONST; + opline->op2.constant = zend_add_ns_func_name_literal( + CG(active_op_array), name); + } + zend_alloc_cache_slot(opline->op2.constant); + + if (args->children == 1 && + (args->child[0]->kind != ZEND_AST_ZVAL || + Z_TYPE_P(zend_ast_get_zval(args->child[0])) != IS_STRING)) { + /* add "assert(condition) as assertion message */ + zend_ast_list_add((zend_ast*)args, + zend_ast_create_zval_from_str( + zend_ast_export("assert(", args->child[0], ")"))); + } + + zend_compile_call_common(result, (zend_ast*)args, fbc); + + CG(active_op_array)->opcodes[check_op_number].op2.opline_num = get_next_op_number(CG(active_op_array)); + } else { + if (!fbc) { + zend_string_release(name); + } + result->op_type = IS_CONST; + ZVAL_TRUE(&result->u.constant); + } + + return SUCCESS; +} +/* }}} */ + +int zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, zend_function *fbc) /* {{{ */ { if (zend_string_equals_literal(lcname, "strlen")) { return zend_compile_func_strlen(result, args); @@ -2930,6 +2978,8 @@ int zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_l return zend_compile_func_cufa(result, args, lcname); } else if (zend_string_equals_literal(lcname, "call_user_func")) { return zend_compile_func_cuf(result, args, lcname); + } else if (zend_string_equals_literal(lcname, "assert")) { + return zend_compile_assert(result, args, lcname, fbc); } else { return FAILURE; } @@ -2952,7 +3002,11 @@ void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ { zend_bool runtime_resolution = zend_compile_function_name(&name_node, name_ast); if (runtime_resolution) { - zend_compile_ns_call(result, &name_node, args_ast); + if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")) { + zend_compile_assert(result, args_ast, Z_STR(name_node.u.constant), NULL); + } else { + zend_compile_ns_call(result, &name_node, args_ast); + } return; } } @@ -2975,7 +3029,7 @@ void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ } if (zend_try_compile_special_func(result, lcname, - zend_ast_get_list(args_ast)) == SUCCESS + zend_ast_get_list(args_ast), fbc) == SUCCESS ) { zend_string_release(lcname); zval_ptr_dtor(&name_node.u.constant); diff --git a/Zend/zend_globals.h b/Zend/zend_globals.h index abebb19ae3..afd668afb8 100644 --- a/Zend/zend_globals.h +++ b/Zend/zend_globals.h @@ -225,6 +225,8 @@ struct _zend_executor_globals { zend_bool active; zend_bool valid_symbol_table; + zend_long assertions; + uint32_t ht_iterators_count; /* number of allocatd slots */ uint32_t ht_iterators_used; /* number of used slots */ HashTableIterator *ht_iterators; diff --git a/Zend/zend_opcode.c b/Zend/zend_opcode.c index 643f51b926..0473fc9f26 100644 --- a/Zend/zend_opcode.c +++ b/Zend/zend_opcode.c @@ -773,6 +773,7 @@ ZEND_API int pass_two(zend_op_array *op_array) case ZEND_FE_RESET_RW: case ZEND_FE_FETCH_R: case ZEND_FE_FETCH_RW: + case ZEND_ASSERT_CHECK: ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2); break; case ZEND_VERIFY_RETURN_TYPE: diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index 39815b400f..2e9ffd6448 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -6839,4 +6839,20 @@ ZEND_VM_HANDLER(122, ZEND_DEFINED, CONST, ANY) ZEND_VM_NEXT_OPCODE(); } +ZEND_VM_HANDLER(151, ZEND_ASSERT_CHECK, ANY, ANY) +{ + USE_OPLINE + + if (EG(assertions) <= 0) { + zend_op *target = OP_JMP_ADDR(opline, opline->op2); + + if (RETURN_VALUE_USED(target-1)) { + ZVAL_TRUE(EX_VAR((target-1)->result.var)); + } + ZEND_VM_JMP(target); + } else { + ZEND_VM_NEXT_OPCODE(); + } +} + ZEND_VM_EXPORT_HANDLER(zend_do_fcall, ZEND_DO_FCALL) diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 90aa7111f0..465ecc6dd4 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -1696,6 +1696,22 @@ static int ZEND_FASTCALL ZEND_FAST_RET_SPEC_HANDLER(ZEND_OPCODE_HANDLER_ARGS) } } +static int ZEND_FASTCALL ZEND_ASSERT_CHECK_SPEC_HANDLER(ZEND_OPCODE_HANDLER_ARGS) +{ + USE_OPLINE + + if (EG(assertions) <= 0) { + zend_op *target = OP_JMP_ADDR(opline, opline->op2); + + if (RETURN_VALUE_USED(target-1)) { + ZVAL_TRUE(EX_VAR((target-1)->result.var)); + } + ZEND_VM_JMP(target); + } else { + ZEND_VM_NEXT_OPCODE(); + } +} + static int ZEND_FASTCALL ZEND_FETCH_CLASS_SPEC_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS) { USE_OPLINE @@ -41219,31 +41235,31 @@ void zend_init_opcodes_handlers(void) ZEND_USER_OPCODE_SPEC_HANDLER, ZEND_USER_OPCODE_SPEC_HANDLER, ZEND_USER_OPCODE_SPEC_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, - ZEND_NULL_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, + ZEND_ASSERT_CHECK_SPEC_HANDLER, ZEND_JMP_SET_SPEC_CONST_HANDLER, ZEND_JMP_SET_SPEC_CONST_HANDLER, ZEND_JMP_SET_SPEC_CONST_HANDLER, diff --git a/Zend/zend_vm_opcodes.c b/Zend/zend_vm_opcodes.c index 991e56bc77..9bc531634d 100644 --- a/Zend/zend_vm_opcodes.c +++ b/Zend/zend_vm_opcodes.c @@ -173,7 +173,7 @@ const char *zend_vm_opcodes_map[171] = { "ZEND_ISSET_ISEMPTY_PROP_OBJ", "ZEND_HANDLE_EXCEPTION", "ZEND_USER_OPCODE", - NULL, + "ZEND_ASSERT_CHECK", "ZEND_JMP_SET", "ZEND_DECLARE_LAMBDA_FUNCTION", "ZEND_ADD_TRAIT", diff --git a/Zend/zend_vm_opcodes.h b/Zend/zend_vm_opcodes.h index 367e52150c..e161ee0078 100644 --- a/Zend/zend_vm_opcodes.h +++ b/Zend/zend_vm_opcodes.h @@ -173,6 +173,7 @@ END_EXTERN_C() #define ZEND_ISSET_ISEMPTY_PROP_OBJ 148 #define ZEND_HANDLE_EXCEPTION 149 #define ZEND_USER_OPCODE 150 +#define ZEND_ASSERT_CHECK 151 #define ZEND_JMP_SET 152 #define ZEND_DECLARE_LAMBDA_FUNCTION 153 #define ZEND_ADD_TRAIT 154 diff --git a/ext/mysqli/tests/mysqli_class_mysqli_properties_no_conn.phpt b/ext/mysqli/tests/mysqli_class_mysqli_properties_no_conn.phpt index e4d0b08a85..7e36c6571e 100644 --- a/ext/mysqli/tests/mysqli_class_mysqli_properties_no_conn.phpt +++ b/ext/mysqli/tests/mysqli_class_mysqli_properties_no_conn.phpt @@ -188,7 +188,7 @@ warning_count = 'NULL' Magic, magic properties: mysqli->affected_rows = ''/NULL (''/NULL) -Warning: assert(): Assertion failed in %s on line %d +Warning: assert(): assert(@mysqli_get_client_info() === @$mysqli->client_info) failed in %s on line %d mysqli->client_info = ''/NULL ('%s'/%s) mysqli->client_version = '%s'/integer ('%s'/integer) mysqli->errno = ''/NULL (''/NULL) @@ -199,7 +199,7 @@ mysqli->sqlstate = ''/NULL (''/NULL) mysqli->host_info = ''/NULL (''/NULL) mysqli->info = ''/NULL (''/NULL) -Warning: assert(): Assertion failed in %s on line %d +Warning: assert(): assert(@mysqli_thread_id($mysqli) > @$mysqli->thread_id) failed in %s on line %d mysqli->thread_id = ''/NULL (''/NULL) mysqli->protocol_version = ''/NULL (''/NULL) mysqli->server_info = ''/NULL (''/NULL) @@ -261,7 +261,7 @@ warning_count = 'NULL' Magic, magic properties: mysqli->affected_rows = ''/NULL (''/NULL) -Warning: assert(): Assertion failed in %s on line %d +Warning: assert(): assert(@mysqli_get_client_info() === @$mysqli->client_info) failed in %s on line %d mysqli->client_info = ''/NULL ('%s'/%s) mysqli->client_version = '%s'/integer ('%s'/integer) mysqli->errno = ''/NULL (''/NULL) @@ -272,7 +272,7 @@ mysqli->sqlstate = ''/NULL (''/NULL) mysqli->host_info = ''/NULL (''/NULL) mysqli->info = ''/NULL (''/NULL) -Warning: assert(): Assertion failed in %s on line %d +Warning: assert(): assert(@mysqli_thread_id($mysqli) > @$mysqli->thread_id) failed in %s on line %d mysqli->thread_id = ''/NULL (''/NULL) mysqli->protocol_version = ''/NULL (''/NULL) mysqli->server_info = ''/NULL (''/NULL) diff --git a/ext/opcache/Optimizer/block_pass.c b/ext/opcache/Optimizer/block_pass.c index 0922d6e809..3e3c16558a 100644 --- a/ext/opcache/Optimizer/block_pass.c +++ b/ext/opcache/Optimizer/block_pass.c @@ -171,6 +171,7 @@ static int find_code_blocks(zend_op_array *op_array, zend_cfg *cfg, zend_optimiz case ZEND_NEW: case ZEND_JMP_SET: case ZEND_COALESCE: + case ZEND_ASSERT_CHECK: START_BLOCK_OP(ZEND_OP2(opline).opline_num); START_BLOCK_OP(opno + 1); break; @@ -306,6 +307,7 @@ static int find_code_blocks(zend_op_array *op_array, zend_cfg *cfg, zend_optimiz case ZEND_COALESCE: case ZEND_FE_FETCH_R: case ZEND_FE_FETCH_RW: + case ZEND_ASSERT_CHECK: cur_block->op2_to = &blocks[ZEND_OP2(opline).opline_num]; /* break missing intentionally */ default: diff --git a/ext/opcache/Optimizer/nop_removal.c b/ext/opcache/Optimizer/nop_removal.c index dccd010ae4..061d2bb89f 100644 --- a/ext/opcache/Optimizer/nop_removal.c +++ b/ext/opcache/Optimizer/nop_removal.c @@ -100,6 +100,7 @@ void zend_optimizer_nop_removal(zend_op_array *op_array) case ZEND_NEW: case ZEND_JMP_SET: case ZEND_COALESCE: + case ZEND_ASSERT_CHECK: ZEND_OP2(opline).opline_num -= shiftlist[ZEND_OP2(opline).opline_num]; break; case ZEND_JMPZNZ: diff --git a/ext/opcache/Optimizer/pass1_5.c b/ext/opcache/Optimizer/pass1_5.c index 990b845d43..5bff6ab3a8 100644 --- a/ext/opcache/Optimizer/pass1_5.c +++ b/ext/opcache/Optimizer/pass1_5.c @@ -609,6 +609,7 @@ void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx) case ZEND_NEW: case ZEND_JMP_SET: case ZEND_COALESCE: + case ZEND_ASSERT_CHECK: collect_constants = 0; break; case ZEND_FETCH_R: diff --git a/ext/opcache/Optimizer/zend_optimizer.c b/ext/opcache/Optimizer/zend_optimizer.c index 1941a56dfa..7be076cb7f 100644 --- a/ext/opcache/Optimizer/zend_optimizer.c +++ b/ext/opcache/Optimizer/zend_optimizer.c @@ -495,6 +495,7 @@ static void zend_accel_optimize(zend_op_array *op_array, case ZEND_FE_RESET_RW: case ZEND_FE_FETCH_R: case ZEND_FE_FETCH_RW: + case ZEND_ASSERT_CHECK: ZEND_PASS_TWO_UNDO_JMP_TARGET(op_array, opline, ZEND_OP2(opline)); break; } @@ -535,6 +536,7 @@ static void zend_accel_optimize(zend_op_array *op_array, case ZEND_FE_RESET_RW: case ZEND_FE_FETCH_R: case ZEND_FE_FETCH_RW: + case ZEND_ASSERT_CHECK: ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, ZEND_OP2(opline)); break; } diff --git a/ext/opcache/zend_persist.c b/ext/opcache/zend_persist.c index 9a929be6ab..c0cd5b270b 100644 --- a/ext/opcache/zend_persist.c +++ b/ext/opcache/zend_persist.c @@ -451,6 +451,7 @@ static void zend_persist_op_array_ex(zend_op_array *op_array, zend_persistent_sc case ZEND_FE_RESET_RW: case ZEND_FE_FETCH_R: case ZEND_FE_FETCH_RW: + case ZEND_ASSERT_CHECK: ZEND_OP2(opline).jmp_addr = &new_opcodes[ZEND_OP2(opline).jmp_addr - op_array->opcodes]; break; } diff --git a/ext/reflection/tests/ReflectionExtension_getClassNames_basic.phpt b/ext/reflection/tests/ReflectionExtension_getClassNames_basic.phpt index dfec951f79..0728d505c3 100644 --- a/ext/reflection/tests/ReflectionExtension_getClassNames_basic.phpt +++ b/ext/reflection/tests/ReflectionExtension_getClassNames_basic.phpt @@ -9,12 +9,14 @@ var_dump($standard->getClassNames()); ?> ==DONE== --EXPECTF-- -array(3) { +array(4) { [0]=> %s(22) "__PHP_Incomplete_Class" [1]=> %s(15) "php_user_filter" [2]=> %s(9) "Directory" + [3]=> + %s(18) "AssertionException" } ==DONE== diff --git a/ext/standard/assert.c b/ext/standard/assert.c index 15803fa075..3eb870dd67 100644 --- a/ext/standard/assert.c +++ b/ext/standard/assert.c @@ -22,6 +22,7 @@ #include "php.h" #include "php_assert.h" #include "php_ini.h" +#include "zend_exceptions.h" /* }}} */ ZEND_BEGIN_MODULE_GLOBALS(assert) @@ -31,10 +32,13 @@ ZEND_BEGIN_MODULE_GLOBALS(assert) zend_bool bail; zend_bool warning; zend_bool quiet_eval; + zend_bool exception; ZEND_END_MODULE_GLOBALS(assert) ZEND_DECLARE_MODULE_GLOBALS(assert) +static zend_class_entry *assertion_exception_ce; + #ifdef ZTS #define ASSERTG(v) ZEND_TSRMG(assert_globals_id, zend_assert_globals *, v) #else @@ -48,7 +52,8 @@ enum { ASSERT_CALLBACK, ASSERT_BAIL, ASSERT_WARNING, - ASSERT_QUIET_EVAL + ASSERT_QUIET_EVAL, + ASSERT_EXCEPTION }; static PHP_INI_MH(OnChangeCallback) /* {{{ */ @@ -83,6 +88,7 @@ PHP_INI_BEGIN() STD_PHP_INI_ENTRY("assert.warning", "1", PHP_INI_ALL, OnUpdateBool, warning, zend_assert_globals, assert_globals) PHP_INI_ENTRY("assert.callback", NULL, PHP_INI_ALL, OnChangeCallback) STD_PHP_INI_ENTRY("assert.quiet_eval", "0", PHP_INI_ALL, OnUpdateBool, quiet_eval, zend_assert_globals, assert_globals) + STD_PHP_INI_ENTRY("assert.exception", "0", PHP_INI_ALL, OnUpdateBool, exception, zend_assert_globals, assert_globals) PHP_INI_END() static void php_assert_init_globals(zend_assert_globals *assert_globals_p) /* {{{ */ @@ -94,6 +100,8 @@ static void php_assert_init_globals(zend_assert_globals *assert_globals_p) /* {{ PHP_MINIT_FUNCTION(assert) /* {{{ */ { + zend_class_entry ce; + ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL); REGISTER_INI_ENTRIES(); @@ -103,6 +111,10 @@ PHP_MINIT_FUNCTION(assert) /* {{{ */ REGISTER_LONG_CONSTANT("ASSERT_BAIL", ASSERT_BAIL, CONST_CS|CONST_PERSISTENT); REGISTER_LONG_CONSTANT("ASSERT_WARNING", ASSERT_WARNING, CONST_CS|CONST_PERSISTENT); REGISTER_LONG_CONSTANT("ASSERT_QUIET_EVAL", ASSERT_QUIET_EVAL, CONST_CS|CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("ASSERT_EXCEPTION", ASSERT_EXCEPTION, CONST_CS|CONST_PERSISTENT); + + INIT_CLASS_ENTRY(ce, "AssertionException", NULL); + assertion_exception_ce = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C)); return SUCCESS; } @@ -135,21 +147,21 @@ PHP_MINFO_FUNCTION(assert) /* {{{ */ } /* }}} */ -/* {{{ proto int assert(string|bool assertion[, string description]) +/* {{{ proto int assert(string|bool assertion[, mixed description]) Checks if assertion is false */ PHP_FUNCTION(assert) { zval *assertion; + zval *description = NULL; int val; - size_t description_len = 0; char *myeval = NULL; - char *compiled_string_description, *description = NULL; + char *compiled_string_description; if (! ASSERTG(active)) { RETURN_TRUE; } - if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|s", &assertion, &description, &description_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|z", &assertion, &description) == FAILURE) { return; } @@ -167,10 +179,12 @@ PHP_FUNCTION(assert) compiled_string_description = zend_make_compiled_string_description("assert code"); if (zend_eval_stringl(myeval, Z_STRLEN_P(assertion), &retval, compiled_string_description) == FAILURE) { efree(compiled_string_description); - if (description_len == 0) { + if (!description) { php_error_docref(NULL, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s", PHP_EOL, myeval); } else { - php_error_docref(NULL, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s:\"%s\"", PHP_EOL, description, myeval); + zend_string *str = zval_get_string(description); + php_error_docref(NULL, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s:\"%s\"", PHP_EOL, str->val, myeval); + zend_string_release(str); } if (ASSERTG(bail)) { zend_bailout(); @@ -186,8 +200,7 @@ PHP_FUNCTION(assert) convert_to_boolean(&retval); val = Z_TYPE(retval) == IS_TRUE; } else { - convert_to_boolean_ex(assertion); - val = Z_TYPE_P(assertion) == IS_TRUE; + val = zend_is_true(assertion); } if (val) { @@ -199,7 +212,7 @@ PHP_FUNCTION(assert) } if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) { - zval *args = safe_emalloc(description_len == 0 ? 3 : 4, sizeof(zval), 0); + zval *args = safe_emalloc(!description ? 3 : 4, sizeof(zval), 0); zval retval; int i; uint lineno = zend_get_executed_lineno(); @@ -212,14 +225,13 @@ PHP_FUNCTION(assert) ZVAL_FALSE(&retval); /* XXX do we want to check for error here? */ - if (description_len == 0) { + if (!description) { call_user_function(CG(function_table), NULL, &ASSERTG(callback), &retval, 3, args); for (i = 0; i <= 2; i++) { zval_ptr_dtor(&(args[i])); } } else { - ZVAL_STRINGL(&args[3], SAFE_STRING(description), description_len); - + ZVAL_STR(&args[3], zval_get_string(description)); call_user_function(CG(function_table), NULL, &ASSERTG(callback), &retval, 4, args); for (i = 0; i <= 3; i++) { zval_ptr_dtor(&(args[i])); @@ -230,19 +242,33 @@ PHP_FUNCTION(assert) zval_ptr_dtor(&retval); } - if (ASSERTG(warning)) { - if (description_len == 0) { + if (ASSERTG(exception)) { + if (!description) { + zend_throw_exception(assertion_exception_ce, NULL, E_ERROR TSRMLS_CC); + } else if (Z_TYPE_P(description) == IS_OBJECT && + instanceof_function(Z_OBJCE_P(description), assertion_exception_ce TSRMLS_CC)) { + Z_ADDREF_P(description); + zend_throw_exception_object(description TSRMLS_CC); + } else { + zend_string *str = zval_get_string(description); + zend_throw_exception(assertion_exception_ce, str->val, E_ERROR TSRMLS_CC); + zend_string_release(str); + } + } else if (ASSERTG(warning)) { + if (!description) { if (myeval) { php_error_docref(NULL, E_WARNING, "Assertion \"%s\" failed", myeval); } else { php_error_docref(NULL, E_WARNING, "Assertion failed"); } } else { + zend_string *str = zval_get_string(description); if (myeval) { - php_error_docref(NULL, E_WARNING, "%s: \"%s\" failed", description, myeval); + php_error_docref(NULL, E_WARNING, "%s: \"%s\" failed", str->val, myeval); } else { - php_error_docref(NULL, E_WARNING, "%s failed", description); + php_error_docref(NULL, E_WARNING, "%s failed", str->val); } + zend_string_release(str); } } @@ -329,6 +355,18 @@ PHP_FUNCTION(assert_options) } return; + case ASSERT_EXCEPTION: + oldint = ASSERTG(exception); + if (ac == 2) { + zend_string *key = zend_string_init("assert.exception", sizeof("assert.exception")-1, 0); + zend_string *val = zval_get_string(value); + zend_alter_ini_entry_ex(key, val, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC); + zend_string_release(val); + zend_string_release(key); + } + RETURN_LONG(oldint); + break; + default: php_error_docref(NULL, E_WARNING, "Unknown value %pd", what); break; diff --git a/ext/standard/tests/assert/assert04.phpt b/ext/standard/tests/assert/assert04.phpt index 12efef97e2..53f8d92167 100644 --- a/ext/standard/tests/assert/assert04.phpt +++ b/ext/standard/tests/assert/assert04.phpt @@ -42,7 +42,7 @@ Warning: assert_options() expects at most 2 parameters, 3 given in %s on line %d Warning: assert_options() expects parameter 1 to be integer, %unicode_string_optional% given in %s on line %d -Warning: assert(): Assertion failed in %s on line %d +Warning: assert(): assert(0) failed in %s on line %d -Warning: assert(): Assertion failed in %s on line %d +Warning: assert(): assert(0) failed in %s on line %d diff --git a/ext/standard/tests/assert/assert_basic2.phpt b/ext/standard/tests/assert/assert_basic2.phpt index 277e5cad18..42c59b1b9a 100644 --- a/ext/standard/tests/assert/assert_basic2.phpt +++ b/ext/standard/tests/assert/assert_basic2.phpt @@ -29,9 +29,9 @@ assert(0); string(2) "f1" f1 called -Warning: assert(): Assertion failed in %s on line 13 +Warning: assert(): assert(0) failed in %s on line 13 string(2) "f1" string(2) "f2" f2 called -Warning: assert(): Assertion failed in %s on line 17 +Warning: assert(): assert(0) failed in %s on line 17 diff --git a/ext/standard/tests/assert/assert_basic3.phpt b/ext/standard/tests/assert/assert_basic3.phpt index 0ce326606f..9fcbbd5c3c 100644 --- a/ext/standard/tests/assert/assert_basic3.phpt +++ b/ext/standard/tests/assert/assert_basic3.phpt @@ -22,4 +22,4 @@ echo "If this is printed BAIL hasn't worked"; int(0) f1 called -Warning: assert(): Assertion "0 != 0" failed in %s on line 10 +Warning: assert(): assert($sa): "0 != 0" failed in %s on line 10 diff --git a/ext/standard/tests/assert/assert_basic5.phpt b/ext/standard/tests/assert/assert_basic5.phpt index 737f902d64..d67d879262 100644 --- a/ext/standard/tests/assert/assert_basic5.phpt +++ b/ext/standard/tests/assert/assert_basic5.phpt @@ -26,7 +26,7 @@ var_dump($rao=assert_options(ASSERT_WARNING, 0)); int(0) f1 called -Warning: assert(): Assertion "0 != 0" failed in %s on line 10 +Warning: assert(): assert($sa): "0 != 0" failed in %s on line 10 NULL bool(true) int(1) diff --git a/ext/standard/tests/assert/assert_closures.phpt b/ext/standard/tests/assert/assert_closures.phpt index e01c11ace9..4a013283a8 100644 --- a/ext/standard/tests/assert/assert_closures.phpt +++ b/ext/standard/tests/assert/assert_closures.phpt @@ -13,4 +13,4 @@ assert(0); --EXPECTF-- Hello World! -Warning: assert(): Assertion failed in %s on line %d +Warning: assert(): assert(0) failed in %s on line %d diff --git a/ext/standard/tests/assert/assert_error2.phpt b/ext/standard/tests/assert/assert_error2.phpt index da7c3d9e0b..41a7197535 100644 --- a/ext/standard/tests/assert/assert_error2.phpt +++ b/ext/standard/tests/assert/assert_error2.phpt @@ -17,8 +17,7 @@ function f1($script, $line, $message, $user_message) //bail out on error var_dump($rao = assert_options(ASSERT_BAIL, 1)); -$sa = "0 != 0"; -var_dump($r2 = assert($sa)); +var_dump($r2 = assert("0 != 0")); echo "If this is printed BAIL hasn't worked"; --EXPECTF-- int(0) @@ -26,5 +25,5 @@ int(0) Warning: Missing argument 4 for f1() in %s on line 2 f1 called -Warning: assert(): Assertion "0 != 0" failed in %s on line 10 +Warning: assert(): Assertion "0 != 0" failed in %s on line 9 diff --git a/ext/standard/tests/assert/assert_error3.phpt b/ext/standard/tests/assert/assert_error3.phpt index 6df2fe4e26..d14397515f 100644 --- a/ext/standard/tests/assert/assert_error3.phpt +++ b/ext/standard/tests/assert/assert_error3.phpt @@ -10,12 +10,11 @@ error_reporting = -1 display_errors = 1 --FILE--