Coverage Report

Created: 2026-10-28 06:20

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
298
#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
166
#define ZEND_INI_STAGE_ASSERT_OPTIONS (1<<6)
40
41
static inline bool php_must_emit_ini_deprecation(int stage)
42
82
{
43
82
  return stage != ZEND_INI_STAGE_DEACTIVATE && stage != ZEND_INI_STAGE_SHUTDOWN && stage != ZEND_INI_STAGE_ASSERT_OPTIONS;
44
82
}
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
20
{
90
20
  bool *p = ZEND_INI_GET_ADDR();
91
20
  *p = zend_ini_parse_bool(new_value);
92
20
  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
20
  return SUCCESS;
96
20
}
97
98
static PHP_INI_MH(OnUpdateExceptionBool)
99
30
{
100
30
  bool *p = ZEND_INI_GET_ADDR();
101
30
  *p = zend_ini_parse_bool(new_value);
102
30
  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
30
  return SUCCESS;
106
30
}
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
306k
{
157
306k
  if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
158
4
    zval_ptr_dtor(&ASSERTG(callback));
159
4
    ZVAL_UNDEF(&ASSERTG(callback));
160
4
  }
161
162
306k
  return SUCCESS;
163
306k
}
164
/* }}} */
165
166
PHP_MINFO_FUNCTION(assert) /* {{{ */
167
0
{
168
0
  DISPLAY_INI_ENTRIES();
169
0
}
170
/* }}} */
171
172
/* {{{ Checks if assertion is false */
173
PHP_FUNCTION(assert)
174
120
{
175
120
  zval *assertion;
176
120
  zend_string *description_str = NULL;
177
120
  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
120
  if (!ASSERTG(active) || EG(assertions) <= 0) {
184
0
    RETURN_TRUE;
185
0
  }
186
187
355
  ZEND_PARSE_PARAMETERS_START(1, 2)
188
460
    Z_PARAM_ZVAL(assertion)
189
460
    Z_PARAM_OPTIONAL
190
575
    Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(description_obj, zend_ce_throwable, description_str)
191
575
  ZEND_PARSE_PARAMETERS_END();
192
193
115
  if (zend_is_true(assertion)) {
194
83
    RETURN_TRUE;
195
83
  }
196
197
32
  if (description_obj) {
198
0
    zend_throw_exception_internal(zend_object_copy(description_obj));
199
0
    RETURN_THROWS();
200
0
  }
201
202
32
  if (Z_TYPE(ASSERTG(callback)) == IS_UNDEF && ASSERTG(cb)) {
203
0
    ZVAL_STRING(&ASSERTG(callback), ASSERTG(cb));
204
0
  }
205
206
32
  if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
207
4
    zval args[4];
208
4
    zval retval;
209
4
    uint32_t lineno = zend_get_executed_lineno();
210
4
    zend_string *filename = zend_get_executed_filename_ex();
211
4
    if (UNEXPECTED(!filename)) {
212
0
      filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED);
213
0
    }
214
215
4
    ZVAL_STR(&args[0], filename);
216
4
    ZVAL_LONG(&args[1], lineno);
217
4
    ZVAL_NULL(&args[2]);
218
219
4
    ZVAL_FALSE(&retval);
220
221
4
    if (description_str) {
222
4
      ZVAL_STR(&args[3], description_str);
223
4
      call_user_function(NULL, NULL, &ASSERTG(callback), &retval, 4, args);
224
4
    } else {
225
0
      call_user_function(NULL, NULL, &ASSERTG(callback), &retval, 3, args);
226
0
    }
227
228
4
    zval_ptr_dtor(&retval);
229
4
  }
230
231
32
  if (ASSERTG(exception)) {
232
32
    zend_throw_exception(assertion_error_ce, description_str ? ZSTR_VAL(description_str) : NULL, E_ERROR);
233
32
    if (ASSERTG(bail)) {
234
      /* When bail is turned on, the exception will not be caught. */
235
2
      zend_exception_error(EG(exception), E_ERROR);
236
2
    }
237
32
  } else if (ASSERTG(warning)) {
238
0
    php_error_docref(NULL, E_WARNING, "%s failed", description_str ? ZSTR_VAL(description_str) : "Assertion");
239
0
  }
240
241
32
  if (ASSERTG(bail)) {
242
2
    if (EG(exception)) {
243
      /* The callback might have thrown. Use E_WARNING to print the
244
       * exception so we can avoid bailout and use unwind_exit. */
245
0
      zend_exception_error(EG(exception), E_WARNING);
246
0
    }
247
2
    if (!EG(exception)) {
248
2
      zend_throw_unwind_exit();
249
2
    }
250
2
    RETURN_THROWS();
251
30
  } else {
252
30
    RETURN_FALSE;
253
30
  }
254
32
}
255
/* }}} */
256
257
/* {{{ Set/get the various assert flags */
258
PHP_FUNCTION(assert_options)
259
14
{
260
14
  zval *value = NULL;
261
14
  zend_long what;
262
14
  bool oldint;
263
14
  uint32_t ac = ZEND_NUM_ARGS();
264
14
  zend_string *key;
265
266
42
  ZEND_PARSE_PARAMETERS_START(1, 2)
267
56
    Z_PARAM_LONG(what)
268
14
    Z_PARAM_OPTIONAL
269
56
    Z_PARAM_ZVAL(value)
270
56
  ZEND_PARSE_PARAMETERS_END();
271
272
14
  switch (what) {
273
0
  case PHP_ASSERT_ACTIVE:
274
0
    oldint = ASSERTG(active);
275
0
    if (ac == 2) {
276
0
      zend_string *value_str = zval_try_get_string(value);
277
0
      if (UNEXPECTED(!value_str)) {
278
0
        RETURN_THROWS();
279
0
      }
280
281
0
      key = ZSTR_INIT_LITERAL("assert.active", 0);
282
0
      zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
283
0
      zend_string_release_ex(key, 0);
284
0
      zend_string_release_ex(value_str, 0);
285
0
    }
286
0
    RETURN_LONG(oldint);
287
288
2
  case PHP_ASSERT_BAIL:
289
2
    oldint = ASSERTG(bail);
290
2
    if (ac == 2) {
291
2
      zend_string *value_str = zval_try_get_string(value);
292
2
      if (UNEXPECTED(!value_str)) {
293
0
        RETURN_THROWS();
294
0
      }
295
296
2
      key = ZSTR_INIT_LITERAL("assert.bail", 0);
297
2
      zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
298
2
      zend_string_release_ex(key, 0);
299
2
      zend_string_release_ex(value_str, 0);
300
2
    }
301
2
    RETURN_LONG(oldint);
302
303
0
  case PHP_ASSERT_WARNING:
304
0
    oldint = ASSERTG(warning);
305
0
    if (ac == 2) {
306
0
      zend_string *value_str = zval_try_get_string(value);
307
0
      if (UNEXPECTED(!value_str)) {
308
0
        RETURN_THROWS();
309
0
      }
310
311
0
      key = ZSTR_INIT_LITERAL("assert.warning", 0);
312
0
      zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
313
0
      zend_string_release_ex(key, 0);
314
0
      zend_string_release_ex(value_str, 0);
315
0
    }
316
0
    RETURN_LONG(oldint);
317
318
4
  case PHP_ASSERT_CALLBACK:
319
4
    if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
320
0
      ZVAL_COPY(return_value, &ASSERTG(callback));
321
4
    } else if (ASSERTG(cb)) {
322
0
      RETVAL_STRING(ASSERTG(cb));
323
4
    } else {
324
4
      RETVAL_NULL();
325
4
    }
326
327
4
    if (ac == 2) {
328
4
      zval_ptr_dtor(&ASSERTG(callback));
329
4
      if (Z_TYPE_P(value) == IS_NULL) {
330
0
        ZVAL_UNDEF(&ASSERTG(callback));
331
4
      } else {
332
4
        ZVAL_COPY(&ASSERTG(callback), value);
333
4
      }
334
4
    }
335
4
    return;
336
337
8
  case PHP_ASSERT_EXCEPTION:
338
8
    oldint = ASSERTG(exception);
339
8
    if (ac == 2) {
340
8
      zend_string *val = zval_try_get_string(value);
341
8
      if (UNEXPECTED(!val)) {
342
0
        RETURN_THROWS();
343
0
      }
344
345
8
      key = ZSTR_INIT_LITERAL("assert.exception", 0);
346
8
      zend_alter_ini_entry_ex(key, val, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
347
8
      zend_string_release_ex(val, 0);
348
8
      zend_string_release_ex(key, 0);
349
8
    }
350
8
    RETURN_LONG(oldint);
351
352
0
  default:
353
0
    zend_argument_value_error(1, "must be an ASSERT_* constant");
354
    RETURN_THROWS();
355
14
  }
356
14
}
357
/* }}} */