summaryrefslogtreecommitdiff
path: root/ext/standard/assert.c
diff options
context:
space:
mode:
authorDmitry Stogov <dmitry@zend.com>2015-03-02 12:25:40 +0300
committerDmitry Stogov <dmitry@zend.com>2015-03-02 12:25:40 +0300
commit9a20323e1946dff57eae8cd054e0893aefe83092 (patch)
tree426b0a7fd399f3099ced10305bd80254c02dfc91 /ext/standard/assert.c
parent9dac9237ab46799e595838ab7821f26de13dc282 (diff)
downloadphp-git-9a20323e1946dff57eae8cd054e0893aefe83092.tar.gz
Improved assert() according to expectations RFC.
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
Diffstat (limited to 'ext/standard/assert.c')
-rw-r--r--ext/standard/assert.c72
1 files changed, 55 insertions, 17 deletions
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;