From 9b4ada5c0e014307b7d20abd54f975f34bd0a635 Mon Sep 17 00:00:00 2001 From: Dmitry Stogov Date: Mon, 14 Feb 2011 10:52:16 +0000 Subject: [PATCH] Fixed Bug #53958 (Closures can't 'use' shared variables by value and by reference) --- NEWS | 2 ++ Zend/tests/bug53958.phpt | 61 ++++++++++++++++++++++++++++++++++++++++ Zend/zend_closures.c | 19 +++++++++---- 3 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 Zend/tests/bug53958.phpt diff --git a/NEWS b/NEWS index 72c83eae11..43be363d36 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,8 @@ . Added options to debug backtrace functions. (Stas) . Fixed Bug #53971 (isset() and empty() produce apparently spurious runtime error). (Dmitry) + . Fixed Bug #53958 (Closures can't 'use' shared variables by value and by + reference). (Dmitry) . Fixed Bug #53629 (memory leak inside highlight_string()). (Hannes, Ilia) . Fixed Bug #51458 (Lack of error context with nested exceptions). (Stas) . Fixed Bug #47143 (Throwing an exception in a destructor causes a fatal diff --git a/Zend/tests/bug53958.phpt b/Zend/tests/bug53958.phpt new file mode 100644 index 0000000000..96a41157a4 --- /dev/null +++ b/Zend/tests/bug53958.phpt @@ -0,0 +1,61 @@ +--TEST-- +Bug #53958 (Closures can't 'use' shared variables by value and by reference) +--FILE-- + +--EXPECT-- +1 +1 +1 +1 +5 +6 +7 +8 +5 +1 +6 +1 +1 +5 +1 +6 diff --git a/Zend/zend_closures.c b/Zend/zend_closures.c index 5d34a05c13..d460416e6c 100644 --- a/Zend/zend_closures.c +++ b/Zend/zend_closures.c @@ -323,6 +323,7 @@ static int zval_copy_static_var(zval **p TSRMLS_DC, int num_args, va_list args, { HashTable *target = va_arg(args, HashTable*); zend_bool is_ref; + zval *tmp; if (Z_TYPE_PP(p) & (IS_LEXICAL_VAR|IS_LEXICAL_REF)) { is_ref = Z_TYPE_PP(p) & IS_LEXICAL_REF; @@ -332,25 +333,31 @@ static int zval_copy_static_var(zval **p TSRMLS_DC, int num_args, va_list args, } if (zend_hash_quick_find(EG(active_symbol_table), key->arKey, key->nKeyLength, key->h, (void **) &p) == FAILURE) { if (is_ref) { - zval *tmp; - ALLOC_INIT_ZVAL(tmp); Z_SET_ISREF_P(tmp); zend_hash_quick_add(EG(active_symbol_table), key->arKey, key->nKeyLength, key->h, &tmp, sizeof(zval*), (void**)&p); } else { - p = &EG(uninitialized_zval_ptr); + tmp = EG(uninitialized_zval_ptr); zend_error(E_NOTICE,"Undefined variable: %s", key->arKey); } } else { if (is_ref) { SEPARATE_ZVAL_TO_MAKE_IS_REF(p); + tmp = *p; } else if (Z_ISREF_PP(p)) { - SEPARATE_ZVAL(p); + ALLOC_INIT_ZVAL(tmp); + *tmp = **p; + Z_SET_REFCOUNT_P(tmp, 0); + Z_UNSET_ISREF_P(tmp); + } else { + tmp = *p; } } + } else { + tmp = *p; } - if (zend_hash_quick_add(target, key->arKey, key->nKeyLength, key->h, p, sizeof(zval*), NULL) == SUCCESS) { - Z_ADDREF_PP(p); + if (zend_hash_quick_add(target, key->arKey, key->nKeyLength, key->h, &tmp, sizeof(zval*), NULL) == SUCCESS) { + Z_ADDREF_P(tmp); } return ZEND_HASH_APPLY_KEEP; } -- 2.40.0