xref: /PHP-7.3/ext/standard/incomplete_class.c (revision 8d3f8ca1)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author:  Sascha Schumann <sascha@schumann.cx>                        |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include "php.h"
20 #include "basic_functions.h"
21 #include "php_incomplete_class.h"
22 
23 #define INCOMPLETE_CLASS_MSG \
24 		"The script tried to execute a method or "  \
25 		"access a property of an incomplete object. " \
26 		"Please ensure that the class definition \"%s\" of the object " \
27 		"you are trying to operate on was loaded _before_ " \
28 		"unserialize() gets called or provide an autoloader " \
29 		"to load the class definition"
30 
31 static zend_object_handlers php_incomplete_object_handlers;
32 
33 /* {{{ incomplete_class_message
34  */
incomplete_class_message(zval * object,int error_type)35 static void incomplete_class_message(zval *object, int error_type)
36 {
37 	zend_string *class_name;
38 
39 	class_name = php_lookup_class_name(object);
40 
41 	if (class_name) {
42 		php_error_docref(NULL, error_type, INCOMPLETE_CLASS_MSG, ZSTR_VAL(class_name));
43 		zend_string_release_ex(class_name, 0);
44 	} else {
45 		php_error_docref(NULL, error_type, INCOMPLETE_CLASS_MSG, "unknown");
46 	}
47 }
48 /* }}} */
49 
incomplete_class_get_property(zval * object,zval * member,int type,void ** cache_slot,zval * rv)50 static zval *incomplete_class_get_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) /* {{{ */
51 {
52 	incomplete_class_message(object, E_NOTICE);
53 
54 	if (type == BP_VAR_W || type == BP_VAR_RW) {
55 		ZVAL_ERROR(rv);
56 		return rv;
57 	} else {
58 		return &EG(uninitialized_zval);
59 	}
60 }
61 /* }}} */
62 
incomplete_class_write_property(zval * object,zval * member,zval * value,void ** cache_slot)63 static void incomplete_class_write_property(zval *object, zval *member, zval *value, void **cache_slot) /* {{{ */
64 {
65 	incomplete_class_message(object, E_NOTICE);
66 }
67 /* }}} */
68 
incomplete_class_get_property_ptr_ptr(zval * object,zval * member,int type,void ** cache_slot)69 static zval *incomplete_class_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
70 {
71 	incomplete_class_message(object, E_NOTICE);
72 	return &EG(error_zval);
73 }
74 /* }}} */
75 
incomplete_class_unset_property(zval * object,zval * member,void ** cache_slot)76 static void incomplete_class_unset_property(zval *object, zval *member, void **cache_slot) /* {{{ */
77 {
78 	incomplete_class_message(object, E_NOTICE);
79 }
80 /* }}} */
81 
incomplete_class_has_property(zval * object,zval * member,int check_empty,void ** cache_slot)82 static int incomplete_class_has_property(zval *object, zval *member, int check_empty, void **cache_slot) /* {{{ */
83 {
84 	incomplete_class_message(object, E_NOTICE);
85 	return 0;
86 }
87 /* }}} */
88 
incomplete_class_get_method(zend_object ** object,zend_string * method,const zval * key)89 static union _zend_function *incomplete_class_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */
90 {
91 	zval zobject;
92 
93 	ZVAL_OBJ(&zobject, *object);
94 	incomplete_class_message(&zobject, E_ERROR);
95 	return NULL;
96 }
97 /* }}} */
98 
99 /* {{{ php_create_incomplete_class
100  */
php_create_incomplete_object(zend_class_entry * class_type)101 static zend_object *php_create_incomplete_object(zend_class_entry *class_type)
102 {
103 	zend_object *object;
104 
105 	object = zend_objects_new( class_type);
106 	object->handlers = &php_incomplete_object_handlers;
107 
108 	object_properties_init(object, class_type);
109 
110 	return object;
111 }
112 
php_create_incomplete_class(void)113 PHPAPI zend_class_entry *php_create_incomplete_class(void)
114 {
115 	zend_class_entry incomplete_class;
116 
117 	INIT_CLASS_ENTRY(incomplete_class, INCOMPLETE_CLASS, NULL);
118 	incomplete_class.create_object = php_create_incomplete_object;
119 
120 	memcpy(&php_incomplete_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
121 	php_incomplete_object_handlers.read_property = incomplete_class_get_property;
122 	php_incomplete_object_handlers.has_property = incomplete_class_has_property;
123 	php_incomplete_object_handlers.unset_property = incomplete_class_unset_property;
124 	php_incomplete_object_handlers.write_property = incomplete_class_write_property;
125 	php_incomplete_object_handlers.get_property_ptr_ptr = incomplete_class_get_property_ptr_ptr;
126     php_incomplete_object_handlers.get_method = incomplete_class_get_method;
127 
128 	return zend_register_internal_class(&incomplete_class);
129 }
130 /* }}} */
131 
132 /* {{{ php_lookup_class_name
133  */
php_lookup_class_name(zval * object)134 PHPAPI zend_string *php_lookup_class_name(zval *object)
135 {
136 	zval *val;
137 	HashTable *object_properties;
138 
139 	object_properties = Z_OBJPROP_P(object);
140 
141 	if ((val = zend_hash_str_find(object_properties, MAGIC_MEMBER, sizeof(MAGIC_MEMBER)-1)) != NULL && Z_TYPE_P(val) == IS_STRING) {
142 		return zend_string_copy(Z_STR_P(val));
143 	}
144 
145 	return NULL;
146 }
147 /* }}} */
148 
149 /* {{{ php_store_class_name
150  */
php_store_class_name(zval * object,const char * name,size_t len)151 PHPAPI void php_store_class_name(zval *object, const char *name, size_t len)
152 {
153 	zval val;
154 
155 
156 	ZVAL_STRINGL(&val, name, len);
157 	zend_hash_str_update(Z_OBJPROP_P(object), MAGIC_MEMBER, sizeof(MAGIC_MEMBER)-1, &val);
158 }
159 /* }}} */
160 
161 /*
162  * Local variables:
163  * tab-width: 4
164  * c-basic-offset: 4
165  * End:
166  * vim600: sw=4 ts=4 fdm=marker
167  * vim<600: sw=4 ts=4
168  */
169