assert.c 11 KB

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