Coverage Report

Created: 2025-12-14 06:05

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