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