xref: /php-src/ext/standard/assert.c (revision 3d4ff5ae)
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 		zend_throw_unwind_exit();
242 		RETURN_THROWS();
243 	} else {
244 		RETURN_FALSE;
245 	}
246 }
247 /* }}} */
248 
249 /* {{{ Set/get the various assert flags */
PHP_FUNCTION(assert_options)250 PHP_FUNCTION(assert_options)
251 {
252 	zval *value = NULL;
253 	zend_long what;
254 	bool oldint;
255 	uint32_t ac = ZEND_NUM_ARGS();
256 	zend_string *key;
257 
258 	ZEND_PARSE_PARAMETERS_START(1, 2)
259 		Z_PARAM_LONG(what)
260 		Z_PARAM_OPTIONAL
261 		Z_PARAM_ZVAL(value)
262 	ZEND_PARSE_PARAMETERS_END();
263 
264 	switch (what) {
265 	case PHP_ASSERT_ACTIVE:
266 		oldint = ASSERTG(active);
267 		if (ac == 2) {
268 			zend_string *value_str = zval_try_get_string(value);
269 			if (UNEXPECTED(!value_str)) {
270 				RETURN_THROWS();
271 			}
272 
273 			key = ZSTR_INIT_LITERAL("assert.active", 0);
274 			zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
275 			zend_string_release_ex(key, 0);
276 			zend_string_release_ex(value_str, 0);
277 		}
278 		RETURN_LONG(oldint);
279 		break;
280 
281 	case PHP_ASSERT_BAIL:
282 		oldint = ASSERTG(bail);
283 		if (ac == 2) {
284 			zend_string *value_str = zval_try_get_string(value);
285 			if (UNEXPECTED(!value_str)) {
286 				RETURN_THROWS();
287 			}
288 
289 			key = ZSTR_INIT_LITERAL("assert.bail", 0);
290 			zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
291 			zend_string_release_ex(key, 0);
292 			zend_string_release_ex(value_str, 0);
293 		}
294 		RETURN_LONG(oldint);
295 		break;
296 
297 	case PHP_ASSERT_WARNING:
298 		oldint = ASSERTG(warning);
299 		if (ac == 2) {
300 			zend_string *value_str = zval_try_get_string(value);
301 			if (UNEXPECTED(!value_str)) {
302 				RETURN_THROWS();
303 			}
304 
305 			key = ZSTR_INIT_LITERAL("assert.warning", 0);
306 			zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
307 			zend_string_release_ex(key, 0);
308 			zend_string_release_ex(value_str, 0);
309 		}
310 		RETURN_LONG(oldint);
311 		break;
312 
313 	case PHP_ASSERT_CALLBACK:
314 		if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
315 			ZVAL_COPY(return_value, &ASSERTG(callback));
316 		} else if (ASSERTG(cb)) {
317 			RETVAL_STRING(ASSERTG(cb));
318 		} else {
319 			RETVAL_NULL();
320 		}
321 
322 		if (ac == 2) {
323 			zval_ptr_dtor(&ASSERTG(callback));
324 			if (Z_TYPE_P(value) == IS_NULL) {
325 				ZVAL_UNDEF(&ASSERTG(callback));
326 			} else {
327 				ZVAL_COPY(&ASSERTG(callback), value);
328 			}
329 		}
330 		return;
331 
332 	case PHP_ASSERT_EXCEPTION:
333 		oldint = ASSERTG(exception);
334 		if (ac == 2) {
335 			zend_string *val = zval_try_get_string(value);
336 			if (UNEXPECTED(!val)) {
337 				RETURN_THROWS();
338 			}
339 
340 			key = ZSTR_INIT_LITERAL("assert.exception", 0);
341 			zend_alter_ini_entry_ex(key, val, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
342 			zend_string_release_ex(val, 0);
343 			zend_string_release_ex(key, 0);
344 		}
345 		RETURN_LONG(oldint);
346 		break;
347 
348 	default:
349 		zend_argument_value_error(1, "must be an ASSERT_* constant");
350 		RETURN_THROWS();
351 	}
352 }
353 /* }}} */
354