From 0a54602089706b0ea534c3693e68068064f72b5e Mon Sep 17 00:00:00 2001 From: Dmitry Stogov Date: Mon, 12 Sep 2005 11:48:28 +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 083a497761..76cf834733 100644 --- a/NEWS +++ b/NEWS @@ -25,6 +25,8 @@ PHP NEWS (Dmitry) - Fixed bug #34260 (Segfault with callbacks (array_map) + overloading). (Dmitry) +- Fixed bug #34199 (if($obj)/if(!$obj) inconsistency because of cast handler). + (Dmitry, Alex) - Fixed bug #34137 (assigning array element by reference causes binary mess). (Dmitry) - Fixed bug #33957 (gmdate('W')/date('W') sometimes returns wrong week number). 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 40e09203ae..ad07e59791 100644 --- a/Zend/zend_execute.h +++ b/Zend/zend_execute.h @@ -111,6 +111,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 66b141610a..fda1e3ccaa 100644 --- a/Zend/zend_operators.c +++ b/Zend/zend_operators.c @@ -568,7 +568,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