Skip to content

Commit 120b4f7

Browse files
authored
Remove code duplication in zend_std_compare_objects (#8710)
1 parent 418f721 commit 120b4f7

File tree

1 file changed

+26
-38
lines changed

1 file changed

+26
-38
lines changed

Zend/zend_object_handlers.c

+26-38
Original file line numberDiff line numberDiff line change
@@ -1616,51 +1616,39 @@ ZEND_API int zend_std_compare_objects(zval *o1, zval *o2) /* {{{ */
16161616

16171617
if (Z_TYPE_P(o1) != Z_TYPE_P(o2)) {
16181618
/* Object and non-object */
1619+
zval *object;
1620+
zval *value;
16191621
zval casted;
1622+
bool object_lhs;
16201623
if (Z_TYPE_P(o1) == IS_OBJECT) {
1621-
ZEND_ASSERT(Z_TYPE_P(o2) != IS_OBJECT);
1622-
zend_uchar target_type = (Z_TYPE_P(o2) == IS_FALSE || Z_TYPE_P(o2) == IS_TRUE)
1623-
? _IS_BOOL : Z_TYPE_P(o2);
1624-
if (Z_OBJ_HT_P(o1)->cast_object(Z_OBJ_P(o1), &casted, target_type) == FAILURE) {
1625-
// TODO: Less crazy.
1626-
if (target_type == IS_LONG || target_type == IS_DOUBLE) {
1627-
zend_error(E_NOTICE, "Object of class %s could not be converted to %s",
1628-
ZSTR_VAL(Z_OBJCE_P(o1)->name), zend_get_type_by_const(target_type));
1629-
if (target_type == IS_LONG) {
1630-
ZVAL_LONG(&casted, 1);
1631-
} else {
1632-
ZVAL_DOUBLE(&casted, 1.0);
1633-
}
1634-
} else {
1635-
return 1;
1636-
}
1637-
}
1638-
int ret = zend_compare(&casted, o2);
1639-
zval_ptr_dtor(&casted);
1640-
return ret;
1624+
object = o1;
1625+
value = o2;
1626+
object_lhs = true;
16411627
} else {
1642-
ZEND_ASSERT(Z_TYPE_P(o2) == IS_OBJECT);
1643-
zend_uchar target_type = (Z_TYPE_P(o1) == IS_FALSE || Z_TYPE_P(o1) == IS_TRUE)
1644-
? _IS_BOOL : Z_TYPE_P(o1);
1645-
if (Z_OBJ_HT_P(o2)->cast_object(Z_OBJ_P(o2), &casted, target_type) == FAILURE) {
1646-
// TODO: Less crazy.
1647-
if (target_type == IS_LONG || target_type == IS_DOUBLE) {
1648-
zend_error(E_NOTICE, "Object of class %s could not be converted to %s",
1649-
ZSTR_VAL(Z_OBJCE_P(o2)->name), zend_get_type_by_const(target_type));
1650-
if (target_type == IS_LONG) {
1651-
ZVAL_LONG(&casted, 1);
1652-
} else {
1653-
ZVAL_DOUBLE(&casted, 1.0);
1654-
}
1628+
object = o2;
1629+
value = o1;
1630+
object_lhs = false;
1631+
}
1632+
ZEND_ASSERT(Z_TYPE_P(value) != IS_OBJECT);
1633+
zend_uchar target_type = (Z_TYPE_P(value) == IS_FALSE || Z_TYPE_P(value) == IS_TRUE)
1634+
? _IS_BOOL : Z_TYPE_P(value);
1635+
if (Z_OBJ_HT_P(object)->cast_object(Z_OBJ_P(object), &casted, target_type) == FAILURE) {
1636+
// TODO: Less crazy.
1637+
if (target_type == IS_LONG || target_type == IS_DOUBLE) {
1638+
zend_error(E_NOTICE, "Object of class %s could not be converted to %s",
1639+
ZSTR_VAL(Z_OBJCE_P(object)->name), zend_get_type_by_const(target_type));
1640+
if (target_type == IS_LONG) {
1641+
ZVAL_LONG(&casted, 1);
16551642
} else {
1656-
return -1;
1643+
ZVAL_DOUBLE(&casted, 1.0);
16571644
}
1645+
} else {
1646+
return object_lhs ? 1 : -1;
16581647
}
1659-
int ret = zend_compare(o1, &casted);
1660-
zval_ptr_dtor(&casted);
1661-
return ret;
16621648
}
1663-
return ZEND_UNCOMPARABLE;
1649+
int ret = object_lhs ? zend_compare(&casted, value) : zend_compare(value, &casted);
1650+
zval_ptr_dtor(&casted);
1651+
return ret;
16641652
}
16651653

16661654
zobj1 = Z_OBJ_P(o1);

0 commit comments

Comments
 (0)