Coverage Report

Created: 2022-02-19 20:28

/src/php-src/Zend/zend_variables.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 2.00 of the Zend license,     |
8
   | that is bundled with this package in the file LICENSE, and is        |
9
   | available through the world-wide-web at the following url:           |
10
   | http://www.zend.com/license/2_00.txt.                                |
11
   | If you did not receive a copy of the Zend license and are unable to  |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@zend.com so we can mail you a copy immediately.              |
14
   +----------------------------------------------------------------------+
15
   | Authors: Andi Gutmans <andi@php.net>                                 |
16
   |          Zeev Suraski <zeev@php.net>                                 |
17
   |          Dmitry Stogov <dmitry@php.net>                              |
18
   +----------------------------------------------------------------------+
19
*/
20
21
#include <stdio.h>
22
#include "zend.h"
23
#include "zend_API.h"
24
#include "zend_ast.h"
25
#include "zend_globals.h"
26
#include "zend_constants.h"
27
#include "zend_list.h"
28
29
#if ZEND_DEBUG
30
static void ZEND_FASTCALL zend_string_destroy(zend_string *str);
31
#else
32
# define zend_string_destroy _efree
33
#endif
34
static void ZEND_FASTCALL zend_reference_destroy(zend_reference *ref);
35
static void ZEND_FASTCALL zend_empty_destroy(zend_reference *ref);
36
37
typedef void (ZEND_FASTCALL *zend_rc_dtor_func_t)(zend_refcounted *p);
38
39
static const zend_rc_dtor_func_t zend_rc_dtor_func[] = {
40
  /* IS_UNDEF        */ (zend_rc_dtor_func_t)zend_empty_destroy,
41
  /* IS_NULL         */ (zend_rc_dtor_func_t)zend_empty_destroy,
42
  /* IS_FALSE        */ (zend_rc_dtor_func_t)zend_empty_destroy,
43
  /* IS_TRUE         */ (zend_rc_dtor_func_t)zend_empty_destroy,
44
  /* IS_LONG         */ (zend_rc_dtor_func_t)zend_empty_destroy,
45
  /* IS_DOUBLE       */ (zend_rc_dtor_func_t)zend_empty_destroy,
46
  /* IS_STRING       */ (zend_rc_dtor_func_t)zend_string_destroy,
47
  /* IS_ARRAY        */ (zend_rc_dtor_func_t)zend_array_destroy,
48
  /* IS_OBJECT       */ (zend_rc_dtor_func_t)zend_objects_store_del,
49
  /* IS_RESOURCE     */ (zend_rc_dtor_func_t)zend_list_free,
50
  /* IS_REFERENCE    */ (zend_rc_dtor_func_t)zend_reference_destroy,
51
  /* IS_CONSTANT_AST */ (zend_rc_dtor_func_t)zend_ast_ref_destroy
52
};
53
54
ZEND_API void ZEND_FASTCALL rc_dtor_func(zend_refcounted *p)
55
16.2M
{
56
16.2M
  ZEND_ASSERT(GC_TYPE(p) <= IS_CONSTANT_AST);
57
16.2M
  zend_rc_dtor_func[GC_TYPE(p)](p);
58
16.2M
}
59
60
#if ZEND_DEBUG
61
static void ZEND_FASTCALL zend_string_destroy(zend_string *str)
62
10.4M
{
63
10.4M
  CHECK_ZVAL_STRING(str);
64
10.4M
  ZEND_ASSERT(!ZSTR_IS_INTERNED(str));
65
10.4M
  ZEND_ASSERT(GC_REFCOUNT(str) == 0);
66
10.4M
  ZEND_ASSERT(!(GC_FLAGS(str) & IS_STR_PERSISTENT));
67
10.4M
  efree(str);
68
10.4M
}
69
#endif
70
71
static void ZEND_FASTCALL zend_reference_destroy(zend_reference *ref)
72
249k
{
73
249k
  ZEND_ASSERT(!ZEND_REF_HAS_TYPE_SOURCES(ref));
74
249k
  i_zval_ptr_dtor(&ref->val);
75
249k
  efree_size(ref, sizeof(zend_reference));
76
249k
}
77
78
static void ZEND_FASTCALL zend_empty_destroy(zend_reference *ref)
79
24.5k
{
80
24.5k
}
81
82
ZEND_API void zval_ptr_dtor(zval *zval_ptr) /* {{{ */
83
17.1M
{
84
17.1M
  i_zval_ptr_dtor(zval_ptr);
85
17.1M
}
86
/* }}} */
87
88
ZEND_API void zval_internal_ptr_dtor(zval *zval_ptr) /* {{{ */
89
0
{
90
0
  if (Z_REFCOUNTED_P(zval_ptr)) {
91
0
    zend_refcounted *ref = Z_COUNTED_P(zval_ptr);
92
93
0
    if (GC_DELREF(ref) == 0) {
94
0
      if (Z_TYPE_P(zval_ptr) == IS_STRING) {
95
0
        zend_string *str = (zend_string*)ref;
96
97
0
        CHECK_ZVAL_STRING(str);
98
0
        ZEND_ASSERT(!ZSTR_IS_INTERNED(str));
99
0
        ZEND_ASSERT((GC_FLAGS(str) & IS_STR_PERSISTENT));
100
0
        free(str);
101
0
      } else {
102
0
        zend_error_noreturn(E_CORE_ERROR, "Internal zval's can't be arrays, objects, resources or reference");
103
0
      }
104
0
    }
105
0
  }
106
0
}
107
/* }}} */
108
109
/* This function should only be used as a copy constructor, i.e. it
110
 * should only be called AFTER a zval has been copied to another
111
 * location using ZVAL_COPY_VALUE. Do not call it before copying,
112
 * otherwise a reference may be leaked. */
113
ZEND_API void zval_add_ref(zval *p)
114
1.32k
{
115
1.32k
  if (Z_REFCOUNTED_P(p)) {
116
126
    if (Z_ISREF_P(p) && Z_REFCOUNT_P(p) == 1) {
117
32
      ZVAL_COPY(p, Z_REFVAL_P(p));
118
94
    } else {
119
94
      Z_ADDREF_P(p);
120
94
    }
121
126
  }
122
1.32k
}
123
124
ZEND_API void ZEND_FASTCALL zval_copy_ctor_func(zval *zvalue)
125
0
{
126
0
  if (EXPECTED(Z_TYPE_P(zvalue) == IS_ARRAY)) {
127
0
    ZVAL_ARR(zvalue, zend_array_dup(Z_ARRVAL_P(zvalue)));
128
0
  } else if (EXPECTED(Z_TYPE_P(zvalue) == IS_STRING)) {
129
0
    ZEND_ASSERT(!ZSTR_IS_INTERNED(Z_STR_P(zvalue)));
130
0
    CHECK_ZVAL_STRING(Z_STR_P(zvalue));
131
0
    ZVAL_NEW_STR(zvalue, zend_string_dup(Z_STR_P(zvalue), 0));
132
0
  }
133
0
}