assert.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. /* {{{ includes */
  17. #include "php.h"
  18. #include "php_assert.h"
  19. #include "php_ini.h"
  20. #include "zend_exceptions.h"
  21. /* }}} */
  22. ZEND_BEGIN_MODULE_GLOBALS(assert)
  23. zval callback;
  24. char *cb;
  25. bool active;
  26. bool bail;
  27. bool warning;
  28. bool exception;
  29. ZEND_END_MODULE_GLOBALS(assert)
  30. ZEND_DECLARE_MODULE_GLOBALS(assert)
  31. #define ASSERTG(v) ZEND_MODULE_GLOBALS_ACCESSOR(assert, v)
  32. #define SAFE_STRING(s) ((s)?(s):"")
  33. enum {
  34. ASSERT_ACTIVE=1,
  35. ASSERT_CALLBACK,
  36. ASSERT_BAIL,
  37. ASSERT_WARNING,
  38. ASSERT_EXCEPTION
  39. };
  40. PHPAPI zend_class_entry *assertion_error_ce;
  41. static PHP_INI_MH(OnChangeCallback) /* {{{ */
  42. {
  43. if (EG(current_execute_data)) {
  44. if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
  45. zval_ptr_dtor(&ASSERTG(callback));
  46. ZVAL_UNDEF(&ASSERTG(callback));
  47. }
  48. if (new_value && (Z_TYPE(ASSERTG(callback)) != IS_UNDEF || ZSTR_LEN(new_value))) {
  49. ZVAL_STR_COPY(&ASSERTG(callback), new_value);
  50. }
  51. } else {
  52. if (ASSERTG(cb)) {
  53. pefree(ASSERTG(cb), 1);
  54. }
  55. if (new_value && ZSTR_LEN(new_value)) {
  56. ASSERTG(cb) = pemalloc(ZSTR_LEN(new_value) + 1, 1);
  57. memcpy(ASSERTG(cb), ZSTR_VAL(new_value), ZSTR_LEN(new_value));
  58. ASSERTG(cb)[ZSTR_LEN(new_value)] = '\0';
  59. } else {
  60. ASSERTG(cb) = NULL;
  61. }
  62. }
  63. return SUCCESS;
  64. }
  65. /* }}} */
  66. PHP_INI_BEGIN()
  67. STD_PHP_INI_BOOLEAN("assert.active", "1", PHP_INI_ALL, OnUpdateBool, active, zend_assert_globals, assert_globals)
  68. STD_PHP_INI_BOOLEAN("assert.bail", "0", PHP_INI_ALL, OnUpdateBool, bail, zend_assert_globals, assert_globals)
  69. STD_PHP_INI_BOOLEAN("assert.warning", "1", PHP_INI_ALL, OnUpdateBool, warning, zend_assert_globals, assert_globals)
  70. PHP_INI_ENTRY("assert.callback", NULL, PHP_INI_ALL, OnChangeCallback)
  71. STD_PHP_INI_BOOLEAN("assert.exception", "1", PHP_INI_ALL, OnUpdateBool, exception, zend_assert_globals, assert_globals)
  72. PHP_INI_END()
  73. static void php_assert_init_globals(zend_assert_globals *assert_globals_p) /* {{{ */
  74. {
  75. ZVAL_UNDEF(&assert_globals_p->callback);
  76. assert_globals_p->cb = NULL;
  77. }
  78. /* }}} */
  79. PHP_MINIT_FUNCTION(assert) /* {{{ */
  80. {
  81. ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL);
  82. REGISTER_INI_ENTRIES();
  83. REGISTER_LONG_CONSTANT("ASSERT_ACTIVE", ASSERT_ACTIVE, CONST_CS|CONST_PERSISTENT);
  84. REGISTER_LONG_CONSTANT("ASSERT_CALLBACK", ASSERT_CALLBACK, CONST_CS|CONST_PERSISTENT);
  85. REGISTER_LONG_CONSTANT("ASSERT_BAIL", ASSERT_BAIL, CONST_CS|CONST_PERSISTENT);
  86. REGISTER_LONG_CONSTANT("ASSERT_WARNING", ASSERT_WARNING, CONST_CS|CONST_PERSISTENT);
  87. REGISTER_LONG_CONSTANT("ASSERT_EXCEPTION", ASSERT_EXCEPTION, CONST_CS|CONST_PERSISTENT);
  88. return SUCCESS;
  89. }
  90. /* }}} */
  91. PHP_MSHUTDOWN_FUNCTION(assert) /* {{{ */
  92. {
  93. if (ASSERTG(cb)) {
  94. pefree(ASSERTG(cb), 1);
  95. ASSERTG(cb) = NULL;
  96. }
  97. return SUCCESS;
  98. }
  99. /* }}} */
  100. PHP_RSHUTDOWN_FUNCTION(assert) /* {{{ */
  101. {
  102. if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
  103. zval_ptr_dtor(&ASSERTG(callback));
  104. ZVAL_UNDEF(&ASSERTG(callback));
  105. }
  106. return SUCCESS;
  107. }
  108. /* }}} */
  109. PHP_MINFO_FUNCTION(assert) /* {{{ */
  110. {
  111. DISPLAY_INI_ENTRIES();
  112. }
  113. /* }}} */
  114. /* {{{ Checks if assertion is false */
  115. PHP_FUNCTION(assert)
  116. {
  117. zval *assertion;
  118. zend_string *description_str = NULL;
  119. zend_object *description_obj = NULL;
  120. if (!ASSERTG(active)) {
  121. RETURN_TRUE;
  122. }
  123. ZEND_PARSE_PARAMETERS_START(1, 2)
  124. Z_PARAM_ZVAL(assertion)
  125. Z_PARAM_OPTIONAL
  126. Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(description_obj, zend_ce_throwable, description_str)
  127. ZEND_PARSE_PARAMETERS_END();
  128. if (zend_is_true(assertion)) {
  129. RETURN_TRUE;
  130. }
  131. if (description_obj) {
  132. GC_ADDREF(description_obj);
  133. zend_throw_exception_internal(description_obj);
  134. RETURN_THROWS();
  135. }
  136. if (Z_TYPE(ASSERTG(callback)) == IS_UNDEF && ASSERTG(cb)) {
  137. ZVAL_STRING(&ASSERTG(callback), ASSERTG(cb));
  138. }
  139. if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
  140. zval args[4];
  141. zval retval;
  142. uint32_t lineno = zend_get_executed_lineno();
  143. const char *filename = zend_get_executed_filename();
  144. ZVAL_STRING(&args[0], SAFE_STRING(filename));
  145. ZVAL_LONG(&args[1], lineno);
  146. ZVAL_NULL(&args[2]);
  147. ZVAL_FALSE(&retval);
  148. if (description_str) {
  149. ZVAL_STR(&args[3], description_str);
  150. call_user_function(NULL, NULL, &ASSERTG(callback), &retval, 4, args);
  151. } else {
  152. call_user_function(NULL, NULL, &ASSERTG(callback), &retval, 3, args);
  153. }
  154. zval_ptr_dtor(&args[0]);
  155. zval_ptr_dtor(&retval);
  156. }
  157. if (ASSERTG(exception)) {
  158. zend_throw_exception(assertion_error_ce, description_str ? ZSTR_VAL(description_str) : NULL, E_ERROR);
  159. if (ASSERTG(bail)) {
  160. /* When bail is turned on, the exception will not be caught. */
  161. zend_exception_error(EG(exception), E_ERROR);
  162. }
  163. } else if (ASSERTG(warning)) {
  164. php_error_docref(NULL, E_WARNING, "%s failed", description_str ? ZSTR_VAL(description_str) : "Assertion failed");
  165. }
  166. if (ASSERTG(bail)) {
  167. zend_throw_unwind_exit();
  168. RETURN_THROWS();
  169. } else {
  170. RETURN_FALSE;
  171. }
  172. }
  173. /* }}} */
  174. /* {{{ Set/get the various assert flags */
  175. PHP_FUNCTION(assert_options)
  176. {
  177. zval *value = NULL;
  178. zend_long what;
  179. bool oldint;
  180. int ac = ZEND_NUM_ARGS();
  181. zend_string *key;
  182. ZEND_PARSE_PARAMETERS_START(1, 2)
  183. Z_PARAM_LONG(what)
  184. Z_PARAM_OPTIONAL
  185. Z_PARAM_ZVAL(value)
  186. ZEND_PARSE_PARAMETERS_END();
  187. switch (what) {
  188. case ASSERT_ACTIVE:
  189. oldint = ASSERTG(active);
  190. if (ac == 2) {
  191. zend_string *value_str = zval_try_get_string(value);
  192. if (UNEXPECTED(!value_str)) {
  193. RETURN_THROWS();
  194. }
  195. key = zend_string_init("assert.active", sizeof("assert.active")-1, 0);
  196. zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
  197. zend_string_release_ex(key, 0);
  198. zend_string_release_ex(value_str, 0);
  199. }
  200. RETURN_LONG(oldint);
  201. break;
  202. case ASSERT_BAIL:
  203. oldint = ASSERTG(bail);
  204. if (ac == 2) {
  205. zend_string *value_str = zval_try_get_string(value);
  206. if (UNEXPECTED(!value_str)) {
  207. RETURN_THROWS();
  208. }
  209. key = zend_string_init("assert.bail", sizeof("assert.bail")-1, 0);
  210. zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
  211. zend_string_release_ex(key, 0);
  212. zend_string_release_ex(value_str, 0);
  213. }
  214. RETURN_LONG(oldint);
  215. break;
  216. case ASSERT_WARNING:
  217. oldint = ASSERTG(warning);
  218. if (ac == 2) {
  219. zend_string *value_str = zval_try_get_string(value);
  220. if (UNEXPECTED(!value_str)) {
  221. RETURN_THROWS();
  222. }
  223. key = zend_string_init("assert.warning", sizeof("assert.warning")-1, 0);
  224. zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
  225. zend_string_release_ex(key, 0);
  226. zend_string_release_ex(value_str, 0);
  227. }
  228. RETURN_LONG(oldint);
  229. break;
  230. case ASSERT_CALLBACK:
  231. if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
  232. ZVAL_COPY(return_value, &ASSERTG(callback));
  233. } else if (ASSERTG(cb)) {
  234. RETVAL_STRING(ASSERTG(cb));
  235. } else {
  236. RETVAL_NULL();
  237. }
  238. if (ac == 2) {
  239. zval_ptr_dtor(&ASSERTG(callback));
  240. if (Z_TYPE_P(value) == IS_NULL) {
  241. ZVAL_UNDEF(&ASSERTG(callback));
  242. } else {
  243. ZVAL_COPY(&ASSERTG(callback), value);
  244. }
  245. }
  246. return;
  247. case ASSERT_EXCEPTION:
  248. oldint = ASSERTG(exception);
  249. if (ac == 2) {
  250. zend_string *val = zval_try_get_string(value);
  251. if (UNEXPECTED(!val)) {
  252. RETURN_THROWS();
  253. }
  254. key = zend_string_init("assert.exception", sizeof("assert.exception")-1, 0);
  255. zend_alter_ini_entry_ex(key, val, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
  256. zend_string_release_ex(val, 0);
  257. zend_string_release_ex(key, 0);
  258. }
  259. RETURN_LONG(oldint);
  260. break;
  261. default:
  262. zend_argument_value_error(1, "must be an ASSERT_* constant");
  263. RETURN_THROWS();
  264. }
  265. }
  266. /* }}} */