php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
assert.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Thies C. Arntzen <thies@thieso.net> |
14 +----------------------------------------------------------------------+
15*/
16
17/* {{{ includes */
18#include "php.h"
19#include "php_assert.h"
20#include "php_ini.h"
21#include "zend_exceptions.h"
22/* }}} */
23
26 char *cb;
27 bool active;
28 bool bail;
29 bool warning;
32
34
35#define ASSERTG(v) ZEND_MODULE_GLOBALS_ACCESSOR(assert, v)
36
38
39/* Hack to pass a custom stage for the our OnModify handler so that a deprecation warning does not get emitted
40 * when an option is modified via assert_option() function */
41#define ZEND_INI_STAGE_ASSERT_OPTIONS (1<<6)
42
43static inline bool php_must_emit_ini_deprecation(int stage)
44{
46}
47
48static PHP_INI_MH(OnChangeCallback) /* {{{ */
49{
50 if (EG(current_execute_data)) {
54 }
55 if (new_value && (Z_TYPE(ASSERTG(callback)) != IS_UNDEF || ZSTR_LEN(new_value))) {
56 if (php_must_emit_ini_deprecation(stage)) {
57 php_error_docref(NULL, E_DEPRECATED, "assert.callback INI setting is deprecated");
58 }
59 ZVAL_STR_COPY(&ASSERTG(callback), new_value);
60 }
61 } else {
62 if (ASSERTG(cb)) {
63 pefree(ASSERTG(cb), 1);
64 }
65 if (new_value && ZSTR_LEN(new_value)) {
66 if (php_must_emit_ini_deprecation(stage)) {
67 php_error_docref(NULL, E_DEPRECATED, "assert.callback INI setting is deprecated");
68 }
69 ASSERTG(cb) = pemalloc(ZSTR_LEN(new_value) + 1, 1);
70 memcpy(ASSERTG(cb), ZSTR_VAL(new_value), ZSTR_LEN(new_value));
71 ASSERTG(cb)[ZSTR_LEN(new_value)] = '\0';
72 } else {
73 ASSERTG(cb) = NULL;
74 }
75 }
76 return SUCCESS;
77}
78/* }}} */
79
80static PHP_INI_MH(OnUpdateActiveBool)
81{
82 bool *p = (bool *) ZEND_INI_GET_ADDR();
83 *p = zend_ini_parse_bool(new_value);
84 if (php_must_emit_ini_deprecation(stage) && !*p) {
85 php_error_docref(NULL, E_DEPRECATED, "assert.active INI setting is deprecated");
86 }
87 return SUCCESS;
88}
89
90static PHP_INI_MH(OnUpdateBailBool)
91{
92 bool *p = (bool *) ZEND_INI_GET_ADDR();
93 *p = zend_ini_parse_bool(new_value);
94 if (php_must_emit_ini_deprecation(stage) && *p) {
95 php_error_docref(NULL, E_DEPRECATED, "assert.bail INI setting is deprecated");
96 }
97 return SUCCESS;
98}
99
100static PHP_INI_MH(OnUpdateExceptionBool)
101{
102 bool *p = (bool *) ZEND_INI_GET_ADDR();
103 *p = zend_ini_parse_bool(new_value);
104 if (php_must_emit_ini_deprecation(stage) && !*p) {
105 php_error_docref(NULL, E_DEPRECATED, "assert.exception INI setting is deprecated");
106 }
107 return SUCCESS;
108}
109
110
111static PHP_INI_MH(OnUpdateWarningBool)
112{
113 bool *p = (bool *) ZEND_INI_GET_ADDR();
114 *p = zend_ini_parse_bool(new_value);
115 if (php_must_emit_ini_deprecation(stage) && !*p) {
116 php_error_docref(NULL, E_DEPRECATED, "assert.warning INI setting is deprecated");
117 }
118 return SUCCESS;
119}
120
121
123 STD_PHP_INI_BOOLEAN("assert.active", "1", PHP_INI_ALL, OnUpdateActiveBool, active, zend_assert_globals, assert_globals)
124 STD_PHP_INI_BOOLEAN("assert.bail", "0", PHP_INI_ALL, OnUpdateBailBool, bail, zend_assert_globals, assert_globals)
125 STD_PHP_INI_BOOLEAN("assert.warning", "1", PHP_INI_ALL, OnUpdateWarningBool, warning, zend_assert_globals, assert_globals)
126 PHP_INI_ENTRY("assert.callback", NULL, PHP_INI_ALL, OnChangeCallback)
127 STD_PHP_INI_BOOLEAN("assert.exception", "1", PHP_INI_ALL, OnUpdateExceptionBool, exception, zend_assert_globals, assert_globals)
129
130static void php_assert_init_globals(zend_assert_globals *assert_globals_p) /* {{{ */
131{
132 ZVAL_UNDEF(&assert_globals_p->callback);
133 assert_globals_p->cb = NULL;
134}
135/* }}} */
136
137PHP_MINIT_FUNCTION(assert) /* {{{ */
138{
139 ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL);
140
142
143 return SUCCESS;
144}
145/* }}} */
146
148{
149 if (ASSERTG(cb)) {
150 pefree(ASSERTG(cb), 1);
151 ASSERTG(cb) = NULL;
152 }
153 return SUCCESS;
154}
155/* }}} */
156
158{
159 if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
162 }
163
164 return SUCCESS;
165}
166/* }}} */
167
168PHP_MINFO_FUNCTION(assert) /* {{{ */
169{
171}
172/* }}} */
173
174/* {{{ Checks if assertion is false */
176{
177 zval *assertion;
178 zend_string *description_str = NULL;
179 zend_object *description_obj = NULL;
180
181 /* EG(assertions) <= 0 is only reachable by dynamic calls to assert(),
182 * since calls known at compile time will skip the entire call when
183 * assertions are disabled.
184 */
185 if (!ASSERTG(active) || EG(assertions) <= 0) {
187 }
188
190 Z_PARAM_ZVAL(assertion)
192 Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(description_obj, zend_ce_throwable, description_str)
194
195 if (zend_is_true(assertion)) {
197 }
198
199 if (description_obj) {
200 GC_ADDREF(description_obj);
201 zend_throw_exception_internal(description_obj);
203 }
204
205 if (Z_TYPE(ASSERTG(callback)) == IS_UNDEF && ASSERTG(cb)) {
207 }
208
209 if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
210 zval args[4];
211 zval retval;
212 uint32_t lineno = zend_get_executed_lineno();
214 if (UNEXPECTED(!filename)) {
215 filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED);
216 }
217
218 ZVAL_STR(&args[0], filename);
219 ZVAL_LONG(&args[1], lineno);
220 ZVAL_NULL(&args[2]);
221
223
224 if (description_str) {
225 ZVAL_STR(&args[3], description_str);
227 } else {
229 }
230
232 }
233
234 if (ASSERTG(exception)) {
235 zend_throw_exception(assertion_error_ce, description_str ? ZSTR_VAL(description_str) : NULL, E_ERROR);
236 if (ASSERTG(bail)) {
237 /* When bail is turned on, the exception will not be caught. */
239 }
240 } else if (ASSERTG(warning)) {
241 php_error_docref(NULL, E_WARNING, "%s failed", description_str ? ZSTR_VAL(description_str) : "Assertion");
242 }
243
244 if (ASSERTG(bail)) {
245 if (EG(exception)) {
246 /* The callback might have thrown. Use E_WARNING to print the
247 * exception so we can avoid bailout and use unwind_exit. */
249 }
252 } else {
254 }
255}
256/* }}} */
257
258/* {{{ Set/get the various assert flags */
260{
261 zval *value = NULL;
262 zend_long what;
263 bool oldint;
264 uint32_t ac = ZEND_NUM_ARGS();
266
268 Z_PARAM_LONG(what)
272
273 switch (what) {
275 oldint = ASSERTG(active);
276 if (ac == 2) {
277 zend_string *value_str = zval_try_get_string(value);
278 if (UNEXPECTED(!value_str)) {
280 }
281
282 key = ZSTR_INIT_LITERAL("assert.active", 0);
285 zend_string_release_ex(value_str, 0);
286 }
287 RETURN_LONG(oldint);
288 break;
289
290 case PHP_ASSERT_BAIL:
291 oldint = ASSERTG(bail);
292 if (ac == 2) {
293 zend_string *value_str = zval_try_get_string(value);
294 if (UNEXPECTED(!value_str)) {
296 }
297
298 key = ZSTR_INIT_LITERAL("assert.bail", 0);
301 zend_string_release_ex(value_str, 0);
302 }
303 RETURN_LONG(oldint);
304 break;
305
307 oldint = ASSERTG(warning);
308 if (ac == 2) {
309 zend_string *value_str = zval_try_get_string(value);
310 if (UNEXPECTED(!value_str)) {
312 }
313
314 key = ZSTR_INIT_LITERAL("assert.warning", 0);
317 zend_string_release_ex(value_str, 0);
318 }
319 RETURN_LONG(oldint);
320 break;
321
323 if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
325 } else if (ASSERTG(cb)) {
327 } else {
328 RETVAL_NULL();
329 }
330
331 if (ac == 2) {
333 if (Z_TYPE_P(value) == IS_NULL) {
335 } else {
337 }
338 }
339 return;
340
342 oldint = ASSERTG(exception);
343 if (ac == 2) {
344 zend_string *val = zval_try_get_string(value);
345 if (UNEXPECTED(!val)) {
347 }
348
349 key = ZSTR_INIT_LITERAL("assert.exception", 0);
353 }
354 RETURN_LONG(oldint);
355 break;
356
357 default:
358 zend_argument_value_error(1, "must be an ASSERT_* constant");
360 }
361}
362/* }}} */
bool warning
Definition assert.c:29
#define ASSERTG(v)
Definition assert.c:35
REGISTER_INI_ENTRIES()
char * cb
Definition assert.c:26
bool bail
Definition assert.c:28
PHPAPI zend_class_entry * assertion_error_ce
Definition assert.c:37
bool exception
Definition assert.c:30
#define ZEND_INI_STAGE_ASSERT_OPTIONS
Definition assert.c:41
zval callback
Definition assert.c:25
assert_options(int $option, mixed $value=UNKNOWN)
assert(mixed $assertion, Throwable|string|null $description=null)
memcpy(ptr1, ptr2, size)
zval * val
Definition ffi.c:4262
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
Definition main.c:1173
#define PHP_FUNCTION
Definition php.h:364
#define PHP_MSHUTDOWN_FUNCTION
Definition php.h:401
#define PHP_MINIT_FUNCTION
Definition php.h:400
#define PHP_MINFO_FUNCTION
Definition php.h:404
#define PHP_RSHUTDOWN_FUNCTION
Definition php.h:403
#define PHPAPI
Definition php.h:71
@ PHP_ASSERT_ACTIVE
Definition php_assert.h:27
@ PHP_ASSERT_CALLBACK
Definition php_assert.h:28
@ PHP_ASSERT_EXCEPTION
Definition php_assert.h:31
@ PHP_ASSERT_WARNING
Definition php_assert.h:30
@ PHP_ASSERT_BAIL
Definition php_assert.h:29
#define PHP_INI_ALL
Definition php_ini.h:45
#define PHP_INI_USER
Definition php_ini.h:41
#define PHP_INI_BEGIN
Definition php_ini.h:52
#define PHP_INI_ENTRY
Definition php_ini.h:62
#define STD_PHP_INI_BOOLEAN
Definition php_ini.h:66
#define PHP_INI_MH
Definition php_ini.h:49
#define PHP_INI_END
Definition php_ini.h:53
php_output_handler * active
Definition php_output.h:140
unsigned char key[REFLECTION_KEY_LEN]
p
Definition session.c:1105
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:433
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define RETURN_FALSE
Definition zend_API.h:1058
#define RETVAL_STRING(s)
Definition zend_API.h:1017
#define ZEND_INIT_MODULE_GLOBALS(module_name, globals_ctor, globals_dtor)
Definition zend_API.h:272
#define ZVAL_STRING(z, s)
Definition zend_API.h:956
#define ZEND_DECLARE_MODULE_GLOBALS(module_name)
Definition zend_API.h:268
#define Z_PARAM_OPTIONAL
Definition zend_API.h:1667
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define ZEND_END_MODULE_GLOBALS(module_name)
Definition zend_API.h:248
#define Z_PARAM_LONG(dest)
Definition zend_API.h:1896
#define RETURN_LONG(l)
Definition zend_API.h:1037
#define RETVAL_NULL()
Definition zend_API.h:1010
#define RETURN_THROWS()
Definition zend_API.h:1060
#define Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(destination_object, base_ce, destination_string)
Definition zend_API.h:1791
#define call_user_function(function_table, object, function_name, retval_ptr, param_count, params)
Definition zend_API.h:687
#define Z_PARAM_ZVAL(dest)
Definition zend_API.h:2100
#define RETURN_TRUE
Definition zend_API.h:1059
#define ZEND_BEGIN_MODULE_GLOBALS(module_name)
Definition zend_API.h:246
#define pefree(ptr, persistent)
Definition zend_alloc.h:191
#define pemalloc(size, persistent)
Definition zend_alloc.h:189
struct _zval_struct zval
zend_string_release_ex(func->internal_function.function_name, 0)
zval * args
#define E_ERROR
Definition zend_errors.h:23
#define E_WARNING
Definition zend_errors.h:24
#define E_DEPRECATED
Definition zend_errors.h:37
ZEND_API ZEND_COLD void zend_throw_exception_internal(zend_object *exception)
ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *ex, int severity)
ZEND_API ZEND_COLD zend_object * zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code)
ZEND_API ZEND_COLD void zend_throw_unwind_exit(void)
ZEND_API zend_class_entry * zend_ce_throwable
ZEND_API uint32_t zend_get_executed_lineno(void)
ZEND_API zend_string * zend_get_executed_filename_ex(void)
#define EG(v)
ZEND_API zend_result zend_alter_ini_entry_ex(zend_string *name, zend_string *new_value, int modify_type, int stage, bool force_change)
Definition zend_ini.c:356
ZEND_API bool zend_ini_parse_bool(zend_string *str)
Definition zend_ini.c:573
#define ZEND_INI_STAGE_SHUTDOWN
Definition zend_ini.h:228
#define DISPLAY_INI_ENTRIES()
Definition zend_ini.h:205
#define ZEND_INI_STAGE_DEACTIVATE
Definition zend_ini.h:230
#define ZEND_INI_GET_ADDR()
Definition zend_ini.h:259
int32_t zend_long
Definition zend_long.h:42
struct _zend_string zend_string
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op)
#define UNEXPECTED(condition)
struct _zend_class_entry zend_class_entry
struct _zend_object zend_object
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_INIT_LITERAL(s, persistent)
#define ZSTR_KNOWN(idx)
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define ZVAL_STR(z, s)
#define ZVAL_FALSE(z)
#define ZVAL_UNDEF(z)
#define IS_UNDEF
Definition zend_types.h:600
#define ZVAL_NULL(z)
#define ZVAL_LONG(z, l)
#define ZVAL_STR_COPY(z, s)
#define GC_ADDREF(p)
Definition zend_types.h:709
#define IS_NULL
Definition zend_types.h:601
#define ZVAL_COPY(z, v)
#define Z_TYPE(zval)
Definition zend_types.h:659
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
zval retval
zval * return_value
value