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