From 9d1f2f30d3f1b04b8fa65ff2f21853872265b7ac Mon Sep 17 00:00:00 2001 From: Dmitry Stogov Date: Mon, 12 Sep 2005 11:48:57 +0000 Subject: [PATCH] Fixed bug #34199 (if($obj)/if(!$obj) inconsistency) --- NEWS | 2 ++ Zend/tests/bug34199.phpt | 17 +++++++++++++++++ Zend/zend_execute.h | 18 ++++++++++++++++++ Zend/zend_operators.c | 2 +- 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100755 Zend/tests/bug34199.phpt diff --git a/NEWS b/NEWS index 34d5db8b98..9efef9c066 100644 --- a/NEWS +++ b/NEWS @@ -46,6 +46,8 @@ PHP NEWS (Dmitry) - Fixed bug #34257 (lib64 not handled correctly in ming extension). (Marcus) - Fixed bug #34221 (Compiling xmlrpc as shared fails other parts). (Jani) +- Fixed bug #34199 (if($obj)/if(!$obj) inconsistency because of cast handler). + (Dmitry, Alex) - Fixed bug #34191 (ob_gzhandler does not enforce trailing \0). (Ilia) - Fixed bug #34156 (memory usage remains elevated after memory limit is reached). (Ilia) diff --git a/Zend/tests/bug34199.phpt b/Zend/tests/bug34199.phpt new file mode 100755 index 0000000000..9541eea9b4 --- /dev/null +++ b/Zend/tests/bug34199.phpt @@ -0,0 +1,17 @@ +--TEST-- +Bug #34199 (if($obj)/if(!$obj) inconsistency because of cast handler) +--SKIPIF-- + +--FILE-- +"; +$xml = simplexml_load_string($xml); +$kids = $xml->children(); +var_dump((bool)$kids); +if($kids) echo "bug\n"; else echo "ok\n"; +if(!$kids) echo "ok\n"; else echo "bug\n"; +?> +--EXPECT-- +bool(false) +ok +ok diff --git a/Zend/zend_execute.h b/Zend/zend_execute.h index 98a6b04949..9aa0ea4398 100644 --- a/Zend/zend_execute.h +++ b/Zend/zend_execute.h @@ -99,6 +99,24 @@ static inline int i_zend_is_true(zval *op) case IS_OBJECT: if(IS_ZEND_STD_OBJECT(*op)) { TSRMLS_FETCH(); + + if (Z_OBJ_HT_P(op)->cast_object) { + zval tmp; + if (Z_OBJ_HT_P(op)->cast_object(op, &tmp, IS_BOOL, 1 TSRMLS_CC) == SUCCESS) { + result = Z_LVAL(tmp); + break; + } + } else if (Z_OBJ_HT_P(op)->get) { + zval *tmp = Z_OBJ_HT_P(op)->get(op TSRMLS_CC); + if(Z_TYPE_P(tmp) != IS_OBJECT) { + /* for safety - avoid loop */ + convert_to_boolean(tmp); + result = Z_LVAL_P(tmp); + zval_ptr_dtor(&tmp); + break; + } + } + if(EG(ze1_compatibility_mode)) { result = (zend_hash_num_elements(Z_OBJPROP_P(op))?1:0); } else { diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c index 509f9e2315..1243701955 100644 --- a/Zend/zend_operators.c +++ b/Zend/zend_operators.c @@ -482,7 +482,7 @@ ZEND_API void convert_to_boolean(zval *op) zend_bool retval = 1; TSRMLS_FETCH(); - convert_object_to_type(op, IS_BOOL, convert_to_double); + convert_object_to_type(op, IS_BOOL, convert_to_boolean); if (op->type == IS_BOOL) { return; -- 2.40.0