Coverage Report

Created: 2025-12-31 07:28

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
926
#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
161
#define ZEND_INI_STAGE_ASSERT_OPTIONS (1<<6)
42
43
static inline bool php_must_emit_ini_deprecation(int stage)
44
79
{
45
79
  return stage != ZEND_INI_STAGE_DEACTIVATE && stage != ZEND_INI_STAGE_SHUTDOWN && stage != ZEND_INI_STAGE_ASSERT_OPTIONS;
46
79
}
47
48
static PHP_INI_MH(OnChangeCallback) /* {{{ */
49
14
{
50
14
  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
14
  } else {
62
14
    if (ASSERTG(cb)) {
63
0
      pefree(ASSERTG(cb), 1);
64
0
    }
65
14
    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
14
    } else {
73
14
      ASSERTG(cb) = NULL;
74
14
    }
75
14
  }
76
14
  return SUCCESS;
77
14
}
78
/* }}} */
79
80
static PHP_INI_MH(OnUpdateActiveBool)
81
14
{
82
14
  bool *p = (bool *) ZEND_INI_GET_ADDR();
83
14
  *p = zend_ini_parse_bool(new_value);
84
14
  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
14
  return SUCCESS;
88
14
}
89
90
static PHP_INI_MH(OnUpdateBailBool)
91
22
{
92
22
  bool *p = (bool *) ZEND_INI_GET_ADDR();
93
22
  *p = zend_ini_parse_bool(new_value);
94
22
  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
22
  return SUCCESS;
98
22
}
99
100
static PHP_INI_MH(OnUpdateExceptionBool)
101
29
{
102
29
  bool *p = (bool *) ZEND_INI_GET_ADDR();
103
29
  *p = zend_ini_parse_bool(new_value);
104
29
  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
29
  return SUCCESS;
108
29
}
109
110
111
static PHP_INI_MH(OnUpdateWarningBool)
112
14
{
113
14
  bool *p = (bool *) ZEND_INI_GET_ADDR();
114
14
  *p = zend_ini_parse_bool(new_value);
115
14
  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
14
  return SUCCESS;
119
14
}
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
14
{
132
14
  ZVAL_UNDEF(&assert_globals_p->callback);
133
14
  assert_globals_p->cb = NULL;
134
14
}
135
/* }}} */
136
137
PHP_MINIT_FUNCTION(assert) /* {{{ */
138
14
{
139
14
  ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL);
140
141
14
  REGISTER_INI_ENTRIES();
142
143
14
  return SUCCESS;
144
14
}
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
222k
{
159
222k
  if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
160
3
    zval_ptr_dtor(&ASSERTG(callback));
161
3
    ZVAL_UNDEF(&ASSERTG(callback));
162
3
  }
163
164
222k
  return SUCCESS;
165
222k
}
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
351
{
177
351
  zval *assertion;
178
351
  zend_string *description_str = NULL;
179
351
  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
351
  if (!ASSERTG(active) || EG(assertions) <= 0) {
186
0
    RETURN_TRUE;
187
0
  }
188
189
1.05k
  ZEND_PARSE_PARAMETERS_START(1, 2)
190
1.40k
    Z_PARAM_ZVAL(assertion)
191
1.40k
    Z_PARAM_OPTIONAL
192
1.75k
    Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL(description_obj, zend_ce_throwable, description_str)
193
1.75k
  ZEND_PARSE_PARAMETERS_END();
194
195
351
  if (zend_is_true(assertion)) {
196
219
    RETURN_TRUE;
197
219
  }
198
199
132
  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
132
  if (Z_TYPE(ASSERTG(callback)) == IS_UNDEF && ASSERTG(cb)) {
206
0
    ZVAL_STRING(&ASSERTG(callback), ASSERTG(cb));
207
0
  }
208
209
132
  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
132
  if (ASSERTG(exception)) {
235
127
    zend_throw_exception(assertion_error_ce, description_str ? ZSTR_VAL(description_str) : NULL, E_ERROR);
236
127
    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
127
  } else if (ASSERTG(warning)) {
241
5
    php_error_docref(NULL, E_WARNING, "%s failed", description_str ? ZSTR_VAL(description_str) : "Assertion");
242
5
  }
243
244
132
  if (ASSERTG(bail)) {
245
4
    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
4
    zend_throw_unwind_exit();
251
4
    RETURN_THROWS();
252
128
  } else {
253
128
    RETURN_FALSE;
254
128
  }
255
132
}
256
/* }}} */
257
258
/* {{{ Set/get the various assert flags */
259
PHP_FUNCTION(assert_options)
260
16
{
261
16
  zval *value = NULL;
262
16
  zend_long what;
263
16
  bool oldint;
264
16
  uint32_t ac = ZEND_NUM_ARGS();
265
16
  zend_string *key;
266
267
48
  ZEND_PARSE_PARAMETERS_START(1, 2)
268
64
    Z_PARAM_LONG(what)
269
16
    Z_PARAM_OPTIONAL
270
64
    Z_PARAM_ZVAL(value)
271
64
  ZEND_PARSE_PARAMETERS_END();
272
273
16
  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
4
  case PHP_ASSERT_BAIL:
291
4
    oldint = ASSERTG(bail);
292
4
    if (ac == 2) {
293
4
      zend_string *value_str = zval_try_get_string(value);
294
4
      if (UNEXPECTED(!value_str)) {
295
0
        RETURN_THROWS();
296
0
      }
297
298
4
      key = ZSTR_INIT_LITERAL("assert.bail", 0);
299
4
      zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
300
4
      zend_string_release_ex(key, 0);
301
4
      zend_string_release_ex(value_str, 0);
302
4
    }
303
4
    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
9
  case PHP_ASSERT_EXCEPTION:
342
9
    oldint = ASSERTG(exception);
343
9
    if (ac == 2) {
344
9
      zend_string *val = zval_try_get_string(value);
345
9
      if (UNEXPECTED(!val)) {
346
0
        RETURN_THROWS();
347
0
      }
348
349
9
      key = ZSTR_INIT_LITERAL("assert.exception", 0);
350
9
      zend_alter_ini_entry_ex(key, val, PHP_INI_USER, ZEND_INI_STAGE_ASSERT_OPTIONS, 0);
351
9
      zend_string_release_ex(val, 0);
352
9
      zend_string_release_ex(key, 0);
353
9
    }
354
9
    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
16
  }
361
16
}
362
/* }}} */