1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Thies C. Arntzen <thies@thieso.net> |
+----------------------------------------------------------------------+
*/
/* {{{ includes */
#include "php.h"
#include "php_assert.h"
#include "php_ini.h"
#include "zend_exceptions.h"
/* }}} */
ZEND_BEGIN_MODULE_GLOBALS(assert)
zval callback;
char *cb;
bool active;
bool bail;
bool warning;
bool exception;
ZEND_END_MODULE_GLOBALS(assert)
ZEND_DECLARE_MODULE_GLOBALS(assert)
#define ASSERTG(v) ZEND_MODULE_GLOBALS_ACCESSOR(assert, v)
PHPAPI zend_class_entry *assertion_error_ce;
/* Hack to pass a custom stage for the our OnModify handler so that a deprecation warning does not get emitted
* when an option is modified via assert_option() function */
#define ZEND_INI_STAGE_ASSERT_OPTIONS (1<<6)
static inline bool php_must_emit_ini_deprecation(int stage)
{
return stage != ZEND_INI_STAGE_DEACTIVATE && stage != ZEND_INI_STAGE_SHUTDOWN && stage != ZEND_INI_STAGE_ASSERT_OPTIONS;
}
static PHP_INI_MH(OnChangeCallback) /* {{{ */
{
if (EG(current_execute_data)) {
if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
zval_ptr_dtor(&ASSERTG(callback));
ZVAL_UNDEF(&ASSERTG(callback));
}
if (new_value && (Z_TYPE(ASSERTG(callback)) != IS_UNDEF || ZSTR_LEN(new_value))) {
if (php_must_emit_ini_deprecation(stage)) {
php_error_docref(NULL, E_DEPRECATED, "assert.callback INI setting is deprecated");
}
ZVAL_STR_COPY(&ASSERTG(callback), new_value);
}
} else {
if (ASSERTG(cb)) {
pefree(ASSERTG(cb), 1);
}
if (new_value && ZSTR_LEN(new_value)) {
if (php_must_emit_ini_deprecation(stage)) {
php_error_docref(NULL, E_DEPRECATED, "assert.callback INI setting is deprecated");
}
ASSERTG(cb) = pemalloc(ZSTR_LEN(new_value) + 1, 1);
memcpy(ASSERTG(cb), ZSTR_VAL(new_value), ZSTR_LEN(new_value));
ASSERTG(cb)[ZSTR_LEN(new_value)] = '\0';
} else {
ASSERTG(cb) = NULL;
}
}
return SUCCESS;
}
/* }}} */
static PHP_INI_MH(OnUpdateActiveBool)
{
bool *p = (bool *) ZEND_INI_GET_ADDR();
*p = zend_ini_parse_bool(new_value);
if (php_must_emit_ini_deprecation(stage) && !*p) {
php_error_docref(NULL, E_DEPRECATED, "assert.active INI setting is deprecated");
}
return SUCCESS;
}
static PHP_INI_MH(OnUpdateBailBool)
{
bool *p = (bool *) ZEND_INI_GET_ADDR();
*p = zend_ini_parse_bool(new_value);
if (php_must_emit_ini_deprecation(stage) && *p) {
php_error_docref(NULL, E_DEPRECATED, "assert.bail INI setting is deprecated");
}
return SUCCESS;
}
static PHP_INI_MH(OnUpdateExceptionBool)
{
bool *p = (bool *) ZEND_INI_GET_ADDR();
*p = zend_ini_parse_bool(new_value);
if (php_must_emit_ini_deprecation(stage) && !*p) {
php_error_docref(NULL, E_DEPRECATED, "assert.exception INI setting is deprecated");
}
return SUCCESS;
}
static PHP_INI_MH(OnUpdateWarningBool)
{
bool *p = (bool *) ZEND_INI_GET_ADDR();
*p = zend_ini_parse_bool(new_value);
if (php_must_emit_ini_deprecation(stage) && !*p) {
php_error_docref(NULL, E_DEPRECATED, "assert.warning INI setting is deprecated");
}
return SUCCESS;
}
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("assert.active", "1", PHP_INI_ALL, OnUpdateActiveBool, active, zend_assert_globals, assert_globals)
STD_PHP_INI_BOOLEAN("assert.bail", "0", PHP_INI_ALL, OnUpdateBailBool, bail, zend_assert_globals, assert_globals)
STD_PHP_INI_BOOLEAN("assert.warning", "1", PHP_INI_ALL, OnUpdateWarningBool, warning, zend_assert_globals, assert_globals)
PHP_INI_ENTRY("assert.callback", NULL, PHP_INI_ALL, OnChangeCallback)
STD_PHP_INI_BOOLEAN("assert.exception", "1", PHP_INI_ALL, OnUpdateExceptionBool, exception, zend_assert_globals, assert_globals)
PHP_INI_END()
static void php_assert_init_globals(zend_assert_globals *assert_globals_p) /* {{{ */
{
ZVAL_UNDEF(&assert_globals_p->callback);
assert_globals_p->cb = NULL;
}
/* }}} */
PHP_MINIT_FUNCTION(assert) /* {{{ */
{
ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL);
REGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
PHP_MSHUTDOWN_FUNCTION(assert) /* {{{ */
{
if (ASSERTG(cb)) {
pefree(ASSERTG(cb), 1);
ASSERTG(cb) = NULL;
}
return SUCCESS;
}
/* }}} */
PHP_RSHUTDOWN_FUNCTION(assert) /* {{{ */
{
if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
zval_ptr_dtor(&ASSERTG(callback));
ZVAL_UNDEF(&ASSERTG(callback));
}
return SUCCESS;
}
/* }}} */
PHP_MINFO_FUNCTION(assert) /* {{{ */
{
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ Checks if assertion is false */
PHP_FUNCTION(assert)
{
zval *assertion;
zend_string *description_str = NULL;
zend_object *description_obj = NULL;
/* EG(assertions) <= 0 is only reachable by dynamic calls to assert(),
* since calls known at compile time will skip the entire call when
* assertions are disabled.
*/
if (!ASSERTG(active) || EG(assertions) <= 0) {
RETURN_TRUE;
}
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ZVAL(assertion)
Z_PARAM_OPTIONAL
Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(description_obj, zend_ce_throwable, description_str)
ZEND_PARSE_PARAMETERS_END();
if (zend_is_true(assertion)) {
RETURN_TRUE;
}
if (description_obj) {
GC_ADDREF(description_obj);
zend_throw_exception_internal(description_obj);
RETURN_THROWS();
}
if (Z_TYPE(ASSERTG(callback)) == IS_UNDEF && ASSERTG(cb)) {
ZVAL_STRING(&ASSERTG(callback), ASSERTG(cb));
}
if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
zval args[4];
zval retval;
uint32_t lineno = zend_get_executed_lineno();
zend_string *filename = zend_get_executed_filename_ex();
if (UNEXPECTED(!filename)) {
filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED);
}
ZVAL_STR(&args[0], filename);
ZVAL_LONG(&args[1], lineno);
ZVAL_NULL(&args[2]);
ZVAL_FALSE(&retval);
if (description_str) {
ZVAL_STR(&args[3], description_str);
call_user_function(NULL, NULL, &ASSERTG(callback), &retval, 4, args);
} else {
call_user_function(NULL, NULL, &ASSERTG(callback), &retval, 3, args);
}
zval_ptr_dtor(&retval);
}
if (ASSERTG(exception)) {
zend_throw_exception(assertion_error_ce, description_str ? ZSTR_VAL(description_str) : NULL, E_ERROR);
if (ASSERTG(bail)) {
/* When bail is turned on, the exception will not be caught. */
zend_exception_error(EG(exception), E_ERROR);
}
} else if (ASSERTG(warning)) {
php_error_docref(NULL, E_WARNING, "%s failed", description_str ? ZSTR_VAL(description_str) : "Assertion");
}
if (ASSERTG(bail)) {
if (EG(exception)) {
/* The callback might have thrown. Use E_WARNING to print the
* exception so we can avoid bailout and use unwind_exit. */
zend_exception_error(EG(exception), E_WARNING);
}
zend_throw_unwind_exit();
RETURN_THROWS();
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ Set/get the various assert flags */
PHP_FUNCTION(assert_options)
{
zval *value = NULL;
zend_long what;
bool oldint;
uint32_t ac = ZEND_NUM_ARGS();
zend_string *key;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_LONG(what)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(value)
ZEND_PARSE_PARAMETERS_END();
switch (what) {
case PHP_ASSERT_ACTIVE:
oldint = ASSERTG(active);
if (ac == 2) {
zend_string *value_str = zval_try_get_string(value);
if (UNEXPECTED(!value_str)) {
RETURN_THROWS();
}
key = ZSTR_INIT_LITERAL("assert.active", 0);
zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
zend_string_release_ex(key, 0);
zend_string_release_ex(value_str, 0);
}
RETURN_LONG(oldint);
break;
case PHP_ASSERT_BAIL:
oldint = ASSERTG(bail);
if (ac == 2) {
zend_string *value_str = zval_try_get_string(value);
if (UNEXPECTED(!value_str)) {
RETURN_THROWS();
}
key = ZSTR_INIT_LITERAL("assert.bail", 0);
zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
zend_string_release_ex(key, 0);
zend_string_release_ex(value_str, 0);
}
RETURN_LONG(oldint);
break;
case PHP_ASSERT_WARNING:
oldint = ASSERTG(warning);
if (ac == 2) {
zend_string *value_str = zval_try_get_string(value);
if (UNEXPECTED(!value_str)) {
RETURN_THROWS();
}
key = ZSTR_INIT_LITERAL("assert.warning", 0);
zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
zend_string_release_ex(key, 0);
zend_string_release_ex(value_str, 0);
}
RETURN_LONG(oldint);
break;
case PHP_ASSERT_CALLBACK:
if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
ZVAL_COPY(return_value, &ASSERTG(callback));
} else if (ASSERTG(cb)) {
RETVAL_STRING(ASSERTG(cb));
} else {
RETVAL_NULL();
}
if (ac == 2) {
zval_ptr_dtor(&ASSERTG(callback));
if (Z_TYPE_P(value) == IS_NULL) {
ZVAL_UNDEF(&ASSERTG(callback));
} else {
ZVAL_COPY(&ASSERTG(callback), value);
}
}
return;
case PHP_ASSERT_EXCEPTION:
oldint = ASSERTG(exception);
if (ac == 2) {
zend_string *val = zval_try_get_string(value);
if (UNEXPECTED(!val)) {
RETURN_THROWS();
}
key = ZSTR_INIT_LITERAL("assert.exception", 0);
zend_alter_ini_entry_ex(key, val, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
zend_string_release_ex(val, 0);
zend_string_release_ex(key, 0);
}
RETURN_LONG(oldint);
break;
default:
zend_argument_value_error(1, "must be an ASSERT_* constant");
RETURN_THROWS();
}
}
/* }}} */
|