xref: /php-src/Zend/zend_variables.c (revision 33104634)
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 
rc_dtor_func(zend_refcounted * p)54 ZEND_API void ZEND_FASTCALL rc_dtor_func(zend_refcounted *p)
55 {
56 	ZEND_ASSERT(GC_TYPE(p) <= IS_CONSTANT_AST);
57 	zend_rc_dtor_func[GC_TYPE(p)](p);
58 }
59 
60 #if ZEND_DEBUG
zend_string_destroy(zend_string * str)61 static void ZEND_FASTCALL zend_string_destroy(zend_string *str)
62 {
63 	CHECK_ZVAL_STRING(str);
64 	ZEND_ASSERT(!ZSTR_IS_INTERNED(str));
65 	ZEND_ASSERT(GC_REFCOUNT(str) == 0);
66 	ZEND_ASSERT(!(GC_FLAGS(str) & IS_STR_PERSISTENT));
67 	efree(str);
68 }
69 #endif
70 
zend_reference_destroy(zend_reference * ref)71 static void ZEND_FASTCALL zend_reference_destroy(zend_reference *ref)
72 {
73 	ZEND_ASSERT(!ZEND_REF_HAS_TYPE_SOURCES(ref));
74 	i_zval_ptr_dtor(&ref->val);
75 	efree_size(ref, sizeof(zend_reference));
76 }
77 
zend_empty_destroy(zend_reference * ref)78 static void ZEND_FASTCALL zend_empty_destroy(zend_reference *ref)
79 {
80 }
81 
zval_ptr_dtor(zval * zval_ptr)82 ZEND_API void zval_ptr_dtor(zval *zval_ptr) /* {{{ */
83 {
84 	i_zval_ptr_dtor(zval_ptr);
85 }
86 /* }}} */
87 
zval_internal_ptr_dtor(zval * zval_ptr)88 ZEND_API void zval_internal_ptr_dtor(zval *zval_ptr) /* {{{ */
89 {
90 	if (Z_REFCOUNTED_P(zval_ptr)) {
91 		zend_refcounted *ref = Z_COUNTED_P(zval_ptr);
92 
93 		if (GC_DELREF(ref) == 0) {
94 			if (Z_TYPE_P(zval_ptr) == IS_STRING) {
95 				zend_string *str = (zend_string*)ref;
96 
97 				CHECK_ZVAL_STRING(str);
98 				ZEND_ASSERT(!ZSTR_IS_INTERNED(str));
99 				ZEND_ASSERT((GC_FLAGS(str) & IS_STR_PERSISTENT));
100 				free(str);
101 			} else {
102 				zend_error_noreturn(E_CORE_ERROR, "Internal zval's can't be arrays, objects, resources or reference");
103 			}
104 		}
105 	}
106 }
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. */
zval_add_ref(zval * p)113 ZEND_API void zval_add_ref(zval *p)
114 {
115 	if (Z_REFCOUNTED_P(p)) {
116 		if (Z_ISREF_P(p) && Z_REFCOUNT_P(p) == 1) {
117 			ZVAL_COPY(p, Z_REFVAL_P(p));
118 		} else {
119 			Z_ADDREF_P(p);
120 		}
121 	}
122 }
123 
zval_copy_ctor_func(zval * zvalue)124 ZEND_API void ZEND_FASTCALL zval_copy_ctor_func(zval *zvalue)
125 {
126 	if (EXPECTED(Z_TYPE_P(zvalue) == IS_ARRAY)) {
127 		ZVAL_ARR(zvalue, zend_array_dup(Z_ARRVAL_P(zvalue)));
128 	} else if (EXPECTED(Z_TYPE_P(zvalue) == IS_STRING)) {
129 		ZEND_ASSERT(!ZSTR_IS_INTERNED(Z_STR_P(zvalue)));
130 		CHECK_ZVAL_STRING(Z_STR_P(zvalue));
131 		ZVAL_NEW_STR(zvalue, zend_string_dup(Z_STR_P(zvalue), 0));
132 	} else {
133 		ZEND_UNREACHABLE();
134 	}
135 }
136