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