Coverage Report

Created: 2026-06-13 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/standard/assert.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Author: Thies C. Arntzen <thies@thieso.net>                          |
12
   +----------------------------------------------------------------------+
13
*/
14
15
/* {{{ includes */
16
#include "php.h"
17
#include "php_assert.h"
18
#include "php_ini.h"
19
#include "zend_exceptions.h"
20
/* }}} */
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
31
ZEND_DECLARE_MODULE_GLOBALS(assert)
32
33
5.90k
#define ASSERTG(v) ZEND_MODULE_GLOBALS_ACCESSOR(assert, v)
34
35
PHPAPI zend_class_entry *assertion_error_ce;
36
37
/* Hack to pass a custom stage for the our OnModify handler so that a deprecation warning does not get emitted
38
 * when an option is modified via assert_option() function */
39
283
#define ZEND_INI_STAGE_ASSERT_OPTIONS (1<<6)
40
41
static inline bool php_must_emit_ini_deprecation(int stage)
42
141
{
43
141
  return stage != ZEND_INI_STAGE_DEACTIVATE && stage != ZEND_INI_STAGE_SHUTDOWN && stage != ZEND_INI_STAGE_ASSERT_OPTIONS;
44
141
}
45
46
static PHP_INI_MH(OnChangeCallback) /* {{{ */
47
16
{
48
16
  if (EG(current_execute_data)) {
49
0
    if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
50
0
      zval_ptr_dtor(&ASSERTG(callback));
51
0
      ZVAL_UNDEF(&ASSERTG(callback));
52
0
    }
53
0
    if (new_value && (Z_TYPE(ASSERTG(callback)) != IS_UNDEF || ZSTR_LEN(new_value))) {
54
0
      if (php_must_emit_ini_deprecation(stage)) {
55
0
        php_error_docref(NULL, E_DEPRECATED, "assert.callback INI setting is deprecated");
56
0
      }
57
0
      ZVAL_STR_COPY(&ASSERTG(callback), new_value);
58
0
    }
59
16
  } else {
60
16
    if (ASSERTG(cb)) {
61
0
      pefree(ASSERTG(cb), 1);
62
0
    }
63
16
    if (new_value && ZSTR_LEN(new_value)) {
64
0
      if (php_must_emit_ini_deprecation(stage)) {
65
0
        php_error_docref(NULL, E_DEPRECATED, "assert.callback INI setting is deprecated");
66
0
      }
67
0
      ASSERTG(cb) = pemalloc(ZSTR_LEN(new_value) + 1, 1);
68
0
      memcpy(ASSERTG(cb), ZSTR_VAL(new_value), ZSTR_LEN(new_value));
69
0
      ASSERTG(cb)[ZSTR_LEN(new_value)] = '\0';
70
16
    } else {
71
16
      ASSERTG(cb) = NULL;
72
16
    }
73
16
  }
74
16
  return SUCCESS;
75
16
}
76
/* }}} */
77
78
static PHP_INI_MH(OnUpdateActiveBool)
79
16
{
80
16
  bool *p = ZEND_INI_GET_ADDR();
81
16
  *p = zend_ini_parse_bool(new_value);
82
16
  if (php_must_emit_ini_deprecation(stage) && !*p) {
83
0
    php_error_docref(NULL, E_DEPRECATED, "assert.active INI setting is deprecated");
84
0
  }
85
16
  return SUCCESS;
86
16
}
87
88
static PHP_INI_MH(OnUpdateBailBool)
89
52
{
90
52
  bool *p = ZEND_INI_GET_ADDR();
91
52
  *p = zend_ini_parse_bool(new_value);
92
52
  if (php_must_emit_ini_deprecation(stage) && *p) {
93
0
    php_error_docref(NULL, E_DEPRECATED, "assert.bail INI setting is deprecated");
94
0
  }
95
52
  return SUCCESS;
96
52
}
97
98
static PHP_INI_MH(OnUpdateExceptionBool)
99
57
{
100
57
  bool *p = ZEND_INI_GET_ADDR();
101
57
  *p = zend_ini_parse_bool(new_value);
102
57
  if (php_must_emit_ini_deprecation(stage) && !*p) {
103
0
    php_error_docref(NULL, E_DEPRECATED, "assert.exception INI setting is deprecated");
104
0
  }
105
57
  return SUCCESS;
106
57
}
107
108
109
static PHP_INI_MH(OnUpdateWarningBool)
110
16
{
111
16
  bool *p = ZEND_INI_GET_ADDR();
112
16
  *p = zend_ini_parse_bool(new_value);
113
16
  if (php_must_emit_ini_deprecation(stage) && !*p) {
114
0
    php_error_docref(NULL, E_DEPRECATED, "assert.warning INI setting is deprecated");
115
0
  }
116
16
  return SUCCESS;
117
16
}
118
119
120
PHP_INI_BEGIN()
121
   STD_PHP_INI_BOOLEAN("assert.active",    "1",  PHP_INI_ALL, OnUpdateActiveBool,   active,       zend_assert_globals,    assert_globals)
122
   STD_PHP_INI_BOOLEAN("assert.bail",      "0",  PHP_INI_ALL, OnUpdateBailBool,   bail,       zend_assert_globals,    assert_globals)
123
   STD_PHP_INI_BOOLEAN("assert.warning",   "1",  PHP_INI_ALL, OnUpdateWarningBool,    warning,      zend_assert_globals,    assert_globals)
124
   PHP_INI_ENTRY("assert.callback",        NULL, PHP_INI_ALL, OnChangeCallback)
125
   STD_PHP_INI_BOOLEAN("assert.exception", "1",  PHP_INI_ALL, OnUpdateExceptionBool,    exception,      zend_assert_globals,    assert_globals)
126
PHP_INI_END()
127
128
static void php_assert_init_globals(zend_assert_globals *assert_globals_p) /* {{{ */
129
16
{
130
16
  ZVAL_UNDEF(&assert_globals_p->callback);
131
16
  assert_globals_p->cb = NULL;
132
16
}
133
/* }}} */
134
135
PHP_MINIT_FUNCTION(assert) /* {{{ */
136
16
{
137
16
  ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL);
138
139
16
  REGISTER_INI_ENTRIES();
140
141
16
  return SUCCESS;
142
16
}
143
/* }}} */
144
145
PHP_MSHUTDOWN_FUNCTION(assert) /* {{{ */
146
0
{
147
0
  if (ASSERTG(cb)) {
148
0
    pefree(ASSERTG(cb), 1);
149
0
    ASSERTG(cb) = NULL;
150
0
  }
151
0
  return SUCCESS;
152
0
}
153
/* }}} */
154
155
PHP_RSHUTDOWN_FUNCTION(assert) /* {{{ */
156
229k
{
157
229k
  if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
158
18
    zval_ptr_dtor(&ASSERTG(callback));
159
18
    ZVAL_UNDEF(&ASSERTG(callback));
160
18
  }
161
162
229k
  return SUCCESS;
163
229k
}
164
/* }}} */
165
166
PHP_MINFO_FUNCTION(assert) /* {{{ */
167
4
{
168
4
  DISPLAY_INI_ENTRIES();
169
4
}
170
/* }}} */
171
172
/* {{{ Checks if assertion is false */
173
PHP_FUNCTION(assert)
174
3.08k
{
175
3.08k
  zval *assertion;
176
3.08k
  zend_string *description_str = NULL;
177
3.08k
  zend_object *description_obj = NULL;
178
179
  /* EG(assertions) <= 0 is only reachable by dynamic calls to assert(),
180
   * since calls known at compile time will skip the entire call when
181
   * assertions are disabled.
182
   */
183
3.08k
  if (!ASSERTG(active) || EG(assertions) <= 0) {
184
0
    RETURN_TRUE;
185
0
  }
186
187
9.25k
  ZEND_PARSE_PARAMETERS_START(1, 2)
188
12.3k
    Z_PARAM_ZVAL(assertion)
189
12.3k
    Z_PARAM_OPTIONAL
190
15.3k
    Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(description_obj, zend_ce_throwable, description_str)
191
15.3k
  ZEND_PARSE_PARAMETERS_END();
192
193
3.08k
  if (zend_is_true(assertion)) {
194
2.39k
    RETURN_TRUE;
195
2.39k
  }
196
197
689
  if (description_obj) {
198
10
    GC_ADDREF(description_obj);
199
10
    zend_throw_exception_internal(description_obj);
200
10
    RETURN_THROWS();
201
10
  }
202
203
679
  if (Z_TYPE(ASSERTG(callback)) == IS_UNDEF && ASSERTG(cb)) {
204
0
    ZVAL_STRING(&ASSERTG(callback), ASSERTG(cb));
205
0
  }
206
207
679
  if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
208
18
    zval args[4];
209
18
    zval retval;
210
18
    uint32_t lineno = zend_get_executed_lineno();
211
18
    zend_string *filename = zend_get_executed_filename_ex();
212
18
    if (UNEXPECTED(!filename)) {
213
0
      filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED);
214
0
    }
215
216
18
    ZVAL_STR(&args[0], filename);
217
18
    ZVAL_LONG(&args[1], lineno);
218
18
    ZVAL_NULL(&args[2]);
219
220
18
    ZVAL_FALSE(&retval);
221
222
18
    if (description_str) {
223
18
      ZVAL_STR(&args[3], description_str);
224
18
      call_user_function(NULL, NULL, &ASSERTG(callback), &retval, 4, args);
225
18
    } else {
226
0
      call_user_function(NULL, NULL, &ASSERTG(callback), &retval, 3, args);
227
0
    }
228
229
18
    zval_ptr_dtor(&retval);
230
18
  }
231
232
679
  if (ASSERTG(exception)) {
233
665
    zend_throw_exception(assertion_error_ce, description_str ? ZSTR_VAL(description_str) : NULL, E_ERROR);
234
665
    if (ASSERTG(bail)) {
235
      /* When bail is turned on, the exception will not be caught. */
236
3
      zend_exception_error(EG(exception), E_ERROR);
237
3
    }
238
665
  } else if (ASSERTG(warning)) {
239
14
    php_error_docref(NULL, E_WARNING, "%s failed", description_str ? ZSTR_VAL(description_str) : "Assertion");
240
14
  }
241
242
679
  if (ASSERTG(bail)) {
243
16
    if (EG(exception)) {
244
      /* The callback might have thrown. Use E_WARNING to print the
245
       * exception so we can avoid bailout and use unwind_exit. */
246
13
      zend_exception_error(EG(exception), E_WARNING);
247
13
    }
248
16
    zend_throw_unwind_exit();
249
16
    RETURN_THROWS();
250
663
  } else {
251
663
    RETURN_FALSE;
252
663
  }
253
679
}
254
/* }}} */
255
256
/* {{{ Set/get the various assert flags */
257
PHP_FUNCTION(assert_options)
258
57
{
259
57
  zval *value = NULL;
260
57
  zend_long what;
261
57
  bool oldint;
262
57
  uint32_t ac = ZEND_NUM_ARGS();
263
57
  zend_string *key;
264
265
171
  ZEND_PARSE_PARAMETERS_START(1, 2)
266
228
    Z_PARAM_LONG(what)
267
57
    Z_PARAM_OPTIONAL
268
228
    Z_PARAM_ZVAL(value)
269
228
  ZEND_PARSE_PARAMETERS_END();
270
271
57
  switch (what) {
272
0
  case PHP_ASSERT_ACTIVE:
273
0
    oldint = ASSERTG(active);
274
0
    if (ac == 2) {
275
0
      zend_string *value_str = zval_try_get_string(value);
276
0
      if (UNEXPECTED(!value_str)) {
277
0
        RETURN_THROWS();
278
0
      }
279
280
0
      key = ZSTR_INIT_LITERAL("assert.active", 0);
281
0
      zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
282
0
      zend_string_release_ex(key, 0);
283
0
      zend_string_release_ex(value_str, 0);
284
0
    }
285
0
    RETURN_LONG(oldint);
286
287
18
  case PHP_ASSERT_BAIL:
288
18
    oldint = ASSERTG(bail);
289
18
    if (ac == 2) {
290
18
      zend_string *value_str = zval_try_get_string(value);
291
18
      if (UNEXPECTED(!value_str)) {
292
0
        RETURN_THROWS();
293
0
      }
294
295
18
      key = ZSTR_INIT_LITERAL("assert.bail", 0);
296
18
      zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
297
18
      zend_string_release_ex(key, 0);
298
18
      zend_string_release_ex(value_str, 0);
299
18
    }
300
18
    RETURN_LONG(oldint);
301
302
0
  case PHP_ASSERT_WARNING:
303
0
    oldint = ASSERTG(warning);
304
0
    if (ac == 2) {
305
0
      zend_string *value_str = zval_try_get_string(value);
306
0
      if (UNEXPECTED(!value_str)) {
307
0
        RETURN_THROWS();
308
0
      }
309
310
0
      key = ZSTR_INIT_LITERAL("assert.warning", 0);
311
0
      zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
312
0
      zend_string_release_ex(key, 0);
313
0
      zend_string_release_ex(value_str, 0);
314
0
    }
315
0
    RETURN_LONG(oldint);
316
317
18
  case PHP_ASSERT_CALLBACK:
318
18
    if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
319
0
      ZVAL_COPY(return_value, &ASSERTG(callback));
320
18
    } else if (ASSERTG(cb)) {
321
0
      RETVAL_STRING(ASSERTG(cb));
322
18
    } else {
323
18
      RETVAL_NULL();
324
18
    }
325
326
18
    if (ac == 2) {
327
18
      zval_ptr_dtor(&ASSERTG(callback));
328
18
      if (Z_TYPE_P(value) == IS_NULL) {
329
0
        ZVAL_UNDEF(&ASSERTG(callback));
330
18
      } else {
331
18
        ZVAL_COPY(&ASSERTG(callback), value);
332
18
      }
333
18
    }
334
18
    return;
335
336
21
  case PHP_ASSERT_EXCEPTION:
337
21
    oldint = ASSERTG(exception);
338
21
    if (ac == 2) {
339
21
      zend_string *val = zval_try_get_string(value);
340
21
      if (UNEXPECTED(!val)) {
341
0
        RETURN_THROWS();
342
0
      }
343
344
21
      key = ZSTR_INIT_LITERAL("assert.exception", 0);
345
21
      zend_alter_ini_entry_ex(key, val, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
346
21
      zend_string_release_ex(val, 0);
347
21
      zend_string_release_ex(key, 0);
348
21
    }
349
21
    RETURN_LONG(oldint);
350
351
0
  default:
352
0
    zend_argument_value_error(1, "must be an ASSERT_* constant");
353
    RETURN_THROWS();
354
57
  }
355
57
}
356
/* }}} */