xref: /PHP-7.2/ext/standard/assert.c (revision 60a69dae)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Thies C. Arntzen <thies@thieso.net>                          |
16    +----------------------------------------------------------------------+
17 */
18 
19 /* $Id$ */
20 
21 /* {{{ includes */
22 #include "php.h"
23 #include "php_assert.h"
24 #include "php_ini.h"
25 #include "zend_exceptions.h"
26 /* }}} */
27 
28 ZEND_BEGIN_MODULE_GLOBALS(assert)
29 	zval callback;
30 	char *cb;
31 	zend_bool active;
32 	zend_bool bail;
33 	zend_bool warning;
34 	zend_bool quiet_eval;
35 	zend_bool exception;
36 ZEND_END_MODULE_GLOBALS(assert)
37 
38 ZEND_DECLARE_MODULE_GLOBALS(assert)
39 
40 static zend_class_entry *assertion_error_ce;
41 
42 #define ASSERTG(v) ZEND_MODULE_GLOBALS_ACCESSOR(assert, v)
43 
44 #define SAFE_STRING(s) ((s)?(s):"")
45 
46 enum {
47 	ASSERT_ACTIVE=1,
48 	ASSERT_CALLBACK,
49 	ASSERT_BAIL,
50 	ASSERT_WARNING,
51 	ASSERT_QUIET_EVAL,
52 	ASSERT_EXCEPTION
53 };
54 
PHP_INI_MH(OnChangeCallback)55 static PHP_INI_MH(OnChangeCallback) /* {{{ */
56 {
57 	if (EG(current_execute_data)) {
58 		if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
59 			zval_ptr_dtor(&ASSERTG(callback));
60 			ZVAL_UNDEF(&ASSERTG(callback));
61 		}
62 		if (new_value && (Z_TYPE(ASSERTG(callback)) != IS_UNDEF || ZSTR_LEN(new_value))) {
63 			ZVAL_STR_COPY(&ASSERTG(callback), new_value);
64 		}
65 	} else {
66 		if (ASSERTG(cb)) {
67 			pefree(ASSERTG(cb), 1);
68 		}
69 		if (new_value && ZSTR_LEN(new_value)) {
70 			ASSERTG(cb) = pemalloc(ZSTR_LEN(new_value) + 1, 1);
71 			memcpy(ASSERTG(cb), ZSTR_VAL(new_value), ZSTR_LEN(new_value));
72 			ASSERTG(cb)[ZSTR_LEN(new_value)] = '\0';
73 		} else {
74 			ASSERTG(cb) = NULL;
75 		}
76 	}
77 	return SUCCESS;
78 }
79 /* }}} */
80 
81 PHP_INI_BEGIN()
82 	 STD_PHP_INI_ENTRY("assert.active",		"1",	PHP_INI_ALL,	OnUpdateBool,		active,	 			zend_assert_globals,		assert_globals)
83 	 STD_PHP_INI_ENTRY("assert.bail",		"0",	PHP_INI_ALL,	OnUpdateBool,		bail,	 			zend_assert_globals,		assert_globals)
84 	 STD_PHP_INI_ENTRY("assert.warning",	"1",	PHP_INI_ALL,	OnUpdateBool,		warning, 			zend_assert_globals,		assert_globals)
85 	 PHP_INI_ENTRY("assert.callback",		NULL,	PHP_INI_ALL,	OnChangeCallback)
86 	 STD_PHP_INI_ENTRY("assert.quiet_eval", "0",	PHP_INI_ALL,	OnUpdateBool,		quiet_eval,		 	zend_assert_globals,		assert_globals)
87 	 STD_PHP_INI_ENTRY("assert.exception",	"0",	PHP_INI_ALL,	OnUpdateBool,		exception, 			zend_assert_globals,		assert_globals)
PHP_INI_END()88 PHP_INI_END()
89 
90 static void php_assert_init_globals(zend_assert_globals *assert_globals_p) /* {{{ */
91 {
92 	ZVAL_UNDEF(&assert_globals_p->callback);
93 	assert_globals_p->cb = NULL;
94 }
95 /* }}} */
96 
PHP_MINIT_FUNCTION(assert)97 PHP_MINIT_FUNCTION(assert) /* {{{ */
98 {
99 	zend_class_entry ce;
100 
101 	ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL);
102 
103 	REGISTER_INI_ENTRIES();
104 
105 	REGISTER_LONG_CONSTANT("ASSERT_ACTIVE", ASSERT_ACTIVE, CONST_CS|CONST_PERSISTENT);
106 	REGISTER_LONG_CONSTANT("ASSERT_CALLBACK", ASSERT_CALLBACK, CONST_CS|CONST_PERSISTENT);
107 	REGISTER_LONG_CONSTANT("ASSERT_BAIL", ASSERT_BAIL, CONST_CS|CONST_PERSISTENT);
108 	REGISTER_LONG_CONSTANT("ASSERT_WARNING", ASSERT_WARNING, CONST_CS|CONST_PERSISTENT);
109 	REGISTER_LONG_CONSTANT("ASSERT_QUIET_EVAL", ASSERT_QUIET_EVAL, CONST_CS|CONST_PERSISTENT);
110 	REGISTER_LONG_CONSTANT("ASSERT_EXCEPTION", ASSERT_EXCEPTION, CONST_CS|CONST_PERSISTENT);
111 
112 	INIT_CLASS_ENTRY(ce, "AssertionError", NULL);
113 	assertion_error_ce = zend_register_internal_class_ex(&ce, zend_ce_error);
114 
115 	return SUCCESS;
116 }
117 /* }}} */
118 
PHP_MSHUTDOWN_FUNCTION(assert)119 PHP_MSHUTDOWN_FUNCTION(assert) /* {{{ */
120 {
121 	if (ASSERTG(cb)) {
122 		pefree(ASSERTG(cb), 1);
123 		ASSERTG(cb) = NULL;
124 	}
125 	return SUCCESS;
126 }
127 /* }}} */
128 
PHP_RSHUTDOWN_FUNCTION(assert)129 PHP_RSHUTDOWN_FUNCTION(assert) /* {{{ */
130 {
131 	if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
132 		zval_ptr_dtor(&ASSERTG(callback));
133 		ZVAL_UNDEF(&ASSERTG(callback));
134 	}
135 
136 	return SUCCESS;
137 }
138 /* }}} */
139 
PHP_MINFO_FUNCTION(assert)140 PHP_MINFO_FUNCTION(assert) /* {{{ */
141 {
142 	DISPLAY_INI_ENTRIES();
143 }
144 /* }}} */
145 
146 /* {{{ proto int assert(string|bool assertion[, mixed description])
147    Checks if assertion is false */
PHP_FUNCTION(assert)148 PHP_FUNCTION(assert)
149 {
150 	zval *assertion;
151 	zval *description = NULL;
152 	int val;
153 	char *myeval = NULL;
154 	char *compiled_string_description;
155 
156 	if (! ASSERTG(active)) {
157 		RETURN_TRUE;
158 	}
159 
160 	ZEND_PARSE_PARAMETERS_START(1, 2)
161 		Z_PARAM_ZVAL(assertion)
162 		Z_PARAM_OPTIONAL
163 		Z_PARAM_ZVAL(description)
164 	ZEND_PARSE_PARAMETERS_END();
165 
166 	if (Z_TYPE_P(assertion) == IS_STRING) {
167 		zval retval;
168 		int old_error_reporting = 0; /* shut up gcc! */
169 
170 		if (zend_forbid_dynamic_call("assert() with string argument") == FAILURE) {
171 			RETURN_FALSE;
172 		}
173 
174 		php_error_docref(NULL, E_DEPRECATED, "Calling assert() with a string argument is deprecated");
175 
176 		myeval = Z_STRVAL_P(assertion);
177 
178 		if (ASSERTG(quiet_eval)) {
179 			old_error_reporting = EG(error_reporting);
180 			EG(error_reporting) = 0;
181 		}
182 
183 		compiled_string_description = zend_make_compiled_string_description("assert code");
184 		if (zend_eval_stringl(myeval, Z_STRLEN_P(assertion), &retval, compiled_string_description) == FAILURE) {
185 			efree(compiled_string_description);
186 			if (!description) {
187 				zend_throw_error(NULL, "Failure evaluating code: %s%s", PHP_EOL, myeval);
188 			} else {
189 				zend_string *str = zval_get_string(description);
190 				zend_throw_error(NULL, "Failure evaluating code: %s%s:\"%s\"", PHP_EOL, ZSTR_VAL(str), myeval);
191 				zend_string_release(str);
192 			}
193 			if (ASSERTG(bail)) {
194 				zend_bailout();
195 			}
196 			RETURN_FALSE;
197 		}
198 		efree(compiled_string_description);
199 
200 		if (ASSERTG(quiet_eval)) {
201 			EG(error_reporting) = old_error_reporting;
202 		}
203 
204 		convert_to_boolean(&retval);
205 		val = Z_TYPE(retval) == IS_TRUE;
206 	} else {
207 		val = zend_is_true(assertion);
208 	}
209 
210 	if (val) {
211 		RETURN_TRUE;
212 	}
213 
214 	if (Z_TYPE(ASSERTG(callback)) == IS_UNDEF && ASSERTG(cb)) {
215 		ZVAL_STRING(&ASSERTG(callback), ASSERTG(cb));
216 	}
217 
218 	if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
219 		zval *args = safe_emalloc(!description ? 3 : 4, sizeof(zval), 0);
220 		zval retval;
221 		int i;
222 		uint32_t lineno = zend_get_executed_lineno();
223 		const char *filename = zend_get_executed_filename();
224 
225 		ZVAL_STRING(&args[0], SAFE_STRING(filename));
226 		ZVAL_LONG (&args[1], lineno);
227 		ZVAL_STRING(&args[2], SAFE_STRING(myeval));
228 
229 		ZVAL_FALSE(&retval);
230 
231 		/* XXX do we want to check for error here? */
232 		if (!description) {
233 			call_user_function(CG(function_table), NULL, &ASSERTG(callback), &retval, 3, args);
234 			for (i = 0; i <= 2; i++) {
235 				zval_ptr_dtor(&(args[i]));
236 			}
237 		} else {
238 			ZVAL_STR(&args[3], zval_get_string(description));
239 			call_user_function(CG(function_table), NULL, &ASSERTG(callback), &retval, 4, args);
240 			for (i = 0; i <= 3; i++) {
241 				zval_ptr_dtor(&(args[i]));
242 			}
243 		}
244 
245 		efree(args);
246 		zval_ptr_dtor(&retval);
247 	}
248 
249 	if (ASSERTG(exception)) {
250 		if (!description) {
251 			zend_throw_exception(assertion_error_ce, NULL, E_ERROR);
252 		} else if (Z_TYPE_P(description) == IS_OBJECT &&
253 			instanceof_function(Z_OBJCE_P(description), zend_ce_throwable)) {
254 			Z_ADDREF_P(description);
255 			zend_throw_exception_object(description);
256 		} else {
257 			zend_string *str = zval_get_string(description);
258 			zend_throw_exception(assertion_error_ce, ZSTR_VAL(str), E_ERROR);
259 			zend_string_release(str);
260 		}
261 	} else if (ASSERTG(warning)) {
262 		if (!description) {
263 			if (myeval) {
264 				php_error_docref(NULL, E_WARNING, "Assertion \"%s\" failed", myeval);
265 			} else {
266 				php_error_docref(NULL, E_WARNING, "Assertion failed");
267 			}
268 		} else {
269 			zend_string *str = zval_get_string(description);
270 			if (myeval) {
271 				php_error_docref(NULL, E_WARNING, "%s: \"%s\" failed", ZSTR_VAL(str), myeval);
272 			} else {
273 				php_error_docref(NULL, E_WARNING, "%s failed", ZSTR_VAL(str));
274 			}
275 			zend_string_release(str);
276 		}
277 	}
278 
279 	if (ASSERTG(bail)) {
280 		zend_bailout();
281 	}
282 
283 	RETURN_FALSE;
284 }
285 /* }}} */
286 
287 /* {{{ proto mixed assert_options(int what [, mixed value])
288    Set/get the various assert flags */
PHP_FUNCTION(assert_options)289 PHP_FUNCTION(assert_options)
290 {
291 	zval *value = NULL;
292 	zend_long what;
293 	zend_bool oldint;
294 	int ac = ZEND_NUM_ARGS();
295 	zend_string *key;
296 
297 	ZEND_PARSE_PARAMETERS_START(1, 2)
298 		Z_PARAM_LONG(what)
299 		Z_PARAM_OPTIONAL
300 		Z_PARAM_ZVAL(value)
301 	ZEND_PARSE_PARAMETERS_END();
302 
303 	switch (what) {
304 	case ASSERT_ACTIVE:
305 		oldint = ASSERTG(active);
306 		if (ac == 2) {
307 			zend_string *value_str = zval_get_string(value);
308 			key = zend_string_init("assert.active", sizeof("assert.active")-1, 0);
309 			zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
310 			zend_string_release(key);
311 			zend_string_release(value_str);
312 		}
313 		RETURN_LONG(oldint);
314 		break;
315 
316 	case ASSERT_BAIL:
317 		oldint = ASSERTG(bail);
318 		if (ac == 2) {
319 			zend_string *value_str = zval_get_string(value);
320 			key = zend_string_init("assert.bail", sizeof("assert.bail")-1, 0);
321 			zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
322 			zend_string_release(key);
323 			zend_string_release(value_str);
324 		}
325 		RETURN_LONG(oldint);
326 		break;
327 
328 	case ASSERT_QUIET_EVAL:
329 		oldint = ASSERTG(quiet_eval);
330 		if (ac == 2) {
331 			zend_string *value_str = zval_get_string(value);
332 			key = zend_string_init("assert.quiet_eval", sizeof("assert.quiet_eval")-1, 0);
333 			zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
334 			zend_string_release(key);
335 			zend_string_release(value_str);
336 		}
337 		RETURN_LONG(oldint);
338 		break;
339 
340 	case ASSERT_WARNING:
341 		oldint = ASSERTG(warning);
342 		if (ac == 2) {
343 			zend_string *value_str = zval_get_string(value);
344 			key = zend_string_init("assert.warning", sizeof("assert.warning")-1, 0);
345 			zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
346 			zend_string_release(key);
347 			zend_string_release(value_str);
348 		}
349 		RETURN_LONG(oldint);
350 		break;
351 
352 	case ASSERT_CALLBACK:
353 		if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
354 			ZVAL_COPY(return_value, &ASSERTG(callback));
355 		} else if (ASSERTG(cb)) {
356 			RETVAL_STRING(ASSERTG(cb));
357 		} else {
358 			RETVAL_NULL();
359 		}
360 		if (ac == 2) {
361 			zval_ptr_dtor(&ASSERTG(callback));
362 			ZVAL_COPY(&ASSERTG(callback), value);
363 		}
364 		return;
365 
366 	case ASSERT_EXCEPTION:
367 		oldint = ASSERTG(exception);
368 		if (ac == 2) {
369 			zend_string *key = zend_string_init("assert.exception", sizeof("assert.exception")-1, 0);
370 			zend_string *val = zval_get_string(value);
371 			zend_alter_ini_entry_ex(key, val, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
372 			zend_string_release(val);
373 			zend_string_release(key);
374 		}
375 		RETURN_LONG(oldint);
376 		break;
377 
378 	default:
379 		php_error_docref(NULL, E_WARNING, "Unknown value " ZEND_LONG_FMT, what);
380 		break;
381 	}
382 
383 	RETURN_FALSE;
384 }
385 /* }}} */
386 
387 /*
388  * Local variables:
389  * tab-width: 4
390  * c-basic-offset: 4
391  * End:
392  * vim600: sw=4 ts=4 fdm=marker
393  * vim<600: sw=4 ts=4
394  */
395