]> granicus.if.org Git - php/commitdiff
Improved assert() according to expectations RFC.
authorDmitry Stogov <dmitry@zend.com>
Mon, 2 Mar 2015 09:25:40 +0000 (12:25 +0300)
committerDmitry Stogov <dmitry@zend.com>
Mon, 2 Mar 2015 09:25:40 +0000 (12:25 +0300)
See https://wiki.php.net/rfc/expectations

Squashed commit of the following:

commit 3f3651a7870738e35ec541e53b53069152135b24
Author: Dmitry Stogov <dmitry@zend.com>
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 <dmitry@zend.com>
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 <dmitry@zend.com>
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 <dmitry@zend.com>
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 <dmitry@zend.com>
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 <dmitry@zend.com>
Date:   Wed Feb 18 23:06:32 2015 +0300

    fixed pretty-printer (support for "elseif")

commit 5a976c8d85078502b48379996ab066e57533a0c3
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Wed Feb 18 19:50:08 2015 +0300

    Fixed vaeious ptetty-printer issues

commit 69491e8e8e692030b0585aab485146906c0fedaf
Merge: 8473157 3ddc246
Author: Dmitry Stogov <dmitry@zend.com>
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 <dmitry@zend.com>
Date:   Wed Feb 18 10:17:26 2015 +0300

    Fixed typo and white spaces

commit 96de5ffc8d604df9797d0141ae5ad9c15e1d6c32
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Wed Feb 18 00:28:39 2015 +0300

    Fixed assert() in namesapaces

commit 5eba069c28e7b6590618707e0b21cdb2dd62a192
Merge: 4a2d9c0 d428bf2
Author: Dmitry Stogov <dmitry@zend.com>
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 <dmitry@zend.com>
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 <dmitry@zend.com>
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 <joe.watkins@live.co.uk>
Date:   Sun Feb 2 12:49:35 2014 +0000

    import expect

42 files changed:
Zend/tests/assert/expect_001.phpt [new file with mode: 0644]
Zend/tests/assert/expect_002.phpt [new file with mode: 0644]
Zend/tests/assert/expect_003.phpt [new file with mode: 0644]
Zend/tests/assert/expect_004.phpt [new file with mode: 0644]
Zend/tests/assert/expect_005.phpt [new file with mode: 0644]
Zend/tests/assert/expect_006.phpt [new file with mode: 0644]
Zend/tests/assert/expect_007.phpt [new file with mode: 0644]
Zend/tests/assert/expect_008.phpt [new file with mode: 0644]
Zend/tests/assert/expect_009.phpt [new file with mode: 0644]
Zend/tests/assert/expect_010.phpt [new file with mode: 0644]
Zend/tests/assert/expect_011.phpt [new file with mode: 0644]
Zend/tests/assert/expect_012.phpt [new file with mode: 0644]
Zend/tests/assert/expect_013.phpt [new file with mode: 0644]
Zend/tests/assert/expect_014.phpt [new file with mode: 0644]
Zend/tests/assert/expect_015.phpt [new file with mode: 0644]
Zend/tests/assert/expect_016.phpt [new file with mode: 0644]
Zend/tests/assert/expect_017.phpt [new file with mode: 0644]
Zend/tests/assert/expect_018.phpt [new file with mode: 0644]
Zend/tests/assert/expect_019.phpt [new file with mode: 0644]
Zend/zend.c
Zend/zend_compile.c
Zend/zend_globals.h
Zend/zend_opcode.c
Zend/zend_vm_def.h
Zend/zend_vm_execute.h
Zend/zend_vm_opcodes.c
Zend/zend_vm_opcodes.h
ext/mysqli/tests/mysqli_class_mysqli_properties_no_conn.phpt
ext/opcache/Optimizer/block_pass.c
ext/opcache/Optimizer/nop_removal.c
ext/opcache/Optimizer/pass1_5.c
ext/opcache/Optimizer/zend_optimizer.c
ext/opcache/zend_persist.c
ext/reflection/tests/ReflectionExtension_getClassNames_basic.phpt
ext/standard/assert.c
ext/standard/tests/assert/assert04.phpt
ext/standard/tests/assert/assert_basic2.phpt
ext/standard/tests/assert/assert_basic3.phpt
ext/standard/tests/assert/assert_basic5.phpt
ext/standard/tests/assert/assert_closures.phpt
ext/standard/tests/assert/assert_error2.phpt
ext/standard/tests/assert/assert_error3.phpt

diff --git a/Zend/tests/assert/expect_001.phpt b/Zend/tests/assert/expect_001.phpt
new file mode 100644 (file)
index 0000000..667c55b
--- /dev/null
@@ -0,0 +1,12 @@
+--TEST--
+test passing assertion
+--INI--
+zend.assertions=1
+assert.exception=1
+--FILE--
+<?php
+assert(true);
+var_dump(true);
+?>
+--EXPECTF--
+bool(true)
diff --git a/Zend/tests/assert/expect_002.phpt b/Zend/tests/assert/expect_002.phpt
new file mode 100644 (file)
index 0000000..2e54b1a
--- /dev/null
@@ -0,0 +1,16 @@
+--TEST--
+test failing assertion
+--INI--
+zend.assertions=1
+assert.exception=1
+--FILE--
+<?php
+assert(false);
+var_dump(true);
+?>
+--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 (file)
index 0000000..9a35f36
--- /dev/null
@@ -0,0 +1,15 @@
+--TEST--
+test catching failed assertion
+--INI--
+zend.assertions=1
+assert.exception=1
+--FILE--
+<?php
+try {
+    assert(false);
+} catch (AssertionException $ex) {
+    var_dump($ex->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 (file)
index 0000000..8b1e648
--- /dev/null
@@ -0,0 +1,15 @@
+--TEST--
+test providing reason (fail)
+--INI--
+zend.assertions=1
+assert.exception=1
+--FILE--
+<?php
+try {
+    assert(false, "I require this to succeed");
+} catch (AssertionException $ex) {
+    var_dump($ex->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 (file)
index 0000000..0a296d6
--- /dev/null
@@ -0,0 +1,17 @@
+--TEST--
+test providing reason (pass)
+--INI--
+zend.assertions=1
+assert.exception=1
+--FILE--
+<?php
+try {
+    /* by passing we test there are no leaks upon success */
+    assert(true, "I require this to succeed");
+} catch (AssertionException $ex) {
+    var_dump($ex->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 (file)
index 0000000..1154f6a
--- /dev/null
@@ -0,0 +1,14 @@
+--TEST--
+test looping assert (pass)
+--INI--
+zend.assertions=1
+assert.exception=1
+--FILE--
+<?php
+for($i=0; $i<100000; $i++) {
+    assert ($i < 100000, "The universe should make sense");
+}
+var_dump(true);
+?>
+--EXPECT--
+bool(true)
diff --git a/Zend/tests/assert/expect_007.phpt b/Zend/tests/assert/expect_007.phpt
new file mode 100644 (file)
index 0000000..5b6aeff
--- /dev/null
@@ -0,0 +1,22 @@
+--TEST--
+test compiled reason
+--INI--
+zend.assertions=1
+assert.exception=1
+--FILE--
+<?php
+$next = 1;
+$data = array(
+    "key" => "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 (file)
index 0000000..64e524c
--- /dev/null
@@ -0,0 +1,34 @@
+--TEST--
+test disabled expectations have no ill side effects
+--INI--
+zend.assertions=0
+assert.exception=1
+--FILE--
+<?php
+class One {
+    public function __construct() {
+        assert($this || 0);
+    }
+}
+class Two extends One {}
+
+class OdEar extends AssertionException {}
+
+function blah(){ return 1; }
+
+$variable = 1;
+assert(true, "constant message");
+assert(($variable && $variable) || php_sapi_name(), new OdEar("constant message"));
+assert(false);
+assert(blah(), blah());
+
+new Two();
+new Two();
+new Two();
+
+assert (blah() || blah() || blah(), blah() || blah() || blah() || blah());
+
+var_dump(true);
+?>
+--EXPECT--
+bool(true)
diff --git a/Zend/tests/assert/expect_009.phpt b/Zend/tests/assert/expect_009.phpt
new file mode 100644 (file)
index 0000000..7e64c64
--- /dev/null
@@ -0,0 +1,25 @@
+--TEST--
+test stack trace is correct from failed exception in extended class
+--INI--
+zend.assertions=1
+assert.exception=1
+--FILE--
+<?php
+class One {
+    public function __construct() {
+    }
+}
+class Two extends One {
+    public function __construct() {
+        assert(false);
+    }
+}
+new Two();
+?>
+--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 (file)
index 0000000..8d52f64
--- /dev/null
@@ -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--
+<?php
+class One {
+    public function __construct() {
+        assert(false);
+    }
+}
+class Two extends One {}
+
+new Two();
+?>
+--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 (file)
index 0000000..0b5cf99
--- /dev/null
@@ -0,0 +1,30 @@
+--TEST--
+test overloaded __toString on custom exception
+--INI--
+zend.assertions=1
+assert.exception=1
+--FILE--
+<?php
+class MyExpectations extends AssertionException {
+    public function __toString() {
+        return sprintf(
+            "[Message]: %s", __CLASS__);
+    }
+}
+
+class One {
+    public function __construct() {
+        assert(false, (string) new MyExpectations());
+    }
+}
+class Two extends One {}
+
+new Two();
+?>
+--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 (file)
index 0000000..71e2f96
--- /dev/null
@@ -0,0 +1,21 @@
+--TEST--
+test enable/disable assertions at runtime
+--INI--
+zend.assertions=1
+assert.exception=1
+--FILE--
+<?php
+var_dump((integer)ini_get("zend.assertions"));
+ini_set("zend.assertions", 0);
+var_dump((integer)ini_get("zend.assertions"));
+assert(false);
+ini_set("zend.assertions", 1);
+var_dump((integer)ini_get("zend.assertions"));
+assert(true);
+var_dump(true);
+?>
+--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 (file)
index 0000000..cf8fa1c
--- /dev/null
@@ -0,0 +1,11 @@
+--TEST--
+test failing assertion when disabled (with return value)
+--INI--
+zend.assertions=0
+assert.exception=1
+--FILE--
+<?php
+var_dump(assert(false));
+?>
+--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 (file)
index 0000000..7cf4fb1
--- /dev/null
@@ -0,0 +1,12 @@
+--TEST--
+test failing assertion when disabled
+--INI--
+zend.assertions=0
+assert.exception=1
+--FILE--
+<?php
+assert(false);
+var_dump(true);
+?>
+--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 (file)
index 0000000..942c480
--- /dev/null
@@ -0,0 +1,295 @@
+--TEST--
+AST pretty-peinter
+--INI--
+zend.assertions=1
+assert.exception=0
+--FILE--
+<?php
+assert(0 && ($a = function () {
+       global $a, $$b;
+       static $c, $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;
+}));
+
+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 (file)
index 0000000..84e71ff
--- /dev/null
@@ -0,0 +1,24 @@
+--TEST--
+test enable/disable assertions at runtime (assertions not completely disabled)
+--INI--
+zend.assertions=0
+assert.exception=0
+--FILE--
+<?php
+ini_set("zend.assertions", 0);
+var_dump(assert(false));
+var_dump(assert(true));
+ini_set("zend.assertions", 1);
+var_dump(assert(false));
+var_dump(assert(true));
+ini_set("zend.assertions", -1);
+?>
+--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 (file)
index 0000000..bc2c4d4
--- /dev/null
@@ -0,0 +1,19 @@
+--TEST--
+test enable/disable assertions at runtime (assertions completely disabled)
+--INI--
+zend.assertions=-1
+assert.exception=0
+--FILE--
+<?php
+var_dump(assert(false));
+var_dump(assert(true));
+ini_set("zend.assertions", 0);
+ini_set("zend.assertions", 1);
+?>
+--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 (file)
index 0000000..0c9b9fe
--- /dev/null
@@ -0,0 +1,33 @@
+--TEST--
+test assertions in namespace
+--INI--
+zend.assertions=1
+assert.exception=0
+--FILE--
+<?php
+namespace Foo;
+
+ini_set("zend.assertions", 0);
+var_dump(\assert(false));
+var_dump(\assert(true));
+var_dump(assert(false));
+var_dump(assert(true));
+ini_set("zend.assertions", 1);
+var_dump(\assert(false));
+var_dump(\assert(true));
+var_dump(assert(false));
+var_dump(assert(true));
+?>
+--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 (file)
index 0000000..38e9a39
--- /dev/null
@@ -0,0 +1,19 @@
+--TEST--
+test assertions in namespace (assertions completely disabled)
+--INI--
+zend.assertions=-1
+assert.exception=0
+--FILE--
+<?php
+namespace Foo;
+
+var_dump(\assert(false));
+var_dump(\assert(true));
+var_dump(assert(false));
+var_dump(assert(true));
+?>
+--EXPECTF--
+bool(true)
+bool(true)
+bool(true)
+bool(true)
index b1177eda5ad07e8ab9c2ac8b97d900918dc879c9..397fe879baecef5da1dba0e017a23676925691e9 100644 (file)
@@ -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)
index 1a716080838e7a935372f41755b91e4ccf24bd05..ae23d83f2a327907acc6eff967d1e9ff9b7b4b11 100644 (file)
@@ -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);
index abebb19ae39022ffec67c1b48b673f283909cbd0..afd668afb87152385ea19cc373b75537bb5259b5 100644 (file)
@@ -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;
index 643f51b92663c091b036e70cb683e9a1d03bfc0a..0473fc9f262695df25d0189a0e3ebcbb845285f6 100644 (file)
@@ -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:
index 39815b400f53e3144df4c3254ab0fb6db4d8d029..2e9ffd6448f58c380700ae318a3e64dd9dca8b00 100644 (file)
@@ -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)
index 90aa7111f0b44bb23e0819473e2dddfa44f8e87e..465ecc6dd45fcd28cba5ed89b304b4fc2a31e461 100644 (file)
@@ -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,
index 991e56bc77aa514696c2b12484f8edcff98ca128..9bc531634d0c48540152417a18842cf0ef076500 100644 (file)
@@ -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",
index 367e52150c0b98c2ca001784a397fac528d5ad33..e161ee0078a0abd7f34bba7c6b896a497c745b72 100644 (file)
@@ -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
index e4d0b08a85e3eae2d134150df5a5969248800f9e..7e36c6571e4cf7d33224b8009c3b8b60b5c408bf 100644 (file)
@@ -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)
index 0922d6e8098f8ab7de2e49b0338ef465a2686a91..3e3c16558a3821c5c6d703edcef2018c2e394265 100644 (file)
@@ -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:
index dccd010ae44213908cf217d53478c17b68027223..061d2bb89f8828816c20692028db98dbd9f214f6 100644 (file)
@@ -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:
index 990b845d4344da6d47a797f83b81d95885ff6327..5bff6ab3a8dfc6b06065e0a1467725ecfe2e5794 100644 (file)
@@ -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:
index 1941a56dfaa25339eb3d13be6a2fee844978d1b0..7be076cb7f5b2db8bfe2328a5cbc9b6a1217ae65 100644 (file)
@@ -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;
                }
index 9a929be6abdb7d5c4aab2d9e5ae1cfc2b5e9e02d..c0cd5b270b7bda318174c52aa11a5a7569068469 100644 (file)
@@ -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;
                                }
index dfec951f799033e4b4e182eefbbd20f816e4fa65..0728d505c317d2fe22deade65bc3de70896ab91e 100644 (file)
@@ -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==
index 15803fa075d3b22690f9295d6ed99c9383810a27..3eb870dd67222cdde19ab7c40b37e89eae1f8656 100644 (file)
@@ -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;
index 12efef97e2452f5b9fb61c3914c491bbdf569eae..53f8d921673628e6f1b3e4df92975dfa0926b7fe 100644 (file)
@@ -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
 
index 277e5cad187e9a284145172ba3a956e90ed96194..42c59b1b9a65aefa05eba541f042b99a337d322f 100644 (file)
@@ -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
index 0ce326606fbf5ab5cf780d959191a103c0f0512e..9fcbbd5c3c292975dc1d4246caa546257464e9e2 100644 (file)
@@ -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
index 737f902d640482240afd68674a25aa291df7037a..d67d8792623717c20fc76532109e77247a2cd4ad 100644 (file)
@@ -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)
index e01c11ace989f45e7afc4239911b6cd5abe0f845..4a013283a8d7eb5aed49a10371c576ffdc096355 100644 (file)
@@ -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
index da7c3d9e0bbbcadd8edc71556381e6745268f7ac..41a7197535e26fb27228752009aa7212d0578c05 100644 (file)
@@ -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
 
index 6df2fe4e26decbb93f15d927bae6d76a811bfebd..d14397515fcbe69126f498ff22486653df6ef128 100644 (file)
@@ -10,12 +10,11 @@ error_reporting = -1
 display_errors = 1
 --FILE--
 <?php
-$sa = "0 $ 0";
-var_dump($r2 = assert($sa));
+var_dump($r2 = assert("0 $ 0"));
 --EXPECTF--
 
-Parse error: syntax error, %s in %s(3) : assert code on line 1
+Parse error: syntax error, unexpected '$', expecting ';' in %s(2) : assert code on line 1
 
 Catchable fatal error: assert(): Failure evaluating code: 
-0 $ 0 in %s on line 3
+0 $ 0 in %s on line 2