From d266ba4f2de013234eb70737f77d42941c0fec1e Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 17 Sep 2019 13:13:44 +0200 Subject: [PATCH] Check for exception after calling count_values() To avoid a duplicate error if count_values() throws. --- Zend/zend_vm_def.h | 4 ++++ Zend/zend_vm_execute.h | 12 ++++++++++++ ext/ffi/tests/008.phpt | 2 +- ext/standard/array.c | 3 +++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index c33b4f1b69e..2d8fe24fbc0 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -8606,6 +8606,10 @@ ZEND_VM_COLD_CONST_HANDLER(190, ZEND_COUNT, CONST|TMPVAR|CV, UNUSED) if (SUCCESS == Z_OBJ_HT_P(op1)->count_elements(op1, &count)) { break; } + if (UNEXPECTED(EG(exception))) { + count = 0; + break; + } } /* if not and the object implements Countable we call its count() method */ diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index a657e020890..5927f795801 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -9634,6 +9634,10 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_COUNT_SPEC_CONST_ if (SUCCESS == Z_OBJ_HT_P(op1)->count_elements(op1, &count)) { break; } + if (UNEXPECTED(EG(exception))) { + count = 0; + break; + } } /* if not and the object implements Countable we call its count() method */ @@ -16771,6 +16775,10 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_COUNT_SPEC_TMPVAR_UNUSED_HANDL if (SUCCESS == Z_OBJ_HT_P(op1)->count_elements(op1, &count)) { break; } + if (UNEXPECTED(EG(exception))) { + count = 0; + break; + } } /* if not and the object implements Countable we call its count() method */ @@ -46640,6 +46648,10 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_COUNT_SPEC_CV_UNUSED_HANDLER(Z if (SUCCESS == Z_OBJ_HT_P(op1)->count_elements(op1, &count)) { break; } + if (UNEXPECTED(EG(exception))) { + count = 0; + break; + } } /* if not and the object implements Countable we call its count() method */ diff --git a/ext/ffi/tests/008.phpt b/ext/ffi/tests/008.phpt index 626b2890ce2..fa3991abeea 100644 --- a/ext/ffi/tests/008.phpt +++ b/ext/ffi/tests/008.phpt @@ -16,7 +16,7 @@ foreach ($a as $key => $val) { $a = FFI::new("struct {int x,y;}"); try { - var_dump(@count($a)); + var_dump(count($a)); } catch (Throwable $e) { echo get_class($e) . ": " . $e->getMessage()."\n"; } diff --git a/ext/standard/array.c b/ext/standard/array.c index 73b3d35a081..d44e5d50e74 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -794,6 +794,9 @@ PHP_FUNCTION(count) if (SUCCESS == Z_OBJ_HT(*array)->count_elements(array, &Z_LVAL_P(return_value))) { return; } + if (EG(exception)) { + return; + } } /* if not and the object implements Countable we call its count() method */ if (instanceof_function(Z_OBJCE_P(array), zend_ce_countable)) {