xref: /PHP-5.6/Zend/zend_object_handlers.c (revision 1cd566e1)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2016 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@zend.com>                                |
16    |          Zeev Suraski <zeev@zend.com>                                |
17    +----------------------------------------------------------------------+
18 */
19 
20 /* $Id$ */
21 
22 #include "zend.h"
23 #include "zend_globals.h"
24 #include "zend_variables.h"
25 #include "zend_API.h"
26 #include "zend_objects.h"
27 #include "zend_objects_API.h"
28 #include "zend_object_handlers.h"
29 #include "zend_interfaces.h"
30 #include "zend_closures.h"
31 #include "zend_compile.h"
32 
33 #define DEBUG_OBJECT_HANDLERS 0
34 
35 #define Z_OBJ_P(zval_p) \
36 	((zend_object*)(EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zval_p)].bucket.obj.object))
37 
38 #define Z_OBJ_PROTECT_RECURSION(zval_p) \
39 	do { \
40 		if (EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zval_p)].apply_count++ >= 3) { \
41 			zend_error(E_ERROR, "Nesting level too deep - recursive dependency?"); \
42 		} \
43 	} while (0)
44 
45 
46 #define Z_OBJ_UNPROTECT_RECURSION(zval_p) \
47 	EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zval_p)].apply_count--
48 
49 /*
50   __X accessors explanation:
51 
52   if we have __get and property that is not part of the properties array is
53   requested, we call __get handler. If it fails, we return uninitialized.
54 
55   if we have __set and property that is not part of the properties array is
56   set, we call __set handler. If it fails, we do not change the array.
57 
58   for both handlers above, when we are inside __get/__set, no further calls for
59   __get/__set for this property of this object will be made, to prevent endless
60   recursion and enable accessors to change properties array.
61 
62   if we have __call and method which is not part of the class function table is
63   called, we cal __call handler.
64 */
65 
rebuild_object_properties(zend_object * zobj)66 ZEND_API void rebuild_object_properties(zend_object *zobj) /* {{{ */
67 {
68 	if (!zobj->properties) {
69 		HashPosition pos;
70 		zend_property_info *prop_info;
71 		zend_class_entry *ce = zobj->ce;
72 
73 		ALLOC_HASHTABLE(zobj->properties);
74 		zend_hash_init(zobj->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
75 		if (ce->default_properties_count) {
76 			for (zend_hash_internal_pointer_reset_ex(&ce->properties_info, &pos);
77 			     zend_hash_get_current_data_ex(&ce->properties_info, (void**)&prop_info, &pos) == SUCCESS;
78 			     zend_hash_move_forward_ex(&ce->properties_info, &pos)) {
79 				if (/*prop_info->ce == ce &&*/
80 				    (prop_info->flags & ZEND_ACC_STATIC) == 0 &&
81 				    prop_info->offset >= 0 &&
82 				    zobj->properties_table[prop_info->offset]) {
83 					zend_hash_quick_add(zobj->properties, prop_info->name, prop_info->name_length+1, prop_info->h, (void**)&zobj->properties_table[prop_info->offset], sizeof(zval*), (void**)&zobj->properties_table[prop_info->offset]);
84 				}
85 			}
86 			while (ce->parent && ce->parent->default_properties_count) {
87 				ce = ce->parent;
88 				for (zend_hash_internal_pointer_reset_ex(&ce->properties_info, &pos);
89 				     zend_hash_get_current_data_ex(&ce->properties_info, (void**)&prop_info, &pos) == SUCCESS;
90 				     zend_hash_move_forward_ex(&ce->properties_info, &pos)) {
91 					if (prop_info->ce == ce &&
92 					    (prop_info->flags & ZEND_ACC_STATIC) == 0 &&
93 					    (prop_info->flags & ZEND_ACC_PRIVATE) != 0 &&
94 					    prop_info->offset >= 0 &&
95 						zobj->properties_table[prop_info->offset]) {
96 						zend_hash_quick_add(zobj->properties, prop_info->name, prop_info->name_length+1, prop_info->h, (void**)&zobj->properties_table[prop_info->offset], sizeof(zval*), (void**)&zobj->properties_table[prop_info->offset]);
97 					}
98 				}
99 			}
100 		}
101 	}
102 }
103 /* }}} */
104 
zend_std_get_properties(zval * object TSRMLS_DC)105 ZEND_API HashTable *zend_std_get_properties(zval *object TSRMLS_DC) /* {{{ */
106 {
107 	zend_object *zobj;
108 	zobj = Z_OBJ_P(object);
109 	if (!zobj->properties) {
110 		rebuild_object_properties(zobj);
111 	}
112 	return zobj->properties;
113 }
114 /* }}} */
115 
zend_std_get_gc(zval * object,zval *** table,int * n TSRMLS_DC)116 ZEND_API HashTable *zend_std_get_gc(zval *object, zval ***table, int *n TSRMLS_DC) /* {{{ */
117 {
118 	if (Z_OBJ_HANDLER_P(object, get_properties) != zend_std_get_properties) {
119 		*table = NULL;
120 		*n = 0;
121 		return Z_OBJ_HANDLER_P(object, get_properties)(object TSRMLS_CC);
122 	} else {
123 		zend_object *zobj = Z_OBJ_P(object);
124 
125 		if (zobj->properties) {
126 			*table = NULL;
127 			*n = 0;
128 			return zobj->properties;
129 		} else {
130 			*table = zobj->properties_table;
131 			*n = zobj->ce->default_properties_count;
132 			return NULL;
133 		}
134 	}
135 }
136 /* }}} */
137 
zend_std_get_debug_info(zval * object,int * is_temp TSRMLS_DC)138 ZEND_API HashTable *zend_std_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
139 {
140 	zend_class_entry *ce = Z_OBJCE_P(object);
141 	zval *retval = NULL;
142 
143 	if (!ce->__debugInfo) {
144 		*is_temp = 0;
145 		return Z_OBJ_HANDLER_P(object, get_properties)
146 			? Z_OBJ_HANDLER_P(object, get_properties)(object TSRMLS_CC)
147 			: NULL;
148 	}
149 
150 	zend_call_method_with_0_params(&object, ce, &ce->__debugInfo, ZEND_DEBUGINFO_FUNC_NAME, &retval);
151 	if (retval && Z_TYPE_P(retval) == IS_ARRAY) {
152 		HashTable *ht = Z_ARRVAL_P(retval);
153 		if (Z_REFCOUNT_P(retval) <= 1) {
154 			*is_temp = 1;
155 			efree(retval);
156 			return ht;
157 		} else {
158 			*is_temp = 0;
159 			zval_ptr_dtor(&retval);
160 		}
161 		return ht;
162 	}
163 	if (retval && Z_TYPE_P(retval) == IS_NULL) {
164 		zval ret;
165 		array_init(&ret);
166 		*is_temp = 1;
167 		zval_ptr_dtor(&retval);
168 		return Z_ARRVAL(ret);
169 	}
170 
171 	zend_error_noreturn(E_ERROR, ZEND_DEBUGINFO_FUNC_NAME "() must return an array");
172 
173 	return NULL; /* Compilers are dumb and don't understand that noreturn means that the function does NOT need a return value... */
174 }
175 /* }}} */
176 
zend_std_call_getter(zval * object,zval * member TSRMLS_DC)177 static zval *zend_std_call_getter(zval *object, zval *member TSRMLS_DC) /* {{{ */
178 {
179 	zval *retval = NULL;
180 	zend_class_entry *ce = Z_OBJCE_P(object);
181 
182 	/* __get handler is called with one argument:
183 	      property name
184 
185 	   it should return whether the call was successfull or not
186 	*/
187 
188 	SEPARATE_ARG_IF_REF(member);
189 
190 	zend_call_method_with_1_params(&object, ce, &ce->__get, ZEND_GET_FUNC_NAME, &retval, member);
191 
192 	zval_ptr_dtor(&member);
193 
194 	if (retval) {
195 		Z_DELREF_P(retval);
196 	}
197 
198 	return retval;
199 }
200 /* }}} */
201 
zend_std_call_setter(zval * object,zval * member,zval * value TSRMLS_DC)202 static int zend_std_call_setter(zval *object, zval *member, zval *value TSRMLS_DC) /* {{{ */
203 {
204 	zval *retval = NULL;
205 	int result;
206 	zend_class_entry *ce = Z_OBJCE_P(object);
207 
208 	SEPARATE_ARG_IF_REF(member);
209 	Z_ADDREF_P(value);
210 
211 	/* __set handler is called with two arguments:
212 	     property name
213 	     value to be set
214 
215 	   it should return whether the call was successfull or not
216 	*/
217 	zend_call_method_with_2_params(&object, ce, &ce->__set, ZEND_SET_FUNC_NAME, &retval, member, value);
218 
219 	zval_ptr_dtor(&member);
220 	zval_ptr_dtor(&value);
221 
222 	if (retval) {
223 		result = i_zend_is_true(retval) ? SUCCESS : FAILURE;
224 		zval_ptr_dtor(&retval);
225 		return result;
226 	} else {
227 		return FAILURE;
228 	}
229 }
230 /* }}} */
231 
zend_std_call_unsetter(zval * object,zval * member TSRMLS_DC)232 static void zend_std_call_unsetter(zval *object, zval *member TSRMLS_DC) /* {{{ */
233 {
234 	zend_class_entry *ce = Z_OBJCE_P(object);
235 
236 	/* __unset handler is called with one argument:
237 	      property name
238 	*/
239 
240 	SEPARATE_ARG_IF_REF(member);
241 
242 	zend_call_method_with_1_params(&object, ce, &ce->__unset, ZEND_UNSET_FUNC_NAME, NULL, member);
243 
244 	zval_ptr_dtor(&member);
245 }
246 /* }}} */
247 
zend_std_call_issetter(zval * object,zval * member TSRMLS_DC)248 static zval *zend_std_call_issetter(zval *object, zval *member TSRMLS_DC) /* {{{ */
249 {
250 	zval *retval = NULL;
251 	zend_class_entry *ce = Z_OBJCE_P(object);
252 
253 	/* __isset handler is called with one argument:
254 	      property name
255 
256 	   it should return whether the property is set or not
257 	*/
258 
259 	SEPARATE_ARG_IF_REF(member);
260 
261 	zend_call_method_with_1_params(&object, ce, &ce->__isset, ZEND_ISSET_FUNC_NAME, &retval, member);
262 
263 	zval_ptr_dtor(&member);
264 
265 	return retval;
266 }
267 /* }}} */
268 
zend_verify_property_access(zend_property_info * property_info,zend_class_entry * ce TSRMLS_DC)269 static zend_always_inline int zend_verify_property_access(zend_property_info *property_info, zend_class_entry *ce TSRMLS_DC) /* {{{ */
270 {
271 	switch (property_info->flags & ZEND_ACC_PPP_MASK) {
272 		case ZEND_ACC_PUBLIC:
273 			return 1;
274 		case ZEND_ACC_PROTECTED:
275 			return zend_check_protected(property_info->ce, EG(scope));
276 		case ZEND_ACC_PRIVATE:
277 			if ((ce==EG(scope) || property_info->ce == EG(scope)) && EG(scope)) {
278 				return 1;
279 			} else {
280 				return 0;
281 			}
282 			break;
283 	}
284 	return 0;
285 }
286 /* }}} */
287 
is_derived_class(zend_class_entry * child_class,zend_class_entry * parent_class)288 static zend_always_inline zend_bool is_derived_class(zend_class_entry *child_class, zend_class_entry *parent_class) /* {{{ */
289 {
290 	child_class = child_class->parent;
291 	while (child_class) {
292 		if (child_class == parent_class) {
293 			return 1;
294 		}
295 		child_class = child_class->parent;
296 	}
297 
298 	return 0;
299 }
300 /* }}} */
301 
zend_get_property_info_quick(zend_class_entry * ce,zval * member,int silent,const zend_literal * key TSRMLS_DC)302 static zend_always_inline struct _zend_property_info *zend_get_property_info_quick(zend_class_entry *ce, zval *member, int silent, const zend_literal *key TSRMLS_DC) /* {{{ */
303 {
304 	zend_property_info *property_info;
305 	zend_property_info *scope_property_info;
306 	zend_bool denied_access = 0;
307 	ulong h;
308 
309 	if (key && (property_info = CACHED_POLYMORPHIC_PTR(key->cache_slot, ce)) != NULL) {
310 		return property_info;
311 	}
312 
313 	if (UNEXPECTED(Z_STRVAL_P(member)[0] == '\0')) {
314 		if (!silent) {
315 			if (Z_STRLEN_P(member) == 0) {
316 				zend_error_noreturn(E_ERROR, "Cannot access empty property");
317 			} else {
318 				zend_error_noreturn(E_ERROR, "Cannot access property started with '\\0'");
319 			}
320 		}
321 		return NULL;
322 	}
323 	property_info = NULL;
324 	h = key ? key->hash_value : zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
325 	if (zend_hash_quick_find(&ce->properties_info, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, h, (void **) &property_info)==SUCCESS) {
326 		if (UNEXPECTED((property_info->flags & ZEND_ACC_SHADOW) != 0)) {
327 			/* if it's a shadow - go to access it's private */
328 			property_info = NULL;
329 		} else {
330 			if (EXPECTED(zend_verify_property_access(property_info, ce TSRMLS_CC) != 0)) {
331 				if (EXPECTED((property_info->flags & ZEND_ACC_CHANGED) != 0)
332 					&& EXPECTED(!(property_info->flags & ZEND_ACC_PRIVATE))) {
333 					/* We still need to make sure that we're not in a context
334 					 * where the right property is a different 'statically linked' private
335 					 * continue checking below...
336 					 */
337 				} else {
338 					if (UNEXPECTED((property_info->flags & ZEND_ACC_STATIC) != 0) && !silent) {
339 						zend_error(E_STRICT, "Accessing static property %s::$%s as non static", ce->name, Z_STRVAL_P(member));
340 					}
341 					if (key) {
342 						CACHE_POLYMORPHIC_PTR(key->cache_slot, ce, property_info);
343 					}
344 					return property_info;
345 				}
346 			} else {
347 				/* Try to look in the scope instead */
348 				denied_access = 1;
349 			}
350 		}
351 	}
352 	if (EG(scope) != ce
353 		&& EG(scope)
354 		&& is_derived_class(ce, EG(scope))
355 		&& zend_hash_quick_find(&EG(scope)->properties_info, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, h, (void **) &scope_property_info)==SUCCESS
356 		&& scope_property_info->flags & ZEND_ACC_PRIVATE) {
357 		if (key) {
358 			CACHE_POLYMORPHIC_PTR(key->cache_slot, ce, scope_property_info);
359 		}
360 		return scope_property_info;
361 	} else if (property_info) {
362 		if (UNEXPECTED(denied_access != 0)) {
363 			/* Information was available, but we were denied access.  Error out. */
364 			if (!silent) {
365 				zend_error_noreturn(E_ERROR, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ce->name, Z_STRVAL_P(member));
366 			}
367 			return NULL;
368 		} else {
369 			/* fall through, return property_info... */
370 			if (key) {
371 				CACHE_POLYMORPHIC_PTR(key->cache_slot, ce, property_info);
372 			}
373 		}
374 	} else {
375 		EG(std_property_info).flags = ZEND_ACC_PUBLIC;
376 		EG(std_property_info).name = Z_STRVAL_P(member);
377 		EG(std_property_info).name_length = Z_STRLEN_P(member);
378 		EG(std_property_info).h = h;
379 		EG(std_property_info).ce = ce;
380 		EG(std_property_info).offset = -1;
381 		property_info = &EG(std_property_info);
382 	}
383 	return property_info;
384 }
385 /* }}} */
386 
zend_get_property_info(zend_class_entry * ce,zval * member,int silent TSRMLS_DC)387 ZEND_API struct _zend_property_info *zend_get_property_info(zend_class_entry *ce, zval *member, int silent TSRMLS_DC) /* {{{ */
388 {
389 	return zend_get_property_info_quick(ce, member, silent, NULL TSRMLS_CC);
390 }
391 /* }}} */
392 
zend_check_property_access(zend_object * zobj,const char * prop_info_name,int prop_info_name_len TSRMLS_DC)393 ZEND_API int zend_check_property_access(zend_object *zobj, const char *prop_info_name, int prop_info_name_len TSRMLS_DC) /* {{{ */
394 {
395 	zend_property_info *property_info;
396 	const char *class_name, *prop_name;
397 	zval member;
398 	int prop_name_len;
399 
400 	zend_unmangle_property_name_ex(prop_info_name, prop_info_name_len, &class_name, &prop_name, &prop_name_len);
401 	ZVAL_STRINGL(&member, prop_name, prop_name_len, 0);
402 	property_info = zend_get_property_info_quick(zobj->ce, &member, 1, NULL TSRMLS_CC);
403 	if (!property_info) {
404 		return FAILURE;
405 	}
406 	if (class_name && class_name[0] != '*') {
407 		if (!(property_info->flags & ZEND_ACC_PRIVATE)) {
408 			/* we we're looking for a private prop but found a non private one of the same name */
409 			return FAILURE;
410 		} else if (strcmp(prop_info_name+1, property_info->name+1)) {
411 			/* we we're looking for a private prop but found a private one of the same name but another class */
412 			return FAILURE;
413 		}
414 	}
415 	return zend_verify_property_access(property_info, zobj->ce TSRMLS_CC) ? SUCCESS : FAILURE;
416 }
417 /* }}} */
418 
zend_get_property_guard(zend_object * zobj,zend_property_info * property_info,zval * member,zend_guard ** pguard)419 static int zend_get_property_guard(zend_object *zobj, zend_property_info *property_info, zval *member, zend_guard **pguard) /* {{{ */
420 {
421 	zend_property_info info;
422 	zend_guard stub;
423 
424 	if (!property_info) {
425 		property_info = &info;
426 		info.name = Z_STRVAL_P(member);
427 		info.name_length = Z_STRLEN_P(member);
428 		info.h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
429 	} else if(property_info->name[0] == '\0'){
430 		const char *class_name = NULL, *prop_name = NULL;
431 		zend_unmangle_property_name(property_info->name, property_info->name_length, &class_name, &prop_name);
432 		if(class_name) {
433 			/* use unmangled name for protected properties */
434 			info.name = prop_name;
435 			info.name_length = strlen(prop_name);
436 			info.h = zend_get_hash_value(info.name, info.name_length+1);
437 			property_info = &info;
438 		}
439 	}
440 	if (!zobj->guards) {
441 		ALLOC_HASHTABLE(zobj->guards);
442 		zend_hash_init(zobj->guards, 0, NULL, NULL, 0);
443 	} else if (zend_hash_quick_find(zobj->guards, property_info->name, property_info->name_length+1, property_info->h, (void **) pguard) == SUCCESS) {
444 		return SUCCESS;
445 	}
446 	stub.in_get = 0;
447 	stub.in_set = 0;
448 	stub.in_unset = 0;
449 	stub.in_isset = 0;
450 	return zend_hash_quick_add(zobj->guards, property_info->name, property_info->name_length+1, property_info->h, (void**)&stub, sizeof(stub), (void**) pguard);
451 }
452 /* }}} */
453 
zend_std_read_property(zval * object,zval * member,int type,const zend_literal * key TSRMLS_DC)454 zval *zend_std_read_property(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */
455 {
456 	zend_object *zobj;
457 	zval *tmp_member = NULL;
458 	zval **retval;
459 	zval *rv = NULL;
460 	zend_property_info *property_info;
461 	int silent;
462 
463 	silent = (type == BP_VAR_IS);
464 	zobj = Z_OBJ_P(object);
465 
466 	if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
467 		ALLOC_ZVAL(tmp_member);
468 		*tmp_member = *member;
469 		INIT_PZVAL(tmp_member);
470 		zval_copy_ctor(tmp_member);
471 		convert_to_string(tmp_member);
472 		member = tmp_member;
473 		key = NULL;
474 	}
475 
476 #if DEBUG_OBJECT_HANDLERS
477 	fprintf(stderr, "Read object #%d property: %s\n", Z_OBJ_HANDLE_P(object), Z_STRVAL_P(member));
478 #endif
479 
480 	/* make zend_get_property_info silent if we have getter - we may want to use it */
481 	property_info = zend_get_property_info_quick(zobj->ce, member, silent || (zobj->ce->__get != NULL), key TSRMLS_CC);
482 
483 	if (UNEXPECTED(!property_info) ||
484 	    ((EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
485 	     property_info->offset >= 0) ?
486 	        (zobj->properties ?
487 	            ((retval = (zval**)zobj->properties_table[property_info->offset]) == NULL) :
488 	            (*(retval = &zobj->properties_table[property_info->offset]) == NULL)) :
489 	        (UNEXPECTED(!zobj->properties) ||
490 	          UNEXPECTED(zend_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &retval) == FAILURE)))) {
491 		zend_guard *guard = NULL;
492 
493 		if (zobj->ce->__get &&
494 		    zend_get_property_guard(zobj, property_info, member, &guard) == SUCCESS &&
495 		    !guard->in_get) {
496 			/* have getter - try with it! */
497 			Z_ADDREF_P(object);
498 			if (PZVAL_IS_REF(object)) {
499 				SEPARATE_ZVAL(&object);
500 			}
501 			guard->in_get = 1; /* prevent circular getting */
502 			rv = zend_std_call_getter(object, member TSRMLS_CC);
503 			guard->in_get = 0;
504 
505 			if (rv) {
506 				retval = &rv;
507 				if (!Z_ISREF_P(rv) &&
508 				    (type == BP_VAR_W || type == BP_VAR_RW  || type == BP_VAR_UNSET)) {
509 					if (Z_REFCOUNT_P(rv) > 0) {
510 						zval *tmp = rv;
511 
512 						ALLOC_ZVAL(rv);
513 						*rv = *tmp;
514 						zval_copy_ctor(rv);
515 						Z_UNSET_ISREF_P(rv);
516 						Z_SET_REFCOUNT_P(rv, 0);
517 					}
518 					if (UNEXPECTED(Z_TYPE_P(rv) != IS_OBJECT)) {
519 						zend_error(E_NOTICE, "Indirect modification of overloaded property %s::$%s has no effect", zobj->ce->name, Z_STRVAL_P(member));
520 					}
521 				}
522 			} else {
523 				retval = &EG(uninitialized_zval_ptr);
524 			}
525 			if (EXPECTED(*retval != object)) {
526 				zval_ptr_dtor(&object);
527 			} else {
528 				Z_DELREF_P(object);
529 			}
530 		} else {
531 			if (zobj->ce->__get && guard && guard->in_get == 1) {
532 				if (Z_STRVAL_P(member)[0] == '\0') {
533 					if (Z_STRLEN_P(member) == 0) {
534 						zend_error(E_ERROR, "Cannot access empty property");
535 					} else {
536 						zend_error(E_ERROR, "Cannot access property started with '\\0'");
537 					}
538 				}
539 			}
540 			if (!silent) {
541 				zend_error(E_NOTICE,"Undefined property: %s::$%s", zobj->ce->name, Z_STRVAL_P(member));
542 			}
543 			retval = &EG(uninitialized_zval_ptr);
544 		}
545 	}
546 	if (UNEXPECTED(tmp_member != NULL)) {
547 		Z_ADDREF_PP(retval);
548 		zval_ptr_dtor(&tmp_member);
549 		Z_DELREF_PP(retval);
550 	}
551 	return *retval;
552 }
553 /* }}} */
554 
zend_std_write_property(zval * object,zval * member,zval * value,const zend_literal * key TSRMLS_DC)555 ZEND_API void zend_std_write_property(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC) /* {{{ */
556 {
557 	zend_object *zobj;
558 	zval *tmp_member = NULL;
559 	zval **variable_ptr;
560 	zend_property_info *property_info;
561 
562 	zobj = Z_OBJ_P(object);
563 
564  	if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
565  		ALLOC_ZVAL(tmp_member);
566 		*tmp_member = *member;
567 		INIT_PZVAL(tmp_member);
568 		zval_copy_ctor(tmp_member);
569 		convert_to_string(tmp_member);
570 		member = tmp_member;
571 		key = NULL;
572 	}
573 
574 	property_info = zend_get_property_info_quick(zobj->ce, member, (zobj->ce->__set != NULL), key TSRMLS_CC);
575 
576 	if (EXPECTED(property_info != NULL) &&
577 	    ((EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
578 	     property_info->offset >= 0) ?
579 	        (zobj->properties ?
580 	            ((variable_ptr = (zval**)zobj->properties_table[property_info->offset]) != NULL) :
581 	            (*(variable_ptr = &zobj->properties_table[property_info->offset]) != NULL)) :
582 	        (EXPECTED(zobj->properties != NULL) &&
583 	          EXPECTED(zend_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &variable_ptr) == SUCCESS)))) {
584 		/* if we already have this value there, we don't actually need to do anything */
585 		if (EXPECTED(*variable_ptr != value)) {
586 			/* if we are assigning reference, we shouldn't move it, but instead assign variable
587 			   to the same pointer */
588 			if (PZVAL_IS_REF(*variable_ptr)) {
589 				zval garbage = **variable_ptr; /* old value should be destroyed */
590 
591 				/* To check: can't *variable_ptr be some system variable like error_zval here? */
592 				Z_TYPE_PP(variable_ptr) = Z_TYPE_P(value);
593 				(*variable_ptr)->value = value->value;
594 				if (Z_REFCOUNT_P(value) > 0) {
595 					zval_copy_ctor(*variable_ptr);
596 				} else {
597 					efree(value);
598 				}
599 				zval_dtor(&garbage);
600 			} else {
601 				zval *garbage = *variable_ptr;
602 
603 				/* if we assign referenced variable, we should separate it */
604 				Z_ADDREF_P(value);
605 				if (PZVAL_IS_REF(value)) {
606 					SEPARATE_ZVAL(&value);
607 				}
608 				*variable_ptr = value;
609 				zval_ptr_dtor(&garbage);
610 			}
611 		}
612 	} else {
613 		zend_guard *guard = NULL;
614 
615 		if (zobj->ce->__set &&
616 		    zend_get_property_guard(zobj, property_info, member, &guard) == SUCCESS &&
617 		    !guard->in_set) {
618 			Z_ADDREF_P(object);
619 			if (PZVAL_IS_REF(object)) {
620 				SEPARATE_ZVAL(&object);
621 			}
622 			guard->in_set = 1; /* prevent circular setting */
623 			if (zend_std_call_setter(object, member, value TSRMLS_CC) != SUCCESS) {
624 				/* for now, just ignore it - __set should take care of warnings, etc. */
625 			}
626 			guard->in_set = 0;
627 			zval_ptr_dtor(&object);
628 		} else if (EXPECTED(property_info != NULL)) {
629 			/* if we assign referenced variable, we should separate it */
630 			Z_ADDREF_P(value);
631 			if (PZVAL_IS_REF(value)) {
632 				SEPARATE_ZVAL(&value);
633 			}
634 			if (EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
635 			    property_info->offset >= 0) {
636 					if (!zobj->properties) {
637 						zobj->properties_table[property_info->offset] = value;
638 					} else if (zobj->properties_table[property_info->offset]) {
639 						*(zval**)zobj->properties_table[property_info->offset] = value;
640 					} else {
641 						zend_hash_quick_update(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, &value, sizeof(zval *), (void**)&zobj->properties_table[property_info->offset]);
642 					}
643 				} else {
644 					if (!zobj->properties) {
645 					rebuild_object_properties(zobj);
646 				}
647 				zend_hash_quick_update(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, &value, sizeof(zval *), NULL);
648 			}
649 		} else if (zobj->ce->__set && guard && guard->in_set == 1) {
650 			if (Z_STRVAL_P(member)[0] == '\0') {
651 				if (Z_STRLEN_P(member) == 0) {
652 					zend_error(E_ERROR, "Cannot access empty property");
653 				} else {
654 					zend_error(E_ERROR, "Cannot access property started with '\\0'");
655 				}
656 			}
657 		}
658 	}
659 
660 	if (UNEXPECTED(tmp_member != NULL)) {
661 		zval_ptr_dtor(&tmp_member);
662 	}
663 }
664 /* }}} */
665 
zend_std_read_dimension(zval * object,zval * offset,int type TSRMLS_DC)666 zval *zend_std_read_dimension(zval *object, zval *offset, int type TSRMLS_DC) /* {{{ */
667 {
668 	zend_class_entry *ce = Z_OBJCE_P(object);
669 	zval *retval;
670 
671 	if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC) != 0)) {
672 		if(offset == NULL) {
673 			/* [] construct */
674 			ALLOC_INIT_ZVAL(offset);
675 		} else {
676 			SEPARATE_ARG_IF_REF(offset);
677 		}
678 		zend_call_method_with_1_params(&object, ce, NULL, "offsetget", &retval, offset);
679 
680 		zval_ptr_dtor(&offset);
681 
682 		if (UNEXPECTED(!retval)) {
683 			if (UNEXPECTED(!EG(exception))) {
684 				zend_error_noreturn(E_ERROR, "Undefined offset for object of type %s used as array", ce->name);
685 			}
686 			return 0;
687 		}
688 
689 		/* Undo PZVAL_LOCK() */
690 		Z_DELREF_P(retval);
691 
692 		return retval;
693 	} else {
694 		zend_error_noreturn(E_ERROR, "Cannot use object of type %s as array", ce->name);
695 		return 0;
696 	}
697 }
698 /* }}} */
699 
zend_std_write_dimension(zval * object,zval * offset,zval * value TSRMLS_DC)700 static void zend_std_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC) /* {{{ */
701 {
702 	zend_class_entry *ce = Z_OBJCE_P(object);
703 
704 	if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC) != 0)) {
705 		if (!offset) {
706 			ALLOC_INIT_ZVAL(offset);
707 		} else {
708 			SEPARATE_ARG_IF_REF(offset);
709 		}
710 		zend_call_method_with_2_params(&object, ce, NULL, "offsetset", NULL, offset, value);
711 		zval_ptr_dtor(&offset);
712 	} else {
713 		zend_error_noreturn(E_ERROR, "Cannot use object of type %s as array", ce->name);
714 	}
715 }
716 /* }}} */
717 
zend_std_has_dimension(zval * object,zval * offset,int check_empty TSRMLS_DC)718 static int zend_std_has_dimension(zval *object, zval *offset, int check_empty TSRMLS_DC) /* {{{ */
719 {
720 	zend_class_entry *ce = Z_OBJCE_P(object);
721 	zval *retval;
722 	int result;
723 
724 	if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC) != 0)) {
725 		SEPARATE_ARG_IF_REF(offset);
726 		zend_call_method_with_1_params(&object, ce, NULL, "offsetexists", &retval, offset);
727 		if (EXPECTED(retval != NULL)) {
728 			result = i_zend_is_true(retval);
729 			zval_ptr_dtor(&retval);
730 			if (check_empty && result && EXPECTED(!EG(exception))) {
731 				zend_call_method_with_1_params(&object, ce, NULL, "offsetget", &retval, offset);
732 				if (retval) {
733 					result = i_zend_is_true(retval);
734 					zval_ptr_dtor(&retval);
735 				}
736 			}
737 		} else {
738 			result = 0;
739 		}
740 		zval_ptr_dtor(&offset);
741 	} else {
742 		zend_error_noreturn(E_ERROR, "Cannot use object of type %s as array", ce->name);
743 		return 0;
744 	}
745 	return result;
746 }
747 /* }}} */
748 
zend_std_get_property_ptr_ptr(zval * object,zval * member,int type,const zend_literal * key TSRMLS_DC)749 static zval **zend_std_get_property_ptr_ptr(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */
750 {
751 	zend_object *zobj;
752 	zval tmp_member;
753 	zval **retval;
754 	zend_property_info *property_info;
755 
756 	zobj = Z_OBJ_P(object);
757 
758  	if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
759 		tmp_member = *member;
760 		zval_copy_ctor(&tmp_member);
761 		convert_to_string(&tmp_member);
762 		member = &tmp_member;
763 		key = NULL;
764 	}
765 
766 #if DEBUG_OBJECT_HANDLERS
767 	fprintf(stderr, "Ptr object #%d property: %s\n", Z_OBJ_HANDLE_P(object), Z_STRVAL_P(member));
768 #endif
769 
770 	property_info = zend_get_property_info_quick(zobj->ce, member, (zobj->ce->__get != NULL), key TSRMLS_CC);
771 
772 	if (UNEXPECTED(!property_info) ||
773 	    ((EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
774 	     property_info->offset >= 0) ?
775 	        (zobj->properties ?
776 	            ((retval = (zval**)zobj->properties_table[property_info->offset]) == NULL) :
777 	            (*(retval = &zobj->properties_table[property_info->offset]) == NULL)) :
778 	        (UNEXPECTED(!zobj->properties) ||
779 	          UNEXPECTED(zend_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &retval) == FAILURE)))) {
780 		zval *new_zval;
781 		zend_guard *guard;
782 
783 		if (!zobj->ce->__get ||
784 			zend_get_property_guard(zobj, property_info, member, &guard) != SUCCESS ||
785 			(property_info && guard->in_get)) {
786 			/* we don't have access controls - will just add it */
787 			new_zval = &EG(uninitialized_zval);
788 
789 			Z_ADDREF_P(new_zval);
790 			if (EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
791 			    property_info->offset >= 0) {
792 				if (!zobj->properties) {
793 					zobj->properties_table[property_info->offset] = new_zval;
794 					retval = &zobj->properties_table[property_info->offset];
795 				} else if (zobj->properties_table[property_info->offset]) {
796 					*(zval**)zobj->properties_table[property_info->offset] = new_zval;
797 					retval = (zval**)zobj->properties_table[property_info->offset];
798 				} else {
799 					zend_hash_quick_update(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, &new_zval, sizeof(zval *), (void**)&zobj->properties_table[property_info->offset]);
800 					retval = (zval**)zobj->properties_table[property_info->offset];
801 				}
802 			} else {
803 				if (!zobj->properties) {
804 					rebuild_object_properties(zobj);
805 				}
806 				zend_hash_quick_update(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, &new_zval, sizeof(zval *), (void **) &retval);
807 			}
808 
809 			/* Notice is thrown after creation of the property, to avoid EG(std_property_info)
810 			 * being overwritten in an error handler. */
811 			if (UNEXPECTED(type == BP_VAR_RW || type == BP_VAR_R)) {
812 				zend_error(E_NOTICE, "Undefined property: %s::$%s", zobj->ce->name, Z_STRVAL_P(member));
813 			}
814 		} else {
815 			/* we do have getter - fail and let it try again with usual get/set */
816 			retval = NULL;
817 		}
818 	}
819 	if (UNEXPECTED(member == &tmp_member)) {
820 		zval_dtor(member);
821 	}
822 	return retval;
823 }
824 /* }}} */
825 
zend_std_unset_property(zval * object,zval * member,const zend_literal * key TSRMLS_DC)826 static void zend_std_unset_property(zval *object, zval *member, const zend_literal *key TSRMLS_DC) /* {{{ */
827 {
828 	zend_object *zobj;
829 	zval *tmp_member = NULL;
830 	zend_property_info *property_info;
831 
832 	zobj = Z_OBJ_P(object);
833 
834  	if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
835  		ALLOC_ZVAL(tmp_member);
836 		*tmp_member = *member;
837 		INIT_PZVAL(tmp_member);
838 		zval_copy_ctor(tmp_member);
839 		convert_to_string(tmp_member);
840 		member = tmp_member;
841 		key = NULL;
842 	}
843 
844 	property_info = zend_get_property_info_quick(zobj->ce, member, (zobj->ce->__unset != NULL), key TSRMLS_CC);
845 
846 	if (EXPECTED(property_info != NULL) &&
847 	    EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
848 	    !zobj->properties &&
849 	    property_info->offset >= 0 &&
850 	    EXPECTED(zobj->properties_table[property_info->offset] != NULL)) {
851 		zval_ptr_dtor(&zobj->properties_table[property_info->offset]);
852 		zobj->properties_table[property_info->offset] = NULL;
853 	} else if (UNEXPECTED(!property_info) ||
854 	           !zobj->properties ||
855 	           UNEXPECTED(zend_hash_quick_del(zobj->properties, property_info->name, property_info->name_length+1, property_info->h) == FAILURE)) {
856 		zend_guard *guard = NULL;
857 
858 		if (zobj->ce->__unset &&
859 		    zend_get_property_guard(zobj, property_info, member, &guard) == SUCCESS &&
860 		    !guard->in_unset) {
861 			/* have unseter - try with it! */
862 			Z_ADDREF_P(object);
863 			if (PZVAL_IS_REF(object)) {
864 				SEPARATE_ZVAL(&object);
865 			}
866 			guard->in_unset = 1; /* prevent circular unsetting */
867 			zend_std_call_unsetter(object, member TSRMLS_CC);
868 			guard->in_unset = 0;
869 			zval_ptr_dtor(&object);
870 		} else if (zobj->ce->__unset && guard && guard->in_unset == 1) {
871 			if (Z_STRVAL_P(member)[0] == '\0') {
872 				if (Z_STRLEN_P(member) == 0) {
873 					zend_error(E_ERROR, "Cannot access empty property");
874 				} else {
875 					zend_error(E_ERROR, "Cannot access property started with '\\0'");
876 				}
877 			}
878 		}
879 	} else if (EXPECTED(property_info != NULL) &&
880 	           EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
881 	           property_info->offset >= 0) {
882 		zobj->properties_table[property_info->offset] = NULL;
883 	}
884 
885 	if (UNEXPECTED(tmp_member != NULL)) {
886 		zval_ptr_dtor(&tmp_member);
887 	}
888 }
889 /* }}} */
890 
zend_std_unset_dimension(zval * object,zval * offset TSRMLS_DC)891 static void zend_std_unset_dimension(zval *object, zval *offset TSRMLS_DC) /* {{{ */
892 {
893 	zend_class_entry *ce = Z_OBJCE_P(object);
894 
895 	if (instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC)) {
896 		SEPARATE_ARG_IF_REF(offset);
897 		zend_call_method_with_1_params(&object, ce, NULL, "offsetunset", NULL, offset);
898 		zval_ptr_dtor(&offset);
899 	} else {
900 		zend_error_noreturn(E_ERROR, "Cannot use object of type %s as array", ce->name);
901 	}
902 }
903 /* }}} */
904 
zend_std_call_user_call(INTERNAL_FUNCTION_PARAMETERS)905 ZEND_API void zend_std_call_user_call(INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
906 {
907 	zend_internal_function *func = (zend_internal_function *)EG(current_execute_data)->function_state.function;
908 	zval *method_name_ptr, *method_args_ptr;
909 	zval *method_result_ptr = NULL;
910 	zend_class_entry *ce = Z_OBJCE_P(this_ptr);
911 
912 	ALLOC_ZVAL(method_args_ptr);
913 	INIT_PZVAL(method_args_ptr);
914 	array_init_size(method_args_ptr, ZEND_NUM_ARGS());
915 
916 	if (UNEXPECTED(zend_copy_parameters_array(ZEND_NUM_ARGS(), method_args_ptr TSRMLS_CC) == FAILURE)) {
917 		zval_dtor(method_args_ptr);
918 		zend_error_noreturn(E_ERROR, "Cannot get arguments for __call");
919 		RETURN_FALSE;
920 	}
921 
922 	ALLOC_ZVAL(method_name_ptr);
923 	INIT_PZVAL(method_name_ptr);
924 	ZVAL_STRING(method_name_ptr, func->function_name, 0); /* no dup - it's a copy */
925 
926 	/* __call handler is called with two arguments:
927 	   method name
928 	   array of method parameters
929 
930 	*/
931 	zend_call_method_with_2_params(&this_ptr, ce, &ce->__call, ZEND_CALL_FUNC_NAME, &method_result_ptr, method_name_ptr, method_args_ptr);
932 
933 	if (method_result_ptr) {
934 		RETVAL_ZVAL_FAST(method_result_ptr);
935 		zval_ptr_dtor(&method_result_ptr);
936 	}
937 
938 	/* now destruct all auxiliaries */
939 	zval_ptr_dtor(&method_args_ptr);
940 	zval_ptr_dtor(&method_name_ptr);
941 
942 	/* destruct the function also, then - we have allocated it in get_method */
943 	efree(func);
944 }
945 /* }}} */
946 
947 /* Ensures that we're allowed to call a private method.
948  * Returns the function address that should be called, or NULL
949  * if no such function exists.
950  */
zend_check_private_int(zend_function * fbc,zend_class_entry * ce,char * function_name_strval,int function_name_strlen,ulong hash_value TSRMLS_DC)951 static inline zend_function *zend_check_private_int(zend_function *fbc, zend_class_entry *ce, char *function_name_strval, int function_name_strlen, ulong hash_value TSRMLS_DC) /* {{{ */
952 {
953 	if (!ce) {
954 		return 0;
955 	}
956 
957 	/* We may call a private function if:
958 	 * 1.  The class of our object is the same as the scope, and the private
959 	 *     function (EX(fbc)) has the same scope.
960 	 * 2.  One of our parent classes are the same as the scope, and it contains
961 	 *     a private function with the same name that has the same scope.
962 	 */
963 	if (fbc->common.scope == ce && EG(scope) == ce) {
964 		/* rule #1 checks out ok, allow the function call */
965 		return fbc;
966 	}
967 
968 
969 	/* Check rule #2 */
970 	ce = ce->parent;
971 	while (ce) {
972 		if (ce == EG(scope)) {
973 			if (zend_hash_quick_find(&ce->function_table, function_name_strval, function_name_strlen+1, hash_value, (void **) &fbc)==SUCCESS
974 				&& fbc->op_array.fn_flags & ZEND_ACC_PRIVATE
975 				&& fbc->common.scope == EG(scope)) {
976 				return fbc;
977 			}
978 			break;
979 		}
980 		ce = ce->parent;
981 	}
982 	return NULL;
983 }
984 /* }}} */
985 
zend_check_private(zend_function * fbc,zend_class_entry * ce,char * function_name_strval,int function_name_strlen TSRMLS_DC)986 ZEND_API int zend_check_private(zend_function *fbc, zend_class_entry *ce, char *function_name_strval, int function_name_strlen TSRMLS_DC) /* {{{ */
987 {
988 	return zend_check_private_int(fbc, ce, function_name_strval, function_name_strlen, zend_hash_func(function_name_strval, function_name_strlen+1) TSRMLS_CC) != NULL;
989 }
990 /* }}} */
991 
992 /* Ensures that we're allowed to call a protected method.
993  */
zend_check_protected(zend_class_entry * ce,zend_class_entry * scope)994 ZEND_API int zend_check_protected(zend_class_entry *ce, zend_class_entry *scope) /* {{{ */
995 {
996 	zend_class_entry *fbc_scope = ce;
997 
998 	/* Is the context that's calling the function, the same as one of
999 	 * the function's parents?
1000 	 */
1001 	while (fbc_scope) {
1002 		if (fbc_scope==scope) {
1003 			return 1;
1004 		}
1005 		fbc_scope = fbc_scope->parent;
1006 	}
1007 
1008 	/* Is the function's scope the same as our current object context,
1009 	 * or any of the parents of our context?
1010 	 */
1011 	while (scope) {
1012 		if (scope==ce) {
1013 			return 1;
1014 		}
1015 		scope = scope->parent;
1016 	}
1017 	return 0;
1018 }
1019 /* }}} */
1020 
zend_get_user_call_function(zend_class_entry * ce,const char * method_name,int method_len)1021 static inline union _zend_function *zend_get_user_call_function(zend_class_entry *ce, const char *method_name, int method_len) /* {{{ */
1022 {
1023 	zend_internal_function *call_user_call = emalloc(sizeof(zend_internal_function));
1024 	call_user_call->type = ZEND_INTERNAL_FUNCTION;
1025 	call_user_call->module = (ce->type == ZEND_INTERNAL_CLASS) ? ce->info.internal.module : NULL;
1026 	call_user_call->handler = zend_std_call_user_call;
1027 	call_user_call->arg_info = NULL;
1028 	call_user_call->num_args = 0;
1029 	call_user_call->scope = ce;
1030 	call_user_call->fn_flags = ZEND_ACC_CALL_VIA_HANDLER;
1031 	call_user_call->function_name = estrndup(method_name, method_len);
1032 
1033 	return (union _zend_function *)call_user_call;
1034 }
1035 /* }}} */
1036 
zend_std_get_method(zval ** object_ptr,char * method_name,int method_len,const zend_literal * key TSRMLS_DC)1037 static union _zend_function *zend_std_get_method(zval **object_ptr, char *method_name, int method_len, const zend_literal *key TSRMLS_DC) /* {{{ */
1038 {
1039 	zend_function *fbc;
1040 	zval *object = *object_ptr;
1041 	zend_object *zobj = Z_OBJ_P(object);
1042 	ulong hash_value;
1043 	char *lc_method_name;
1044 	ALLOCA_FLAG(use_heap)
1045 
1046 	if (EXPECTED(key != NULL)) {
1047 		lc_method_name = Z_STRVAL(key->constant);
1048 		hash_value = key->hash_value;
1049 	} else {
1050 		lc_method_name = do_alloca(method_len+1, use_heap);
1051 		/* Create a zend_copy_str_tolower(dest, src, src_length); */
1052 		zend_str_tolower_copy(lc_method_name, method_name, method_len);
1053 		hash_value = zend_hash_func(lc_method_name, method_len+1);
1054 	}
1055 
1056 	if (UNEXPECTED(zend_hash_quick_find(&zobj->ce->function_table, lc_method_name, method_len+1, hash_value, (void **)&fbc) == FAILURE)) {
1057 		if (UNEXPECTED(!key)) {
1058 			free_alloca(lc_method_name, use_heap);
1059 		}
1060 		if (zobj->ce->__call) {
1061 			return zend_get_user_call_function(zobj->ce, method_name, method_len);
1062 		} else {
1063 			return NULL;
1064 		}
1065 	}
1066 
1067 	/* Check access level */
1068 	if (fbc->op_array.fn_flags & ZEND_ACC_PRIVATE) {
1069 		zend_function *updated_fbc;
1070 
1071 		/* Ensure that if we're calling a private function, we're allowed to do so.
1072 		 * If we're not and __call() handler exists, invoke it, otherwise error out.
1073 		 */
1074 		updated_fbc = zend_check_private_int(fbc, Z_OBJ_HANDLER_P(object, get_class_entry)(object TSRMLS_CC), lc_method_name, method_len, hash_value TSRMLS_CC);
1075 		if (EXPECTED(updated_fbc != NULL)) {
1076 			fbc = updated_fbc;
1077 		} else {
1078 			if (zobj->ce->__call) {
1079 				fbc = zend_get_user_call_function(zobj->ce, method_name, method_len);
1080 			} else {
1081 				zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), method_name, EG(scope) ? EG(scope)->name : "");
1082 			}
1083 		}
1084 	} else {
1085 		/* Ensure that we haven't overridden a private function and end up calling
1086 		 * the overriding public function...
1087 		 */
1088 		if (EG(scope) &&
1089 		    is_derived_class(fbc->common.scope, EG(scope)) &&
1090 		    fbc->op_array.fn_flags & ZEND_ACC_CHANGED) {
1091 			zend_function *priv_fbc;
1092 
1093 			if (zend_hash_quick_find(&EG(scope)->function_table, lc_method_name, method_len+1, hash_value, (void **) &priv_fbc)==SUCCESS
1094 				&& priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE
1095 				&& priv_fbc->common.scope == EG(scope)) {
1096 				fbc = priv_fbc;
1097 			}
1098 		}
1099 		if ((fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
1100 			/* Ensure that if we're calling a protected function, we're allowed to do so.
1101 			 * If we're not and __call() handler exists, invoke it, otherwise error out.
1102 			 */
1103 			if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), EG(scope)))) {
1104 				if (zobj->ce->__call) {
1105 					fbc = zend_get_user_call_function(zobj->ce, method_name, method_len);
1106 				} else {
1107 					zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), method_name, EG(scope) ? EG(scope)->name : "");
1108 				}
1109 			}
1110 		}
1111 	}
1112 
1113 	if (UNEXPECTED(!key)) {
1114 		free_alloca(lc_method_name, use_heap);
1115 	}
1116 	return fbc;
1117 }
1118 /* }}} */
1119 
zend_std_callstatic_user_call(INTERNAL_FUNCTION_PARAMETERS)1120 ZEND_API void zend_std_callstatic_user_call(INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
1121 {
1122 	zend_internal_function *func = (zend_internal_function *)EG(current_execute_data)->function_state.function;
1123 	zval *method_name_ptr, *method_args_ptr;
1124 	zval *method_result_ptr = NULL;
1125 	zend_class_entry *ce = EG(scope);
1126 
1127 	ALLOC_ZVAL(method_args_ptr);
1128 	INIT_PZVAL(method_args_ptr);
1129 	array_init_size(method_args_ptr, ZEND_NUM_ARGS());
1130 
1131 	if (UNEXPECTED(zend_copy_parameters_array(ZEND_NUM_ARGS(), method_args_ptr TSRMLS_CC) == FAILURE)) {
1132 		zval_dtor(method_args_ptr);
1133 		zend_error_noreturn(E_ERROR, "Cannot get arguments for " ZEND_CALLSTATIC_FUNC_NAME);
1134 		RETURN_FALSE;
1135 	}
1136 
1137 	ALLOC_ZVAL(method_name_ptr);
1138 	INIT_PZVAL(method_name_ptr);
1139 	ZVAL_STRING(method_name_ptr, func->function_name, 0); /* no dup - it's a copy */
1140 
1141 	/* __callStatic handler is called with two arguments:
1142 	   method name
1143 	   array of method parameters
1144 	*/
1145 	zend_call_method_with_2_params(NULL, ce, &ce->__callstatic, ZEND_CALLSTATIC_FUNC_NAME, &method_result_ptr, method_name_ptr, method_args_ptr);
1146 
1147 	if (method_result_ptr) {
1148 		RETVAL_ZVAL_FAST(method_result_ptr);
1149 		zval_ptr_dtor(&method_result_ptr);
1150 	}
1151 
1152 	/* now destruct all auxiliaries */
1153 	zval_ptr_dtor(&method_args_ptr);
1154 	zval_ptr_dtor(&method_name_ptr);
1155 
1156 	/* destruct the function also, then - we have allocated it in get_method */
1157 	efree(func);
1158 }
1159 /* }}} */
1160 
zend_get_user_callstatic_function(zend_class_entry * ce,const char * method_name,int method_len)1161 static inline union _zend_function *zend_get_user_callstatic_function(zend_class_entry *ce, const char *method_name, int method_len) /* {{{ */
1162 {
1163 	zend_internal_function *callstatic_user_call = emalloc(sizeof(zend_internal_function));
1164 	callstatic_user_call->type     = ZEND_INTERNAL_FUNCTION;
1165 	callstatic_user_call->module   = (ce->type == ZEND_INTERNAL_CLASS) ? ce->info.internal.module : NULL;
1166 	callstatic_user_call->handler  = zend_std_callstatic_user_call;
1167 	callstatic_user_call->arg_info = NULL;
1168 	callstatic_user_call->num_args = 0;
1169 	callstatic_user_call->scope    = ce;
1170 	callstatic_user_call->fn_flags = ZEND_ACC_STATIC | ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER;
1171 	callstatic_user_call->function_name = estrndup(method_name, method_len);
1172 
1173 	return (zend_function *)callstatic_user_call;
1174 }
1175 /* }}} */
1176 
1177 /* This is not (yet?) in the API, but it belongs in the built-in objects callbacks */
1178 
zend_std_get_static_method(zend_class_entry * ce,const char * function_name_strval,int function_name_strlen,const zend_literal * key TSRMLS_DC)1179 ZEND_API zend_function *zend_std_get_static_method(zend_class_entry *ce, const char *function_name_strval, int function_name_strlen, const zend_literal *key TSRMLS_DC) /* {{{ */
1180 {
1181 	zend_function *fbc = NULL;
1182 	char *lc_class_name, *lc_function_name = NULL;
1183 	ulong hash_value;
1184 	ALLOCA_FLAG(use_heap)
1185 
1186 	if (EXPECTED(key != NULL)) {
1187 #if (ZEND_GCC_VERSION == 4009) && !(defined(ZTS) && defined(NETWARE)) && !(defined(ZTS) && defined(HPUX)) && !defined(DARWIN)
1188 		/* This is a workaround for bug in GCC 4.9.2 */
1189 		use_heap = 0;
1190 #endif
1191 		lc_function_name = Z_STRVAL(key->constant);
1192 		hash_value = key->hash_value;
1193 	} else {
1194 		lc_function_name = do_alloca(function_name_strlen+1, use_heap);
1195 		/* Create a zend_copy_str_tolower(dest, src, src_length); */
1196 		zend_str_tolower_copy(lc_function_name, function_name_strval, function_name_strlen);
1197 		hash_value = zend_hash_func(lc_function_name, function_name_strlen+1);
1198 	}
1199 
1200 	if (function_name_strlen == ce->name_length && ce->constructor) {
1201 		lc_class_name = zend_str_tolower_dup(ce->name, ce->name_length);
1202 		/* Only change the method to the constructor if the constructor isn't called __construct
1203 		 * we check for __ so we can be binary safe for lowering, we should use ZEND_CONSTRUCTOR_FUNC_NAME
1204 		 */
1205 		if (!memcmp(lc_class_name, lc_function_name, function_name_strlen) && memcmp(ce->constructor->common.function_name, "__", sizeof("__") - 1)) {
1206 			fbc = ce->constructor;
1207 		}
1208 		efree(lc_class_name);
1209 	}
1210 	if (EXPECTED(!fbc) &&
1211 	    UNEXPECTED(zend_hash_quick_find(&ce->function_table, lc_function_name, function_name_strlen+1, hash_value, (void **) &fbc)==FAILURE)) {
1212 		if (UNEXPECTED(!key)) {
1213 			free_alloca(lc_function_name, use_heap);
1214 		}
1215 
1216 		if (ce->__call &&
1217 		    EG(This) &&
1218 		    Z_OBJ_HT_P(EG(This))->get_class_entry &&
1219 		    instanceof_function(Z_OBJCE_P(EG(This)), ce TSRMLS_CC)) {
1220 			return zend_get_user_call_function(ce, function_name_strval, function_name_strlen);
1221 		} else if (ce->__callstatic) {
1222 			return zend_get_user_callstatic_function(ce, function_name_strval, function_name_strlen);
1223 		} else {
1224 	   		return NULL;
1225 		}
1226 	}
1227 
1228 #if MBO_0
1229 	/* right now this function is used for non static method lookup too */
1230 	/* Is the function static */
1231 	if (UNEXPECTED(!(fbc->common.fn_flags & ZEND_ACC_STATIC))) {
1232 		zend_error_noreturn(E_ERROR, "Cannot call non static method %s::%s() without object", ZEND_FN_SCOPE_NAME(fbc), fbc->common.function_name);
1233 	}
1234 #endif
1235 	if (fbc->op_array.fn_flags & ZEND_ACC_PUBLIC) {
1236 		/* No further checks necessary, most common case */
1237 	} else if (fbc->op_array.fn_flags & ZEND_ACC_PRIVATE) {
1238 		zend_function *updated_fbc;
1239 
1240 		/* Ensure that if we're calling a private function, we're allowed to do so.
1241 		 */
1242 		updated_fbc = zend_check_private_int(fbc, EG(scope), lc_function_name, function_name_strlen, hash_value TSRMLS_CC);
1243 		if (EXPECTED(updated_fbc != NULL)) {
1244 			fbc = updated_fbc;
1245 		} else {
1246 			if (ce->__callstatic) {
1247 				fbc = zend_get_user_callstatic_function(ce, function_name_strval, function_name_strlen);
1248 			} else {
1249 				zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), function_name_strval, EG(scope) ? EG(scope)->name : "");
1250 			}
1251 		}
1252 	} else if ((fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
1253 		/* Ensure that if we're calling a protected function, we're allowed to do so.
1254 		 */
1255 		if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), EG(scope)))) {
1256 			if (ce->__callstatic) {
1257 				fbc = zend_get_user_callstatic_function(ce, function_name_strval, function_name_strlen);
1258 			} else {
1259 				zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), function_name_strval, EG(scope) ? EG(scope)->name : "");
1260 			}
1261 		}
1262 	}
1263 
1264 	if (UNEXPECTED(!key)) {
1265 		free_alloca(lc_function_name, use_heap);
1266 	}
1267 
1268 	return fbc;
1269 }
1270 /* }}} */
1271 
zend_std_get_static_property(zend_class_entry * ce,const char * property_name,int property_name_len,zend_bool silent,const zend_literal * key TSRMLS_DC)1272 ZEND_API zval **zend_std_get_static_property(zend_class_entry *ce, const char *property_name, int property_name_len, zend_bool silent, const zend_literal *key TSRMLS_DC) /* {{{ */
1273 {
1274 	zend_property_info *property_info;
1275 	ulong hash_value;
1276 
1277 	if (UNEXPECTED(!key) ||
1278 	    (property_info = CACHED_POLYMORPHIC_PTR(key->cache_slot, ce)) == NULL) {
1279 		if (EXPECTED(key != NULL)) {
1280 			hash_value = key->hash_value;
1281 		} else {
1282 			hash_value = zend_hash_func(property_name, property_name_len+1);
1283 		}
1284 
1285 		if (UNEXPECTED(zend_hash_quick_find(&ce->properties_info, property_name, property_name_len+1, hash_value, (void **) &property_info)==FAILURE)) {
1286 			if (!silent) {
1287 				zend_error_noreturn(E_ERROR, "Access to undeclared static property: %s::$%s", ce->name, property_name);
1288 			}
1289 			return NULL;
1290 		}
1291 
1292 #if DEBUG_OBJECT_HANDLERS
1293 		zend_printf("Access type for %s::%s is %s\n", ce->name, property_name, zend_visibility_string(property_info->flags));
1294 #endif
1295 
1296 		if (UNEXPECTED(!zend_verify_property_access(property_info, ce TSRMLS_CC))) {
1297 			if (!silent) {
1298 				zend_error_noreturn(E_ERROR, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ce->name, property_name);
1299 			}
1300 			return NULL;
1301 		}
1302 
1303 		if (UNEXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0)) {
1304 			if (!silent) {
1305 				zend_error_noreturn(E_ERROR, "Access to undeclared static property: %s::$%s", ce->name, property_name);
1306 			}
1307 			return NULL;
1308 		}
1309 
1310 		zend_update_class_constants(ce TSRMLS_CC);
1311 
1312 		if (EXPECTED(key != NULL)) {
1313 			CACHE_POLYMORPHIC_PTR(key->cache_slot, ce, property_info);
1314 		}
1315 	}
1316 
1317 	if (UNEXPECTED(CE_STATIC_MEMBERS(ce) == NULL) ||
1318 	    UNEXPECTED(CE_STATIC_MEMBERS(ce)[property_info->offset] == NULL)) {
1319 		if (!silent) {
1320 			zend_error_noreturn(E_ERROR, "Access to undeclared static property: %s::$%s", ce->name, property_name);
1321 		}
1322 		return NULL;
1323 	}
1324 
1325 	return &CE_STATIC_MEMBERS(ce)[property_info->offset];
1326 }
1327 /* }}} */
1328 
zend_std_unset_static_property(zend_class_entry * ce,const char * property_name,int property_name_len,const zend_literal * key TSRMLS_DC)1329 ZEND_API zend_bool zend_std_unset_static_property(zend_class_entry *ce, const char *property_name, int property_name_len, const zend_literal *key TSRMLS_DC) /* {{{ */
1330 {
1331 	zend_error_noreturn(E_ERROR, "Attempt to unset static property %s::$%s", ce->name, property_name);
1332 	return 0;
1333 }
1334 /* }}} */
1335 
zend_std_get_constructor(zval * object TSRMLS_DC)1336 ZEND_API union _zend_function *zend_std_get_constructor(zval *object TSRMLS_DC) /* {{{ */
1337 {
1338 	zend_object *zobj = Z_OBJ_P(object);
1339 	zend_function *constructor = zobj->ce->constructor;
1340 
1341 	if (constructor) {
1342 		if (constructor->op_array.fn_flags & ZEND_ACC_PUBLIC) {
1343 			/* No further checks necessary */
1344 		} else if (constructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
1345 			/* Ensure that if we're calling a private function, we're allowed to do so.
1346 			 */
1347 			if (UNEXPECTED(constructor->common.scope != EG(scope))) {
1348 				if (EG(scope)) {
1349 					zend_error_noreturn(E_ERROR, "Call to private %s::%s() from context '%s'", constructor->common.scope->name, constructor->common.function_name, EG(scope)->name);
1350 				} else {
1351 					zend_error_noreturn(E_ERROR, "Call to private %s::%s() from invalid context", constructor->common.scope->name, constructor->common.function_name);
1352 				}
1353 			}
1354 		} else if ((constructor->common.fn_flags & ZEND_ACC_PROTECTED)) {
1355 			/* Ensure that if we're calling a protected function, we're allowed to do so.
1356 			 * Constructors only have prototype if they are defined by an interface but
1357 			 * it is the compilers responsibility to take care of the prototype.
1358 			 */
1359 			if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(constructor), EG(scope)))) {
1360 				if (EG(scope)) {
1361 					zend_error_noreturn(E_ERROR, "Call to protected %s::%s() from context '%s'", constructor->common.scope->name, constructor->common.function_name, EG(scope)->name);
1362 				} else {
1363 					zend_error_noreturn(E_ERROR, "Call to protected %s::%s() from invalid context", constructor->common.scope->name, constructor->common.function_name);
1364 				}
1365 			}
1366 		}
1367 	}
1368 
1369 	return constructor;
1370 }
1371 /* }}} */
1372 
1373 int zend_compare_symbol_tables_i(HashTable *ht1, HashTable *ht2 TSRMLS_DC);
1374 
zend_std_compare_objects(zval * o1,zval * o2 TSRMLS_DC)1375 static int zend_std_compare_objects(zval *o1, zval *o2 TSRMLS_DC) /* {{{ */
1376 {
1377 	zend_object *zobj1, *zobj2;
1378 
1379 	zobj1 = Z_OBJ_P(o1);
1380 	zobj2 = Z_OBJ_P(o2);
1381 
1382 	if (zobj1->ce != zobj2->ce) {
1383 		return 1; /* different classes */
1384 	}
1385 	if (!zobj1->properties && !zobj2->properties) {
1386 		int i;
1387 
1388 		Z_OBJ_PROTECT_RECURSION(o1);
1389 		Z_OBJ_PROTECT_RECURSION(o2);
1390 		for (i = 0; i < zobj1->ce->default_properties_count; i++) {
1391 			if (zobj1->properties_table[i]) {
1392 				if (zobj2->properties_table[i]) {
1393 					zval result;
1394 
1395 					if (compare_function(&result, zobj1->properties_table[i], zobj2->properties_table[i] TSRMLS_CC)==FAILURE) {
1396 						Z_OBJ_UNPROTECT_RECURSION(o1);
1397 						Z_OBJ_UNPROTECT_RECURSION(o2);
1398 						return 1;
1399 					}
1400 					if (Z_LVAL(result) != 0) {
1401 						Z_OBJ_UNPROTECT_RECURSION(o1);
1402 						Z_OBJ_UNPROTECT_RECURSION(o2);
1403 						return Z_LVAL(result);
1404 					}
1405 				} else {
1406 					Z_OBJ_UNPROTECT_RECURSION(o1);
1407 					Z_OBJ_UNPROTECT_RECURSION(o2);
1408 					return 1;
1409 				}
1410 			} else {
1411 				if (zobj2->properties_table[i]) {
1412 					Z_OBJ_UNPROTECT_RECURSION(o1);
1413 					Z_OBJ_UNPROTECT_RECURSION(o2);
1414 					return 1;
1415 				}
1416 			}
1417 		}
1418 		Z_OBJ_UNPROTECT_RECURSION(o1);
1419 		Z_OBJ_UNPROTECT_RECURSION(o2);
1420 		return 0;
1421 	} else {
1422 		if (!zobj1->properties) {
1423 			rebuild_object_properties(zobj1);
1424 		}
1425 		if (!zobj2->properties) {
1426 			rebuild_object_properties(zobj2);
1427 		}
1428 		return zend_compare_symbol_tables_i(zobj1->properties, zobj2->properties TSRMLS_CC);
1429 	}
1430 }
1431 /* }}} */
1432 
zend_std_has_property(zval * object,zval * member,int has_set_exists,const zend_literal * key TSRMLS_DC)1433 static int zend_std_has_property(zval *object, zval *member, int has_set_exists, const zend_literal *key TSRMLS_DC) /* {{{ */
1434 {
1435 	zend_object *zobj;
1436 	int result;
1437 	zval **value = NULL;
1438 	zval *tmp_member = NULL;
1439 	zend_property_info *property_info;
1440 
1441 	zobj = Z_OBJ_P(object);
1442 
1443 	if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
1444 		ALLOC_ZVAL(tmp_member);
1445 		*tmp_member = *member;
1446 		INIT_PZVAL(tmp_member);
1447 		zval_copy_ctor(tmp_member);
1448 		convert_to_string(tmp_member);
1449 		member = tmp_member;
1450 		key = NULL;
1451 	}
1452 
1453 #if DEBUG_OBJECT_HANDLERS
1454 	fprintf(stderr, "Read object #%d property: %s\n", Z_OBJ_HANDLE_P(object), Z_STRVAL_P(member));
1455 #endif
1456 
1457 	property_info = zend_get_property_info_quick(zobj->ce, member, 1, key TSRMLS_CC);
1458 
1459 	if (UNEXPECTED(!property_info) ||
1460 	    ((EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
1461 	     property_info->offset >= 0) ?
1462 	        (zobj->properties ?
1463 	            ((value = (zval**)zobj->properties_table[property_info->offset]) == NULL) :
1464 	            (*(value = &zobj->properties_table[property_info->offset]) == NULL)) :
1465 	        (UNEXPECTED(!zobj->properties) ||
1466 	          UNEXPECTED(zend_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &value) == FAILURE)))) {
1467 		zend_guard *guard;
1468 
1469 		result = 0;
1470 		if ((has_set_exists != 2) &&
1471 		    zobj->ce->__isset &&
1472 		    zend_get_property_guard(zobj, property_info, member, &guard) == SUCCESS &&
1473 		    !guard->in_isset) {
1474 			zval *rv;
1475 
1476 			/* have issetter - try with it! */
1477 			Z_ADDREF_P(object);
1478 			if (PZVAL_IS_REF(object)) {
1479 				SEPARATE_ZVAL(&object);
1480 			}
1481 			guard->in_isset = 1; /* prevent circular getting */
1482 			rv = zend_std_call_issetter(object, member TSRMLS_CC);
1483 			if (rv) {
1484 				result = zend_is_true(rv);
1485 				zval_ptr_dtor(&rv);
1486 				if (has_set_exists && result) {
1487 					if (EXPECTED(!EG(exception)) && zobj->ce->__get && !guard->in_get) {
1488 						guard->in_get = 1;
1489 						rv = zend_std_call_getter(object, member TSRMLS_CC);
1490 						guard->in_get = 0;
1491 						if (rv) {
1492 							Z_ADDREF_P(rv);
1493 							result = i_zend_is_true(rv);
1494 							zval_ptr_dtor(&rv);
1495 						} else {
1496 							result = 0;
1497 						}
1498 					} else {
1499 						result = 0;
1500 					}
1501 				}
1502 			}
1503 			guard->in_isset = 0;
1504 			zval_ptr_dtor(&object);
1505 		}
1506 	} else {
1507 		switch (has_set_exists) {
1508 		case 0:
1509 			result = (Z_TYPE_PP(value) != IS_NULL);
1510 			break;
1511 		default:
1512 			result = zend_is_true(*value);
1513 			break;
1514 		case 2:
1515 			result = 1;
1516 			break;
1517 		}
1518 	}
1519 
1520 	if (UNEXPECTED(tmp_member != NULL)) {
1521 		zval_ptr_dtor(&tmp_member);
1522 	}
1523 	return result;
1524 }
1525 /* }}} */
1526 
zend_std_object_get_class(const zval * object TSRMLS_DC)1527 zend_class_entry *zend_std_object_get_class(const zval *object TSRMLS_DC) /* {{{ */
1528 {
1529 	zend_object *zobj;
1530 	zobj = Z_OBJ_P(object);
1531 
1532 	return zobj->ce;
1533 }
1534 /* }}} */
1535 
zend_std_object_get_class_name(const zval * object,const char ** class_name,zend_uint * class_name_len,int parent TSRMLS_DC)1536 int zend_std_object_get_class_name(const zval *object, const char **class_name, zend_uint *class_name_len, int parent TSRMLS_DC) /* {{{ */
1537 {
1538 	zend_object *zobj;
1539 	zend_class_entry *ce;
1540 	zobj = Z_OBJ_P(object);
1541 
1542 	if (parent) {
1543 		if (!zobj->ce->parent) {
1544 			return FAILURE;
1545 		}
1546 		ce = zobj->ce->parent;
1547 	} else {
1548 		ce = zobj->ce;
1549 	}
1550 
1551 	*class_name_len = ce->name_length;
1552 	*class_name = estrndup(ce->name, ce->name_length);
1553 	return SUCCESS;
1554 }
1555 /* }}} */
1556 
zend_std_cast_object_tostring(zval * readobj,zval * writeobj,int type TSRMLS_DC)1557 ZEND_API int zend_std_cast_object_tostring(zval *readobj, zval *writeobj, int type TSRMLS_DC) /* {{{ */
1558 {
1559 	zval *retval;
1560 	zend_class_entry *ce;
1561 
1562 	switch (type) {
1563 		case IS_STRING:
1564 			ce = Z_OBJCE_P(readobj);
1565 			if (ce->__tostring &&
1566 				(zend_call_method_with_0_params(&readobj, ce, &ce->__tostring, "__tostring", &retval) || EG(exception))) {
1567 				if (UNEXPECTED(EG(exception) != NULL)) {
1568 					if (retval) {
1569 						zval_ptr_dtor(&retval);
1570 					}
1571 					EG(exception) = NULL;
1572 					zend_error_noreturn(E_ERROR, "Method %s::__toString() must not throw an exception", ce->name);
1573 					return FAILURE;
1574 				}
1575 				if (EXPECTED(Z_TYPE_P(retval) == IS_STRING)) {
1576 					INIT_PZVAL(writeobj);
1577 					if (readobj == writeobj) {
1578 						zval_dtor(readobj);
1579 					}
1580 					ZVAL_ZVAL(writeobj, retval, 1, 1);
1581 					if (Z_TYPE_P(writeobj) != type) {
1582 						convert_to_explicit_type(writeobj, type);
1583 					}
1584 					return SUCCESS;
1585 				} else {
1586 					zval_ptr_dtor(&retval);
1587 					INIT_PZVAL(writeobj);
1588 					if (readobj == writeobj) {
1589 						zval_dtor(readobj);
1590 					}
1591 					ZVAL_EMPTY_STRING(writeobj);
1592 					zend_error(E_RECOVERABLE_ERROR, "Method %s::__toString() must return a string value", ce->name);
1593 					return SUCCESS;
1594 				}
1595 			}
1596 			return FAILURE;
1597 		case IS_BOOL:
1598 			INIT_PZVAL(writeobj);
1599 			ZVAL_BOOL(writeobj, 1);
1600 			return SUCCESS;
1601 		case IS_LONG:
1602 			ce = Z_OBJCE_P(readobj);
1603 			zend_error(E_NOTICE, "Object of class %s could not be converted to int", ce->name);
1604 			INIT_PZVAL(writeobj);
1605 			if (readobj == writeobj) {
1606 				zval_dtor(readobj);
1607 			}
1608 			ZVAL_LONG(writeobj, 1);
1609 			return SUCCESS;
1610 		case IS_DOUBLE:
1611 			ce = Z_OBJCE_P(readobj);
1612 			zend_error(E_NOTICE, "Object of class %s could not be converted to double", ce->name);
1613 			INIT_PZVAL(writeobj);
1614 			if (readobj == writeobj) {
1615 				zval_dtor(readobj);
1616 			}
1617 			ZVAL_DOUBLE(writeobj, 1);
1618 			return SUCCESS;
1619 		default:
1620 			INIT_PZVAL(writeobj);
1621 			Z_TYPE_P(writeobj) = IS_NULL;
1622 			break;
1623 	}
1624 	return FAILURE;
1625 }
1626 /* }}} */
1627 
zend_std_get_closure(zval * obj,zend_class_entry ** ce_ptr,zend_function ** fptr_ptr,zval ** zobj_ptr TSRMLS_DC)1628 int zend_std_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zval **zobj_ptr TSRMLS_DC) /* {{{ */
1629 {
1630 	zend_class_entry *ce;
1631 	if (Z_TYPE_P(obj) != IS_OBJECT) {
1632 		return FAILURE;
1633 	}
1634 
1635 	ce = Z_OBJCE_P(obj);
1636 
1637 	if (zend_hash_find(&ce->function_table, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME), (void**)fptr_ptr) == FAILURE) {
1638 		return FAILURE;
1639 	}
1640 
1641 	*ce_ptr = ce;
1642 	if ((*fptr_ptr)->common.fn_flags & ZEND_ACC_STATIC) {
1643 		if (zobj_ptr) {
1644 			*zobj_ptr = NULL;
1645 		}
1646 	} else {
1647 		if (zobj_ptr) {
1648 			*zobj_ptr = obj;
1649 		}
1650 	}
1651 	return SUCCESS;
1652 }
1653 /* }}} */
1654 
1655 ZEND_API zend_object_handlers std_object_handlers = {
1656 	zend_objects_store_add_ref,				/* add_ref */
1657 	zend_objects_store_del_ref,				/* del_ref */
1658 	zend_objects_clone_obj,					/* clone_obj */
1659 
1660 	zend_std_read_property,					/* read_property */
1661 	zend_std_write_property,				/* write_property */
1662 	zend_std_read_dimension,				/* read_dimension */
1663 	zend_std_write_dimension,				/* write_dimension */
1664 	zend_std_get_property_ptr_ptr,			/* get_property_ptr_ptr */
1665 	NULL,									/* get */
1666 	NULL,									/* set */
1667 	zend_std_has_property,					/* has_property */
1668 	zend_std_unset_property,				/* unset_property */
1669 	zend_std_has_dimension,					/* has_dimension */
1670 	zend_std_unset_dimension,				/* unset_dimension */
1671 	zend_std_get_properties,				/* get_properties */
1672 	zend_std_get_method,					/* get_method */
1673 	NULL,									/* call_method */
1674 	zend_std_get_constructor,				/* get_constructor */
1675 	zend_std_object_get_class,				/* get_class_entry */
1676 	zend_std_object_get_class_name,			/* get_class_name */
1677 	zend_std_compare_objects,				/* compare_objects */
1678 	zend_std_cast_object_tostring,			/* cast_object */
1679 	NULL,									/* count_elements */
1680 	zend_std_get_debug_info,				/* get_debug_info */
1681 	zend_std_get_closure,					/* get_closure */
1682 	zend_std_get_gc,						/* get_gc */
1683 	NULL,									/* do_operation */
1684 	NULL,									/* compare */
1685 };
1686 
1687 /*
1688  * Local variables:
1689  * tab-width: 4
1690  * c-basic-offset: 4
1691  * indent-tabs-mode: t
1692  * End:
1693  */
1694