Skip to content

Commit df79b9b

Browse files
committed
Update get_class_name semantics
* get_class_name is now only used for displaying the class name in debugging functions like var_dump, print_r, etc. It is no longer used in get_class() etc. * As it is no longer used in get_parent_class() the parent argument is now gone. This also fixes incorrect parent classes being reported in COM. * get_class_name is now always required (previously some places made it optional and some required it) and is also required to return a non-NULL value. * Remove zend_get_object_classname. This also fixes a number of potential leaks due to incorrect usage of this function.
1 parent c061c82 commit df79b9b

12 files changed

+32
-96
lines changed

Zend/zend.c

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -263,19 +263,10 @@ ZEND_API void zend_print_flat_zval_r(zval *expr TSRMLS_DC) /* {{{ */
263263
case IS_OBJECT:
264264
{
265265
HashTable *properties = NULL;
266-
zend_string *class_name = NULL;
266+
zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr) TSRMLS_CC);
267+
zend_printf("%s Object (", class_name->val);
268+
zend_string_release(class_name);
267269

268-
if (Z_OBJ_HANDLER_P(expr, get_class_name)) {
269-
class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr), 0 TSRMLS_CC);
270-
}
271-
if (class_name) {
272-
zend_printf("%s Object (", class_name->val);
273-
} else {
274-
zend_printf("%s Object (", "Unknown Class");
275-
}
276-
if (class_name) {
277-
zend_string_release(class_name);
278-
}
279270
if (Z_OBJ_HANDLER_P(expr, get_properties)) {
280271
properties = Z_OBJPROP_P(expr);
281272
}
@@ -324,21 +315,13 @@ ZEND_API void zend_print_zval_r_ex(zend_write_func_t write_func, zval *expr, int
324315
case IS_OBJECT:
325316
{
326317
HashTable *properties;
327-
zend_string *class_name = NULL;
328318
int is_temp;
329319

330-
if (Z_OBJ_HANDLER_P(expr, get_class_name)) {
331-
class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr), 0 TSRMLS_CC);
332-
}
333-
if (class_name) {
334-
ZEND_PUTS_EX(class_name->val);
335-
} else {
336-
ZEND_PUTS_EX("Unknown Class");
337-
}
320+
zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr) TSRMLS_CC);
321+
ZEND_PUTS_EX(class_name->val);
322+
zend_string_release(class_name);
323+
338324
ZEND_PUTS_EX(" Object\n");
339-
if (class_name) {
340-
zend_string_release(class_name);
341-
}
342325
if ((properties = Z_OBJDEBUG_P(expr, is_temp)) == NULL) {
343326
break;
344327
}

Zend/zend_API.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,6 @@ ZEND_API char *zend_zval_type_name(const zval *arg) /* {{{ */
196196
}
197197
/* }}} */
198198

199-
/* returns 1 if you need to copy result, 0 if it's already a copy */
200-
ZEND_API zend_string *zend_get_object_classname(const zend_object *object TSRMLS_DC) /* {{{ */
201-
{
202-
zend_string *ret;
203-
204-
if (object->handlers->get_class_name != NULL) {
205-
ret = object->handlers->get_class_name(object, 0 TSRMLS_CC);
206-
if (ret) {
207-
return ret;
208-
}
209-
}
210-
return object->ce->name;
211-
}
212-
/* }}} */
213-
214199
static int parse_arg_object_to_string(zval *arg, char **p, size_t *pl, int type TSRMLS_DC) /* {{{ */
215200
{
216201
if (Z_OBJ_HANDLER_P(arg, cast_object)) {
@@ -3814,8 +3799,7 @@ ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, const
38143799
EG(scope) = scope;
38153800

38163801
if (!Z_OBJ_HT_P(object)->write_property) {
3817-
zend_string *class_name = zend_get_object_classname(Z_OBJ_P(object) TSRMLS_CC);
3818-
zend_error(E_CORE_ERROR, "Property %s of class %s cannot be updated", name, class_name->val);
3802+
zend_error(E_CORE_ERROR, "Property %s of class %s cannot be updated", name, Z_OBJCE_P(object)->name->val);
38193803
}
38203804
ZVAL_STRINGL(&property, name, name_length);
38213805
Z_OBJ_HT_P(object)->write_property(object, &property, value, NULL TSRMLS_CC);
@@ -3994,8 +3978,7 @@ ZEND_API zval *zend_read_property(zend_class_entry *scope, zval *object, const c
39943978
EG(scope) = scope;
39953979

39963980
if (!Z_OBJ_HT_P(object)->read_property) {
3997-
zend_string *class_name = zend_get_object_classname(Z_OBJ_P(object) TSRMLS_CC);
3998-
zend_error(E_CORE_ERROR, "Property %s of class %s cannot be read", name, class_name->val);
3981+
zend_error(E_CORE_ERROR, "Property %s of class %s cannot be read", name, Z_OBJCE_P(object)->name->val);
39993982
}
40003983

40013984
ZVAL_STRINGL(&property, name, name_length);

Zend/zend_API.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ ZEND_API zval *zend_read_property(zend_class_entry *scope, zval *object, const c
339339

340340
ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, int name_length, zend_bool silent TSRMLS_DC);
341341

342-
ZEND_API zend_string *zend_get_object_classname(const zend_object *object TSRMLS_DC);
343342
ZEND_API char *zend_get_type_by_const(int type);
344343

345344
#define getThis() (Z_OBJ(EX(This)) ? &EX(This) : NULL)

Zend/zend_builtin_functions.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ ZEND_FUNCTION(get_class)
811811
}
812812
}
813813

814-
RETURN_STR(zend_get_object_classname(Z_OBJ_P(obj) TSRMLS_CC));
814+
RETURN_STR(zend_string_copy(Z_OBJCE_P(obj)->name));
815815
}
816816
/* }}} */
817817

@@ -838,7 +838,6 @@ ZEND_FUNCTION(get_parent_class)
838838
{
839839
zval *arg;
840840
zend_class_entry *ce = NULL;
841-
zend_string *name;
842841

843842
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &arg) == FAILURE) {
844843
return;
@@ -854,12 +853,7 @@ ZEND_FUNCTION(get_parent_class)
854853
}
855854

856855
if (Z_TYPE_P(arg) == IS_OBJECT) {
857-
if (Z_OBJ_HT_P(arg)->get_class_name
858-
&& (name = Z_OBJ_HT_P(arg)->get_class_name(Z_OBJ_P(arg), 1 TSRMLS_CC)) != NULL) {
859-
RETURN_STR(name);
860-
} else {
861-
ce = Z_OBJ_P(arg)->ce;
862-
}
856+
ce = Z_OBJ_P(arg)->ce;
863857
} else if (Z_TYPE_P(arg) == IS_STRING) {
864858
ce = zend_lookup_class(Z_STR_P(arg) TSRMLS_CC);
865859
}
@@ -2194,7 +2188,7 @@ ZEND_FUNCTION(debug_print_backtrace)
21942188
if (func->common.scope) {
21952189
class_name = func->common.scope->name;
21962190
} else {
2197-
class_name = zend_get_object_classname(object TSRMLS_CC);
2191+
class_name = object->ce->name;
21982192
}
21992193

22002194
call_type = "->";
@@ -2301,7 +2295,6 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int
23012295
zend_function *func;
23022296
const char *function_name;
23032297
const char *filename;
2304-
zend_string *class_name;
23052298
const char *include_filename = NULL;
23062299
zval stack_frame;
23072300

@@ -2416,8 +2409,7 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int
24162409
if (func->common.scope) {
24172410
add_assoc_str_ex(&stack_frame, "class", sizeof("class")-1, zend_string_copy(func->common.scope->name));
24182411
} else {
2419-
class_name = zend_get_object_classname(object TSRMLS_CC);
2420-
add_assoc_str_ex(&stack_frame, "class", sizeof("class")-1, zend_string_copy(class_name));
2412+
add_assoc_str_ex(&stack_frame, "class", sizeof("class")-1, zend_string_copy(object->ce->name));
24212413

24222414
}
24232415
if ((options & DEBUG_BACKTRACE_PROVIDE_OBJECT) != 0) {

Zend/zend_exceptions.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,8 @@ ZEND_API void zend_throw_exception_internal(zval *exception TSRMLS_DC) /* {{{ */
8989
{
9090
#ifdef HAVE_DTRACE
9191
if (DTRACE_EXCEPTION_THROWN_ENABLED()) {
92-
zend_string *classname;
93-
9492
if (exception != NULL) {
95-
classname = zend_get_object_classname(Z_OBJ_P(exception) TSRMLS_CC);
96-
DTRACE_EXCEPTION_THROWN(classname->val);
93+
DTRACE_EXCEPTION_THROWN(Z_OBJ_P(exception)->ce->val);
9794
} else {
9895
DTRACE_EXCEPTION_THROWN(NULL);
9996
}
@@ -452,7 +449,7 @@ static void _build_trace_args(zval *arg, smart_str *str TSRMLS_DC) /* {{{ */
452449
break;
453450
case IS_OBJECT:
454451
smart_str_appends(str, "Object(");
455-
smart_str_append(str, zend_get_object_classname(Z_OBJ_P(arg) TSRMLS_CC));
452+
smart_str_append(str, Z_OBJCE_P(arg)->name);
456453
smart_str_appends(str, "), ");
457454
break;
458455
}

Zend/zend_object_handlers.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,20 +1525,9 @@ static int zend_std_has_property(zval *object, zval *member, int has_set_exists,
15251525
}
15261526
/* }}} */
15271527

1528-
zend_string* zend_std_object_get_class_name(const zend_object *zobj, int parent TSRMLS_DC) /* {{{ */
1528+
zend_string *zend_std_object_get_class_name(const zend_object *zobj TSRMLS_DC) /* {{{ */
15291529
{
1530-
zend_class_entry *ce;
1531-
1532-
if (parent) {
1533-
if (!zobj->ce->parent) {
1534-
return NULL;
1535-
}
1536-
ce = zobj->ce->parent;
1537-
} else {
1538-
ce = zobj->ce;
1539-
}
1540-
1541-
return zend_string_copy(ce->name);
1530+
return zend_string_copy(zobj->ce->name);
15421531
}
15431532
/* }}} */
15441533

Zend/zend_object_handlers.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ typedef void (*zend_object_dtor_obj_t)(zend_object *object TSRMLS_DC);
9595
typedef void (*zend_object_free_obj_t)(zend_object *object TSRMLS_DC);
9696
typedef zend_object* (*zend_object_clone_obj_t)(zval *object TSRMLS_DC);
9797

98-
typedef zend_string *(*zend_object_get_class_name_t)(const zend_object *object, int parent TSRMLS_DC);
98+
/* Get class name for display in var_dump and other debugging functions.
99+
* Must be defined and must return a non-NULL value. */
100+
typedef zend_string *(*zend_object_get_class_name_t)(const zend_object *object TSRMLS_DC);
101+
99102
typedef int (*zend_object_compare_t)(zval *object1, zval *object2 TSRMLS_DC);
100103
typedef int (*zend_object_compare_zvals_t)(zval *resul, zval *op1, zval *op2 TSRMLS_DC);
101104

ext/com_dotnet/com_handlers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ static union _zend_function *com_constructor_get(zend_object *object TSRMLS_DC)
423423
}
424424
}
425425

426-
static zend_string* com_class_name_get(const zend_object *object, int parent TSRMLS_DC)
426+
static zend_string* com_class_name_get(const zend_object *object TSRMLS_DC)
427427
{
428428
php_com_dotnet_object *obj = (php_com_dotnet_object *)object;
429429

ext/com_dotnet/com_saproxy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static union _zend_function *saproxy_constructor_get(zend_object *object TSRMLS_
333333
return NULL;
334334
}
335335

336-
static zend_string* saproxy_class_name_get(const zend_object *object, int parent TSRMLS_DC)
336+
static zend_string* saproxy_class_name_get(const zend_object *object TSRMLS_DC)
337337
{
338338
return zend_string_copy(php_com_saproxy_class_entry->name);
339339
}

ext/pdo/pdo_stmt.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,13 +2636,9 @@ static union _zend_function *row_get_ctor(zend_object *object TSRMLS_DC)
26362636
return (union _zend_function*)&ctor;
26372637
}
26382638

2639-
static zend_string *row_get_classname(const zend_object *object, int parent TSRMLS_DC)
2639+
static zend_string *row_get_classname(const zend_object *object TSRMLS_DC)
26402640
{
2641-
if (parent) {
2642-
return NULL;
2643-
} else {
2644-
return zend_string_init("PDORow", sizeof("PDORow") - 1, 0);
2645-
}
2641+
return zend_string_init("PDORow", sizeof("PDORow") - 1, 0);
26462642
}
26472643

26482644
static int row_compare(zval *object1, zval *object2 TSRMLS_DC)

ext/standard/php_incomplete_class.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
} \
3636
incomplete_class = 1; \
3737
} else { \
38-
class_name = zend_get_object_classname(Z_OBJ_P(struc) TSRMLS_CC); \
38+
class_name = zend_string_copy(Z_OBJCE_P(struc)->name); \
3939
}
4040

4141
#define PHP_CLEANUP_CLASS_ATTRIBUTES() \

ext/standard/var.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,10 @@ PHPAPI void php_var_dump(zval *struc, int level TSRMLS_DC) /* {{{ */
163163
return;
164164
}
165165

166-
if (Z_OBJ_HANDLER_P(struc, get_class_name)) {
167-
class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc), 0 TSRMLS_CC);
168-
php_printf("%sobject(%s)#%d (%d) {\n", COMMON, class_name->val, Z_OBJ_HANDLE_P(struc), myht ? zend_obj_num_elements(myht) : 0);
169-
zend_string_release(class_name);
170-
} else {
171-
php_printf("%sobject(unknown class)#%d (%d) {\n", COMMON, Z_OBJ_HANDLE_P(struc), myht ? zend_obj_num_elements(myht) : 0);
172-
}
166+
class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc) TSRMLS_CC);
167+
php_printf("%sobject(%s)#%d (%d) {\n", COMMON, class_name->val, Z_OBJ_HANDLE_P(struc), myht ? zend_obj_num_elements(myht) : 0);
168+
zend_string_release(class_name);
169+
173170
if (myht) {
174171
zend_ulong num;
175172
zend_string *key;
@@ -334,7 +331,7 @@ PHPAPI void php_debug_zval_dump(zval *struc, int level TSRMLS_DC) /* {{{ */
334331
myht->u.v.nApplyCount++;
335332
}
336333
}
337-
class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc), 0 TSRMLS_CC);
334+
class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc) TSRMLS_CC);
338335
php_printf("%sobject(%s)#%d (%d) refcount(%u){\n", COMMON, class_name->val, Z_OBJ_HANDLE_P(struc), myht ? zend_obj_num_elements(myht) : 0, Z_REFCOUNT_P(struc));
339336
zend_string_release(class_name);
340337
if (myht) {
@@ -456,7 +453,6 @@ PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf TSRMLS_DC)
456453
HashTable *myht;
457454
char *tmp_str;
458455
size_t tmp_len;
459-
zend_string *class_name;
460456
zend_string *ztmp, *ztmp2;
461457
zend_ulong index;
462458
zend_string *key;
@@ -533,12 +529,10 @@ PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf TSRMLS_DC)
533529
smart_str_appendc(buf, '\n');
534530
buffer_append_spaces(buf, level - 1);
535531
}
536-
class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc), 0 TSRMLS_CC);
537532

538-
smart_str_append(buf, class_name);
533+
smart_str_append(buf, Z_OBJCE_P(struc)->name);
539534
smart_str_appendl(buf, "::__set_state(array(\n", 21);
540535

541-
zend_string_release(class_name);
542536
if (myht) {
543537
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) {
544538
php_object_element_export(val, index, key, level, buf TSRMLS_CC);

0 commit comments

Comments
 (0)