cvs: ZendEngine2 / zend.c zend_object_handlers.c zend_operators.c /tests bug26166.phpt
| From: | Marcus Boerger | Date: | Sat, 08 Nov 2003 14:06:08 +0000 |
| Subject: | cvs: ZendEngine2 / zend.c zend_object_handlers.c zend_operators.c /tests bug26166.phpt | ||
| Groups: | php.zend-engine.cvs | ||
| Request: | Send a blank email to php-zend-engine-cvs+get-1969@lists.php.net to get a copy of this message | ||
helly Sat Nov 8 09:06:08 2003 EDT
Added files:
/ZendEngine2/tests bug26166.phpt
Modified files:
/ZendEngine2 zend.c zend_object_handlers.c zend_operators.c
Log:
Handle exceptions in casting more gracefully.
This fixes bug #26166
Index: ZendEngine2/zend.c
diff -u ZendEngine2/zend.c:1.253 ZendEngine2/zend.c:1.254
--- ZendEngine2/zend.c:1.253 Wed Oct 22 19:42:54 2003
+++ ZendEngine2/zend.c Sat Nov 8 09:06:07 2003
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: zend.c,v 1.253 2003/10/22 23:42:54 iliaa Exp $ */
+/* $Id: zend.c,v 1.254 2003/11/08 14:06:07 helly Exp $ */
#include "zend.h"
#include "zend_extensions.h"
@@ -228,6 +228,12 @@
if (expr->value.obj.handlers->cast_object) {
TSRMLS_FETCH();
if (expr->value.obj.handlers->cast_object(expr, expr_copy, IS_STRING, 0 TSRMLS_CC) ==
SUCCESS) {
+ break;
+ }
+ if (EG(exception)) {
+ zval_dtor(expr_copy);
+ expr_copy->value.str.len = 0;
+ expr_copy->value.str.val = empty_string;
break;
}
}
Index: ZendEngine2/zend_object_handlers.c
diff -u ZendEngine2/zend_object_handlers.c:1.72 ZendEngine2/zend_object_handlers.c:1.73
--- ZendEngine2/zend_object_handlers.c:1.72 Tue Nov 4 12:34:56 2003
+++ ZendEngine2/zend_object_handlers.c Sat Nov 8 09:06:07 2003
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: zend_object_handlers.c,v 1.72 2003/11/04 17:34:56 moriyoshi Exp $ */
+/* $Id: zend_object_handlers.c,v 1.73 2003/11/08 14:06:07 helly Exp $ */
#include "zend.h"
#include "zend_globals.h"
@@ -869,8 +869,13 @@
case IS_STRING:
ZVAL_STRING(&fname, "__tostring", 0);
if (call_user_function_ex(NULL, &readobj, &fname, &retval, 0, NULL, 0, NULL
TSRMLS_CC) == SUCCESS) {
- if (Z_TYPE_P(retval) != IS_STRING) {
- zend_error(E_ERROR, "Method %s::__toString() must return a string value",
Z_OBJCE_P(readobj)->name);
+ if (retval) {
+ if (Z_TYPE_P(retval) != IS_STRING) {
+ zend_error(E_ERROR, "Method %s::__toString() must return a string value",
Z_OBJCE_P(readobj)->name);
+ }
+ } else {
+ MAKE_STD_ZVAL(retval);
+ ZVAL_STRINGL(retval, empty_string, 0, 0);
}
REPLACE_ZVAL_VALUE(&writeobj, retval, 0);
return SUCCESS;
Index: ZendEngine2/zend_operators.c
diff -u ZendEngine2/zend_operators.c:1.165 ZendEngine2/zend_operators.c:1.166
--- ZendEngine2/zend_operators.c:1.165 Thu Nov 6 15:33:47 2003
+++ ZendEngine2/zend_operators.c Sat Nov 8 09:06:07 2003
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: zend_operators.c,v 1.165 2003/11/06 20:33:47 iliaa Exp $ */
+/* $Id: zend_operators.c,v 1.166 2003/11/08 14:06:07 helly Exp $ */
#include <ctype.h>
@@ -445,7 +445,6 @@
{
long lval;
double dval;
- TSRMLS_FETCH();
switch (op->type) {
case IS_NULL:
@@ -465,6 +464,7 @@
break;
case IS_RESOURCE: {
long tmp = op->value.lval;
+ TSRMLS_FETCH();
zend_list_delete(op->value.lval);
op->value.str.val = (char *) emalloc(sizeof("Resource id #")-1 +
MAX_LENGTH_OF_LONG);
@@ -478,6 +478,7 @@
op->value.str.len = zend_sprintf(op->value.str.val, "%ld", lval); /* SAFE */
break;
case IS_DOUBLE: {
+ TSRMLS_FETCH();
dval = op->value.dval;
op->value.str.val = (char *) emalloc_rel(MAX_LENGTH_OF_DOUBLE + EG(precision) + 1);
op->value.str.len = zend_sprintf(op->value.str.val, "%.*G", (int) EG(precision),
dval); /* SAFE */
@@ -490,11 +491,11 @@
op->value.str.val = estrndup_rel("Array", sizeof("Array")-1);
op->value.str.len = sizeof("Array")-1;
break;
- case IS_OBJECT:
+ case IS_OBJECT: {
+ TSRMLS_FETCH();
if (op->value.obj.handlers->cast_object) {
zval tmp;
- TSRMLS_FETCH();
- if (op->value.obj.handlers->cast_object(op, &tmp, IS_STRING, 1 TSRMLS_CC) == SUCCESS)
{
+ if (op->value.obj.handlers->cast_object(op, &tmp, IS_STRING, 1 TSRMLS_CC) == SUCCESS
&& tmp.type == IS_STRING) {
zval_dtor(op);
*op = tmp;
break;
@@ -507,6 +508,7 @@
op->value.str.val = estrndup_rel("Object", sizeof("Object")-1);
op->value.str.len = sizeof("Object")-1;
break;
+ }
default:
zval_dtor(op);
ZVAL_BOOL(op, 0);
Index: ZendEngine2/tests/bug26166.phpt
+++ ZendEngine2/tests/bug26166.phpt
--TEST--
Bug #26166: __toString() crash when no values returned
--FILE--
<?php
class Foo
{
function __toString()
{
return "Hello World!\n";
}
}
class Bar
{
private $obj;
function __construct()
{
$this->obj = new Foo();
}
function __toString()
{
return $this->obj->__toString();
}
}
$o = new Bar;
echo $o;
echo "===THROW===\n";
class Error
{
function __toString() {
throw new Exception("This is an error!");
}
}
$o = new Error;
try {
echo $o;
}
catch (Exception $e) {
echo "Got the exception\n";
}
echo "===NONE===\n";
class None
{
function __toString() {
}
}
$o = new None;
echo $o;
?>
===DONE===
--EXPECTF--
Hello World!
===THROW===
Got the exception
===NONE===
Fatal error: Method none::__toString() must return a string value in %sbug26166.php on line %d