xref: /PHP-8.4/ext/standard/assert.c (revision b5c09b1a)
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 
24 ZEND_BEGIN_MODULE_GLOBALS(assert)
25 	zval callback;
26 	char *cb;
27 	bool active;
28 	bool bail;
29 	bool warning;
30 	bool exception;
31 ZEND_END_MODULE_GLOBALS(assert)
32 
33 ZEND_DECLARE_MODULE_GLOBALS(assert)
34 
35 #define ASSERTG(v) ZEND_MODULE_GLOBALS_ACCESSOR(assert, v)
36 
37 PHPAPI zend_class_entry *assertion_error_ce;
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 
php_must_emit_ini_deprecation(int stage)43 static inline bool php_must_emit_ini_deprecation(int stage)
44 {
45 	return stage != ZEND_INI_STAGE_DEACTIVATE && stage != ZEND_INI_STAGE_SHUTDOWN && stage != ZEND_INI_STAGE_ASSERT_OPTIONS;
46 }
47 
PHP_INI_MH(OnChangeCallback)48 static PHP_INI_MH(OnChangeCallback) /* {{{ */
49 {
50 	if (EG(current_execute_data)) {
51 		if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
52 			zval_ptr_dtor(&ASSERTG(callback));
53 			ZVAL_UNDEF(&ASSERTG(callback));
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 
PHP_INI_MH(OnUpdateActiveBool)80 static 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 
PHP_INI_MH(OnUpdateBailBool)90 static 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 
PHP_INI_MH(OnUpdateExceptionBool)100 static 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 
PHP_INI_MH(OnUpdateWarningBool)111 static 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 
122 PHP_INI_BEGIN()
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)
PHP_INI_END()128 PHP_INI_END()
129 
130 static 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 
PHP_MINIT_FUNCTION(assert)137 PHP_MINIT_FUNCTION(assert) /* {{{ */
138 {
139 	ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL);
140 
141 	REGISTER_INI_ENTRIES();
142 
143 	return SUCCESS;
144 }
145 /* }}} */
146 
PHP_MSHUTDOWN_FUNCTION(assert)147 PHP_MSHUTDOWN_FUNCTION(assert) /* {{{ */
148 {
149 	if (ASSERTG(cb)) {
150 		pefree(ASSERTG(cb), 1);
151 		ASSERTG(cb) = NULL;
152 	}
153 	return SUCCESS;
154 }
155 /* }}} */
156 
PHP_RSHUTDOWN_FUNCTION(assert)157 PHP_RSHUTDOWN_FUNCTION(assert) /* {{{ */
158 {
159 	if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
160 		zval_ptr_dtor(&ASSERTG(callback));
161 		ZVAL_UNDEF(&ASSERTG(callback));
162 	}
163 
164 	return SUCCESS;
165 }
166 /* }}} */
167 
PHP_MINFO_FUNCTION(assert)168 PHP_MINFO_FUNCTION(assert) /* {{{ */
169 {
170 	DISPLAY_INI_ENTRIES();
171 }
172 /* }}} */
173 
174 /* {{{ Checks if assertion is false */
PHP_FUNCTION(assert)175 PHP_FUNCTION(assert)
176 {
177 	zval *assertion;
178 	zend_string *description_str = NULL;
179 	zend_object *description_obj = NULL;
180 
181 	if (!ASSERTG(active)) {
182 		RETURN_TRUE;
183 	}
184 
185 	ZEND_PARSE_PARAMETERS_START(1, 2)
186 		Z_PARAM_ZVAL(assertion)
187 		Z_PARAM_OPTIONAL
188 		Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(description_obj, zend_ce_throwable, description_str)
189 	ZEND_PARSE_PARAMETERS_END();
190 
191 	if (zend_is_true(assertion)) {
192 		RETURN_TRUE;
193 	}
194 
195 	if (description_obj) {
196 		GC_ADDREF(description_obj);
197 		zend_throw_exception_internal(description_obj);
198 		RETURN_THROWS();
199 	}
200 
201 	if (Z_TYPE(ASSERTG(callback)) == IS_UNDEF && ASSERTG(cb)) {
202 		ZVAL_STRING(&ASSERTG(callback), ASSERTG(cb));
203 	}
204 
205 	if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
206 		zval args[4];
207 		zval retval;
208 		uint32_t lineno = zend_get_executed_lineno();
209 		zend_string *filename = zend_get_executed_filename_ex();
210 		if (UNEXPECTED(!filename)) {
211 			filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED);
212 		}
213 
214 		ZVAL_STR(&args[0], filename);
215 		ZVAL_LONG(&args[1], lineno);
216 		ZVAL_NULL(&args[2]);
217 
218 		ZVAL_FALSE(&retval);
219 
220 		if (description_str) {
221 			ZVAL_STR(&args[3], description_str);
222 			call_user_function(NULL, NULL, &ASSERTG(callback), &retval, 4, args);
223 		} else {
224 			call_user_function(NULL, NULL, &ASSERTG(callback), &retval, 3, args);
225 		}
226 
227 		zval_ptr_dtor(&retval);
228 	}
229 
230 	if (ASSERTG(exception)) {
231 		zend_throw_exception(assertion_error_ce, description_str ? ZSTR_VAL(description_str) : NULL, E_ERROR);
232 		if (ASSERTG(bail)) {
233 			/* When bail is turned on, the exception will not be caught. */
234 			zend_exception_error(EG(exception), E_ERROR);
235 		}
236 	} else if (ASSERTG(warning)) {
237 		php_error_docref(NULL, E_WARNING, "%s failed", description_str ? ZSTR_VAL(description_str) : "Assertion");
238 	}
239 
240 	if (ASSERTG(bail)) {
241 		if (EG(exception)) {
242 			/* The callback might have thrown. Use E_WARNING to print the
243 			 * exception so we can avoid bailout and use unwind_exit. */
244 			zend_exception_error(EG(exception), E_WARNING);
245 		}
246 		zend_throw_unwind_exit();
247 		RETURN_THROWS();
248 	} else {
249 		RETURN_FALSE;
250 	}
251 }
252 /* }}} */
253 
254 /* {{{ Set/get the various assert flags */
PHP_FUNCTION(assert_options)255 PHP_FUNCTION(assert_options)
256 {
257 	zval *value = NULL;
258 	zend_long what;
259 	bool oldint;
260 	uint32_t ac = ZEND_NUM_ARGS();
261 	zend_string *key;
262 
263 	ZEND_PARSE_PARAMETERS_START(1, 2)
264 		Z_PARAM_LONG(what)
265 		Z_PARAM_OPTIONAL
266 		Z_PARAM_ZVAL(value)
267 	ZEND_PARSE_PARAMETERS_END();
268 
269 	switch (what) {
270 	case PHP_ASSERT_ACTIVE:
271 		oldint = ASSERTG(active);
272 		if (ac == 2) {
273 			zend_string *value_str = zval_try_get_string(value);
274 			if (UNEXPECTED(!value_str)) {
275 				RETURN_THROWS();
276 			}
277 
278 			key = ZSTR_INIT_LITERAL("assert.active", 0);
279 			zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
280 			zend_string_release_ex(key, 0);
281 			zend_string_release_ex(value_str, 0);
282 		}
283 		RETURN_LONG(oldint);
284 		break;
285 
286 	case PHP_ASSERT_BAIL:
287 		oldint = ASSERTG(bail);
288 		if (ac == 2) {
289 			zend_string *value_str = zval_try_get_string(value);
290 			if (UNEXPECTED(!value_str)) {
291 				RETURN_THROWS();
292 			}
293 
294 			key = ZSTR_INIT_LITERAL("assert.bail", 0);
295 			zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
296 			zend_string_release_ex(key, 0);
297 			zend_string_release_ex(value_str, 0);
298 		}
299 		RETURN_LONG(oldint);
300 		break;
301 
302 	case PHP_ASSERT_WARNING:
303 		oldint = ASSERTG(warning);
304 		if (ac == 2) {
305 			zend_string *value_str = zval_try_get_string(value);
306 			if (UNEXPECTED(!value_str)) {
307 				RETURN_THROWS();
308 			}
309 
310 			key = ZSTR_INIT_LITERAL("assert.warning", 0);
311 			zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
312 			zend_string_release_ex(key, 0);
313 			zend_string_release_ex(value_str, 0);
314 		}
315 		RETURN_LONG(oldint);
316 		break;
317 
318 	case PHP_ASSERT_CALLBACK:
319 		if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
320 			ZVAL_COPY(return_value, &ASSERTG(callback));
321 		} else if (ASSERTG(cb)) {
322 			RETVAL_STRING(ASSERTG(cb));
323 		} else {
324 			RETVAL_NULL();
325 		}
326 
327 		if (ac == 2) {
328 			zval_ptr_dtor(&ASSERTG(callback));
329 			if (Z_TYPE_P(value) == IS_NULL) {
330 				ZVAL_UNDEF(&ASSERTG(callback));
331 			} else {
332 				ZVAL_COPY(&ASSERTG(callback), value);
333 			}
334 		}
335 		return;
336 
337 	case PHP_ASSERT_EXCEPTION:
338 		oldint = ASSERTG(exception);
339 		if (ac == 2) {
340 			zend_string *val = zval_try_get_string(value);
341 			if (UNEXPECTED(!val)) {
342 				RETURN_THROWS();
343 			}
344 
345 			key = ZSTR_INIT_LITERAL("assert.exception", 0);
346 			zend_alter_ini_entry_ex(key, val, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
347 			zend_string_release_ex(val, 0);
348 			zend_string_release_ex(key, 0);
349 		}
350 		RETURN_LONG(oldint);
351 		break;
352 
353 	default:
354 		zend_argument_value_error(1, "must be an ASSERT_* constant");
355 		RETURN_THROWS();
356 	}
357 }
358 /* }}} */
359