Skip to content

Fix enum to bool comparison #17031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Zend/tests/enum/comparison.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
50 changes: 50 additions & 0 deletions Zend/tests/gh16954.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--TEST--
GH-16954: Enum to bool comparison is inconsistent
--FILE--
<?php

enum E {
case C;
}

$true = true;
$false = false;

var_dump(E::C == true);
var_dump(E::C == $true);
var_dump(true == E::C);
var_dump($true == E::C);

var_dump(E::C != true);
var_dump(E::C != $true);
var_dump(true != E::C);
var_dump($true != E::C);

var_dump(E::C == false);
var_dump(E::C == $false);
var_dump(false == E::C);
var_dump($false == E::C);

var_dump(E::C != false);
var_dump(E::C != $false);
var_dump(false != E::C);
var_dump($false != E::C);

?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
5 changes: 3 additions & 2 deletions Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -2063,8 +2063,9 @@ ZEND_API int zend_std_compare_objects(zval *o1, zval *o2) /* {{{ */
object_lhs = false;
}
ZEND_ASSERT(Z_TYPE_P(value) != IS_OBJECT);
uint8_t target_type = (Z_TYPE_P(value) == IS_FALSE || Z_TYPE_P(value) == IS_TRUE)
? _IS_BOOL : Z_TYPE_P(value);
uint8_t target_type = Z_TYPE_P(value);
/* Should be handled in zend_compare(). */
ZEND_ASSERT(target_type != IS_FALSE && target_type != IS_TRUE);
if (Z_OBJ_HT_P(object)->cast_object(Z_OBJ_P(object), &casted, target_type) == FAILURE) {
// TODO: Less crazy.
if (target_type == IS_LONG || target_type == IS_DOUBLE) {
Expand Down
30 changes: 23 additions & 7 deletions Zend/zend_operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -2333,13 +2333,29 @@ ZEND_API int ZEND_FASTCALL zend_compare(zval *op1, zval *op2) /* {{{ */
}

if (Z_TYPE_P(op1) == IS_OBJECT
&& Z_TYPE_P(op2) == IS_OBJECT
&& Z_OBJ_P(op1) == Z_OBJ_P(op2)) {
return 0;
} else if (Z_TYPE_P(op1) == IS_OBJECT) {
return Z_OBJ_HANDLER_P(op1, compare)(op1, op2);
} else if (Z_TYPE_P(op2) == IS_OBJECT) {
return Z_OBJ_HANDLER_P(op2, compare)(op1, op2);
|| Z_TYPE_P(op2) == IS_OBJECT) {
zval *object, *other;
if (Z_TYPE_P(op1) == IS_OBJECT) {
object = op1;
other = op2;
} else {
object = op2;
other = op1;
}
if (EXPECTED(Z_TYPE_P(other) == IS_OBJECT)) {
if (Z_OBJ_P(object) == Z_OBJ_P(other)) {
return 0;
}
} else if (Z_TYPE_P(other) == IS_TRUE || Z_TYPE_P(other) == IS_FALSE) {
zval casted;
if (Z_OBJ_HANDLER_P(object, cast_object)(Z_OBJ_P(object), &casted, _IS_BOOL) == FAILURE) {
return object == op1 ? 1 : -1;
}
int ret = object == op1 ? zend_compare(&casted, other) : zend_compare(other, &casted);
ZEND_ASSERT(!Z_REFCOUNTED_P(&casted));
return ret;
}
return Z_OBJ_HANDLER_P(object, compare)(op1, op2);
}

if (!converted) {
Expand Down
Loading