]> granicus.if.org Git - php/commitdiff
Fixed bug #34199 (if($obj)/if(!$obj) inconsistency)
authorDmitry Stogov <dmitry@php.net>
Mon, 12 Sep 2005 11:48:28 +0000 (11:48 +0000)
committerDmitry Stogov <dmitry@php.net>
Mon, 12 Sep 2005 11:48:28 +0000 (11:48 +0000)
NEWS
Zend/tests/bug34199.phpt [new file with mode: 0755]
Zend/zend_execute.h
Zend/zend_operators.c

diff --git a/NEWS b/NEWS
index 083a497761923f0503cda6fcf132ded96b3e7069..76cf834733e52771c1a7ee6c66e24b0835e0fecb 100644 (file)
--- 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 (executable)
index 0000000..9541eea
--- /dev/null
@@ -0,0 +1,17 @@
+--TEST--
+Bug #34199 (if($obj)/if(!$obj) inconsistency because of cast handler)
+--SKIPIF--
+<?php if (!extension_loaded("simplexml")) print "skip"; ?>
+--FILE--
+<?php
+$xml = "<root></root>";
+$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
index 40e09203aec49037dea946c71c8d13456d15ed98..ad07e597914834ab1fdcdeb8b626408d6f392f59 100644 (file)
@@ -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 {
index 66b141610acafb5a916472bb7e6af408349c3992..fda1e3ccaa9f99056f940d1d1b26f164c009c2e8 100644 (file)
@@ -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;