From b00cd68d7419bea8d8155ae585b85cf4f5fe8897 Mon Sep 17 00:00:00 2001 From: Danack Date: Sun, 1 Mar 2015 02:37:57 +0000 Subject: [PATCH 01/11] Fixed PDORow behaviour and message. --- ext/pdo/pdo_stmt.c | 7 ++++--- ext/pdo/tests/pdo_036.phpt | 32 ++++++++++++++++++++++---------- ext/pdo/tests/pdorow.phpt | 18 ++++++++++++++++-- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c index c6ebc37ec2283..947428789321f 100644 --- a/ext/pdo/pdo_stmt.c +++ b/ext/pdo/pdo_stmt.c @@ -113,9 +113,10 @@ ZEND_END_ARG_INFO() RETURN_FALSE; \ } \ -static PHP_FUNCTION(dbstmt_constructor) /* {{{ */ +static PHP_FUNCTION(dbrow_constructor) /* {{{ */ { - php_error_docref(NULL, E_ERROR, "You should not create a PDOStatement manually"); +// php_error_docref(NULL, E_ERROR, "You should not create a PDOStatement manually"); + zend_throw_exception_ex(php_pdo_get_exception(), 0, "You may not create a PDORow manually"); } /* }}} */ @@ -2640,7 +2641,7 @@ static union _zend_function *row_get_ctor(zend_object *object) ctor.type = ZEND_INTERNAL_FUNCTION; ctor.function_name = zend_string_init("__construct", sizeof("__construct") - 1, 0); ctor.scope = pdo_row_ce; - ctor.handler = ZEND_FN(dbstmt_constructor); + ctor.handler = ZEND_FN(dbrow_constructor); ctor.fn_flags = ZEND_ACC_PUBLIC; return (union _zend_function*)&ctor; diff --git a/ext/pdo/tests/pdo_036.phpt b/ext/pdo/tests/pdo_036.phpt index 55c88762ba3ea..16a1597f3633f 100644 --- a/ext/pdo/tests/pdo_036.phpt +++ b/ext/pdo/tests/pdo_036.phpt @@ -7,17 +7,29 @@ Testing PDORow and PDOStatement instances with Reflection $instance = new reflectionclass('pdostatement'); $x = $instance->newInstance(); -var_dump($x); -$instance = new reflectionclass('pdorow'); -$x = $instance->newInstance(); -var_dump($x); +if ($x instanceof pdostatement) { + echo "Ok".PHP_EOL; +} +else { + echo "Failed to create instance of pdostatment"; +} -?> ---EXPECTF-- -object(PDOStatement)#%d (1) { - [%u|b%"queryString"]=> - NULL +try { + $instance = new reflectionclass('pdorow'); + $x = $instance->newInstance(); + echo "Failed to throw exception: ".var_export($x, true); +} +catch(PDOException $pe) { + if ($pe->getMessage() != "You may not create a PDORow manually") { + echo "PDOException has wrong message."; + } + else { + echo "Ok".PHP_EOL; + } } -Fatal error: PDORow::__construct(): You should not create a PDOStatement manually in %spdo_036.php on line %d +?> +--EXPECTF-- +Ok +Ok diff --git a/ext/pdo/tests/pdorow.phpt b/ext/pdo/tests/pdorow.phpt index bcfd8ff229d01..3b4660423c874 100644 --- a/ext/pdo/tests/pdorow.phpt +++ b/ext/pdo/tests/pdorow.phpt @@ -5,8 +5,22 @@ Trying instantiate a PDORow object manually --FILE-- getMessage() != "You may not create a PDORow manually") { + echo "PDOException has wrong message."; + } + else { + echo "Ok".PHP_EOL; + } +} +catch(\Exception $e) { + echo "Failed to "; +} ?> --EXPECTF-- -Fatal error: PDORow::__construct(): You should not create a PDOStatement manually in %s on line %d +Ok From 043a02605f7bbf03f8a085354bfa617689a919d3 Mon Sep 17 00:00:00 2001 From: Danack Date: Sun, 1 Mar 2015 13:06:58 +0000 Subject: [PATCH 02/11] Fixed ReflectionFunction, ReflectionMethod and ReflectionParameter. --- ext/pdo/tests/pdorow.phpt | 2 +- ext/reflection/php_reflection.c | 55 ++++++++++++------- .../ReflectionFunction_construct.001.phpt | 39 ++++++++++--- .../tests/ReflectionMethod_006.phpt | 31 +++++------ .../ReflectionMethod_constructor_error2.phpt | 39 ++++++++++--- ...nParameter_invalidMethodInConstructor.phpt | 21 ++++++- 6 files changed, 133 insertions(+), 54 deletions(-) diff --git a/ext/pdo/tests/pdorow.phpt b/ext/pdo/tests/pdorow.phpt index 3b4660423c874..fca40a1ee1a1a 100644 --- a/ext/pdo/tests/pdorow.phpt +++ b/ext/pdo/tests/pdorow.phpt @@ -18,7 +18,7 @@ catch(PDOException $pe) { } } catch(\Exception $e) { - echo "Failed to "; + echo "Exception throw was not of type PDOException instead was ".get_class($e).PHP_EOL; } ?> diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 80e04046ed17e..a01a0d67c56a5 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -1576,6 +1576,8 @@ ZEND_METHOD(reflection_function, __construct) zend_function *fptr; char *name_str; size_t name_len; + int rv; + zend_error_handling zeh; object = getThis(); intern = Z_REFLECTION_P(object); @@ -1586,27 +1588,32 @@ ZEND_METHOD(reflection_function, __construct) if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "O", &closure, zend_ce_closure) == SUCCESS) { fptr = (zend_function*)zend_get_closure_method_def(closure); Z_ADDREF_P(closure); - } else if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len) == SUCCESS) { - char *nsname; - - lcname = zend_str_tolower_dup(name_str, name_len); - - /* Ignore leading "\" */ - nsname = lcname; - if (lcname[0] == '\\') { - nsname = &lcname[1]; - name_len--; - } - - if ((fptr = zend_hash_str_find_ptr(EG(function_table), nsname, name_len)) == NULL) { + } else { + zend_replace_error_handling(EH_THROW, reflection_exception_ptr, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len); + zend_restore_error_handling(&zeh TSRMLS_CC); + if (rv == SUCCESS) { + char *nsname; + lcname = zend_str_tolower_dup(name_str, name_len); + + /* Ignore leading "\" */ + nsname = lcname; + if (lcname[0] == '\\') { + nsname = &lcname[1]; + name_len--; + } + + if ((fptr = zend_hash_str_find_ptr(EG(function_table), nsname, name_len)) == NULL) { + efree(lcname); + zend_throw_exception_ex(reflection_exception_ptr, 0, + "Function %s() does not exist", name_str); + return; + } efree(lcname); - zend_throw_exception_ex(reflection_exception_ptr, 0, - "Function %s() does not exist", name_str); + } else { + //Exception has been thrown. return; } - efree(lcname); - } else { - return; } ZVAL_STR_COPY(&name, fptr->common.function_name); @@ -2128,10 +2135,14 @@ ZEND_METHOD(reflection_parameter, __construct) zend_class_entry *ce = NULL; zend_bool is_closure = 0; zend_bool is_invoke = 0; + zend_error_handling zeh; + zend_replace_error_handling(EH_THROW, reflection_exception_ptr, &zeh TSRMLS_CC); if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &reference, ¶meter) == FAILURE) { + zend_restore_error_handling(&zeh TSRMLS_CC); return; } + zend_restore_error_handling(&zeh TSRMLS_CC); object = getThis(); intern = Z_REFLECTION_P(object); @@ -2712,7 +2723,13 @@ ZEND_METHOD(reflection_method, __construct) zval ztmp; if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "zs", &classname, &name_str, &name_len) == FAILURE) { - if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len) == FAILURE) { + zend_error_handling zeh; + int rv; + + zend_replace_error_handling(EH_THROW, reflection_exception_ptr, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len); + zend_restore_error_handling(&zeh TSRMLS_CC); + if (rv == FAILURE) { return; } if ((tmp = strstr(name_str, "::")) == NULL) { diff --git a/ext/reflection/tests/ReflectionFunction_construct.001.phpt b/ext/reflection/tests/ReflectionFunction_construct.001.phpt index c8e0a17a9d4b9..90259ac9976a5 100644 --- a/ext/reflection/tests/ReflectionFunction_construct.001.phpt +++ b/ext/reflection/tests/ReflectionFunction_construct.001.phpt @@ -6,18 +6,41 @@ Steve Seear --FILE-- getMessage().PHP_EOL; +} try { $a = new ReflectionFunction('nonExistentFunction'); } catch (Exception $e) { - echo $e->getMessage(); + echo $e->getMessage().PHP_EOL; +} +try { + $a = new ReflectionFunction(); +} +catch(ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; +} +try { + $a = new ReflectionFunction(1, 2); +} +catch(ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; +} +try { + $a = new ReflectionFunction([]); } -$a = new ReflectionFunction(); -$a = new ReflectionFunction(1, 2); +catch(ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; +} + ?> --EXPECTF-- -Warning: ReflectionFunction::__construct() expects parameter 1 to be string, array given in %s on line %d +Ok - ReflectionFunction::__construct() expects parameter 1 to be string, array given Function nonExistentFunction() does not exist -Warning: ReflectionFunction::__construct() expects exactly 1 parameter, 0 given in %s on line %d - -Warning: ReflectionFunction::__construct() expects exactly 1 parameter, 2 given in %s on line %d +Ok - ReflectionFunction::__construct() expects exactly 1 parameter, 0 given +Ok - ReflectionFunction::__construct() expects exactly 1 parameter, 2 given +Ok - ReflectionFunction::__construct() expects parameter 1 to be string, array given diff --git a/ext/reflection/tests/ReflectionMethod_006.phpt b/ext/reflection/tests/ReflectionMethod_006.phpt index a5164190ca67d..0b8228989c7bb 100644 --- a/ext/reflection/tests/ReflectionMethod_006.phpt +++ b/ext/reflection/tests/ReflectionMethod_006.phpt @@ -6,8 +6,18 @@ Steve Seear --FILE-- getMessage().PHP_EOL; +} +try { + new ReflectionMethod('a', 'b', 'c'); +} +catch(ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; +} class C { public function f() {} @@ -35,21 +45,8 @@ var_dump($rm->getName(1)); ?> --EXPECTF-- -Warning: ReflectionMethod::__construct() expects exactly 1 parameter, 0 given in %s on line %d -object(ReflectionMethod)#%d (2) { - ["name"]=> - string(0) "" - ["class"]=> - string(0) "" -} - -Warning: ReflectionMethod::__construct() expects exactly 1 parameter, 3 given in %s on line %d -object(ReflectionMethod)#%d (2) { - ["name"]=> - string(0) "" - ["class"]=> - string(0) "" -} +Ok - ReflectionMethod::__construct() expects exactly 1 parameter, 0 given +Ok - ReflectionMethod::__construct() expects exactly 1 parameter, 3 given Warning: ReflectionMethod::isFinal() expects exactly 0 parameters, 1 given in %s on line %d NULL diff --git a/ext/reflection/tests/ReflectionMethod_constructor_error2.phpt b/ext/reflection/tests/ReflectionMethod_constructor_error2.phpt index 1c2d3a138fcaf..85f8097825482 100644 --- a/ext/reflection/tests/ReflectionMethod_constructor_error2.phpt +++ b/ext/reflection/tests/ReflectionMethod_constructor_error2.phpt @@ -16,22 +16,47 @@ class TestClass try { echo "Too few arguments:\n"; $methodInfo = new ReflectionMethod(); -} catch (Exception $e) { - print $e->__toString(); +} catch (ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; } try { echo "\nToo many arguments:\n"; $methodInfo = new ReflectionMethod("TestClass", "foo", true); -} catch (Exception $e) { - print $e->__toString(); +} catch (ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; +} + + +try { + //invalid class + $methodInfo = new ReflectionMethod("InvalidClassName", "foo"); +} catch (ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; +} + + +try { + //invalid 1st param + $methodInfo = new ReflectionMethod([], "foo"); +} catch (ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; +} + +try{ + //invalid 2nd param + $methodInfo = new ReflectionMethod("TestClass", []); +} catch (ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; } ?> --EXPECTF-- Too few arguments: - -Warning: ReflectionMethod::__construct() expects exactly 1 parameter, 0 given in %s on line 12 +Ok - ReflectionMethod::__construct() expects exactly 1 parameter, 0 given Too many arguments: +Ok - ReflectionMethod::__construct() expects exactly 1 parameter, 3 given +Ok - Class InvalidClassName does not exist +Ok - The parameter class is expected to be either a string or an object +Ok - ReflectionMethod::__construct() expects exactly 1 parameter, 2 given -Warning: ReflectionMethod::__construct() expects exactly 1 parameter, 3 given in %s on line 18 diff --git a/ext/reflection/tests/ReflectionParameter_invalidMethodInConstructor.phpt b/ext/reflection/tests/ReflectionParameter_invalidMethodInConstructor.phpt index 3118c17be8b37..1775dee514419 100644 --- a/ext/reflection/tests/ReflectionParameter_invalidMethodInConstructor.phpt +++ b/ext/reflection/tests/ReflectionParameter_invalidMethodInConstructor.phpt @@ -6,7 +6,7 @@ ReflectionParameter::__construct(): Invalid method as constructor // Invalid class name try { new ReflectionParameter (array ('A', 'b'), 0); -} catch (ReflectionException $e) { echo $e->getMessage ()."\n"; } +} catch (ReflectionException $e) { echo $e->getMessage()."\n"; } // Invalid class method try { @@ -18,14 +18,31 @@ try { new ReflectionParameter (array (new C, 'b'), 0); } catch (ReflectionException $e) { echo $e->getMessage ()."\n"; } -echo "Done.\n"; class C { } +try { + new ReflectionParameter(array ('A', 'b')); +} +catch(ReflectionException $e) { + printf( "Ok - %s\n", $e->getMessage()); +} + +try { + new ReflectionParameter(0, 0); +} +catch(ReflectionException $e) { + printf( "Ok - %s\n", $e->getMessage()); +} + +echo "Done.\n"; + ?> --EXPECTF-- Class A does not exist Method C::b() does not exist Method C::b() does not exist +Ok - ReflectionParameter::__construct() expects exactly 2 parameters, 1 given +Ok - The parameter class is expected to be either a string, an array(class, method) or a callable object Done. From a519838ece18dd1babce5b6683b04e99e7816184 Mon Sep 17 00:00:00 2001 From: Danack Date: Sun, 1 Mar 2015 13:20:07 +0000 Subject: [PATCH 03/11] Fixed ReflectionExtension and ReflectionProperty. --- ext/reflection/php_reflection.c | 25 ++++++++-- ...ReflectionExtension_constructor_error.phpt | 30 +++++++++-- .../tests/ReflectionProperty_error.phpt | 50 +++++++++---------- 3 files changed, 71 insertions(+), 34 deletions(-) diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index a01a0d67c56a5..6c96acb457839 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -4802,7 +4802,14 @@ ZEND_METHOD(reflection_property, __construct) zend_property_info *property_info = NULL; property_reference *reference; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &classname, &name_str, &name_len) == FAILURE) { + int rv; + zend_error_handling zeh; + + zend_replace_error_handling(EH_THROW, reflection_exception_ptr, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &classname, &name_str, &name_len); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (rv == FAILURE) { return; } @@ -5197,9 +5204,15 @@ ZEND_METHOD(reflection_extension, __construct) zend_module_entry *module; char *name_str; size_t name_len; + int rv; + zend_error_handling zeh; ALLOCA_FLAG(use_heap) - if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len) == FAILURE) { + zend_replace_error_handling(EH_THROW, reflection_exception_ptr, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (rv == FAILURE) { return; } @@ -5566,8 +5579,14 @@ ZEND_METHOD(reflection_zend_extension, __construct) zend_extension *extension; char *name_str; size_t name_len; + int rv; + zend_error_handling zeh; + + zend_replace_error_handling(EH_THROW, reflection_exception_ptr, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len); + zend_restore_error_handling(&zeh TSRMLS_CC); - if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len) == FAILURE) { + if (rv == FAILURE) { return; } diff --git a/ext/reflection/tests/ReflectionExtension_constructor_error.phpt b/ext/reflection/tests/ReflectionExtension_constructor_error.phpt index 9eae206c5034b..f731ab51cc7db 100644 --- a/ext/reflection/tests/ReflectionExtension_constructor_error.phpt +++ b/ext/reflection/tests/ReflectionExtension_constructor_error.phpt @@ -5,12 +5,32 @@ Gerrit "Remi" te Sligte Leon Luijkx --FILE-- getMessage().PHP_EOL; +} + +try { + $obj = new ReflectionExtension('foo', 'bar'); +} +catch(ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; +} + +try { + $obj = new ReflectionExtension([]); +} +catch(ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; +} + + ?> ==DONE== --EXPECTF-- -Warning: ReflectionExtension::__construct() expects exactly %d parameter, %d given in %s.php on line %d -bool(true) +Ok - ReflectionExtension::__construct() expects exactly %d parameter, %d given +Ok - ReflectionExtension::__construct() expects exactly %d parameter, %d given +Ok - ReflectionExtension::__construct() expects parameter 1 to be string, array given ==DONE== diff --git a/ext/reflection/tests/ReflectionProperty_error.phpt b/ext/reflection/tests/ReflectionProperty_error.phpt index 56de6e1f2e8af..d3910296b58bf 100644 --- a/ext/reflection/tests/ReflectionProperty_error.phpt +++ b/ext/reflection/tests/ReflectionProperty_error.phpt @@ -7,9 +7,27 @@ class C { public static $p; } -var_dump(new ReflectionProperty()); -var_dump(new ReflectionProperty('C::p')); -var_dump(new ReflectionProperty('C', 'p', 'x')); +try { + new ReflectionProperty(); +} +catch(ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; +} +try { + new ReflectionProperty('C::p'); +} +catch(ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; +} + +try { + new ReflectionProperty('C', 'p', 'x'); +} +catch(ReflectionException $re) { + echo "Ok - ".$re->getMessage().PHP_EOL; +} + + $rp = new ReflectionProperty('C', 'p'); var_dump($rp->getName(1)); var_dump($rp->isPrivate(1)); @@ -21,29 +39,9 @@ var_dump($rp->isDefault(1)); ?> --EXPECTF-- -Warning: ReflectionProperty::__construct() expects exactly 2 parameters, 0 given in %s on line %d -object(ReflectionProperty)#%d (2) { - ["name"]=> - string(0) "" - ["class"]=> - string(0) "" -} - -Warning: ReflectionProperty::__construct() expects exactly 2 parameters, 1 given in %s on line %d -object(ReflectionProperty)#%d (2) { - ["name"]=> - string(0) "" - ["class"]=> - string(0) "" -} - -Warning: ReflectionProperty::__construct() expects exactly 2 parameters, 3 given in %s on line %d -object(ReflectionProperty)#%d (2) { - ["name"]=> - string(0) "" - ["class"]=> - string(0) "" -} +Ok - ReflectionProperty::__construct() expects exactly 2 parameters, 0 given +Ok - ReflectionProperty::__construct() expects exactly 2 parameters, 1 given +Ok - ReflectionProperty::__construct() expects exactly 2 parameters, 3 given Warning: ReflectionProperty::getName() expects exactly 0 parameters, 1 given in %s on line %d NULL From c57bde7c9e2f4ac3e93f096eaa93d4ab0133915d Mon Sep 17 00:00:00 2001 From: Danack Date: Sun, 1 Mar 2015 13:44:55 +0000 Subject: [PATCH 04/11] Fixed SplFixedArray and tests. --- ext/spl/spl_fixedarray.c | 7 ++++ .../SplFixedArray__construct_param_array.phpt | 9 ++++- ...SplFixedArray__construct_param_string.phpt | 9 ++++- ...edArray_construct_param_SplFixedArray.phpt | 13 +++--- ext/spl/tests/fixedarray_005.phpt | 10 ++++- ext/spl/tests/fixedarray_009.phpt | 10 +++-- ext/spl/tests/fixedarray_015.phpt | 40 ++----------------- 7 files changed, 48 insertions(+), 50 deletions(-) diff --git a/ext/spl/spl_fixedarray.c b/ext/spl/spl_fixedarray.c index cd83a17b56f1c..dcac83d60df5c 100644 --- a/ext/spl/spl_fixedarray.c +++ b/ext/spl/spl_fixedarray.c @@ -561,6 +561,13 @@ SPL_METHOD(SplFixedArray, __construct) spl_fixedarray_object *intern; zend_long size = 0; + int rv; + zend_error_handling zeh; + +// zend_replace_error_handling(EH_THROW, spl_ce_InvalidArgumentException, &zeh TSRMLS_CC); +// rv = zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &size); +// zend_restore_error_handling(&zeh TSRMLS_CC); + if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &size) == FAILURE) { return; } diff --git a/ext/spl/tests/SplFixedArray__construct_param_array.phpt b/ext/spl/tests/SplFixedArray__construct_param_array.phpt index 1d78695d4a9a3..aa5933ebdbef2 100644 --- a/ext/spl/tests/SplFixedArray__construct_param_array.phpt +++ b/ext/spl/tests/SplFixedArray__construct_param_array.phpt @@ -5,8 +5,13 @@ PHPNW Test Fest 2009 - Jordan Hatch --FILE-- getMessage().PHP_EOL; +} ?> --EXPECTF-- -Warning: SplFixedArray::__construct() expects parameter 1 to be integer, array given in %s on line %d \ No newline at end of file +Ok - SplFixedArray::__construct() expects parameter 1 to be integer, array given \ No newline at end of file diff --git a/ext/spl/tests/SplFixedArray__construct_param_string.phpt b/ext/spl/tests/SplFixedArray__construct_param_string.phpt index 99742e3eb7376..411d7402df044 100644 --- a/ext/spl/tests/SplFixedArray__construct_param_string.phpt +++ b/ext/spl/tests/SplFixedArray__construct_param_string.phpt @@ -4,9 +4,14 @@ SplFixedArray::__construct() with string passed as parameter. PHPNW Test Fest 2009 - Jordan Hatch --FILE-- getMessage().PHP_EOL; +} -$array = new SplFixedArray( "string" ); ?> --EXPECTF-- -Warning: SplFixedArray::__construct() expects parameter 1 to be integer, %unicode_string_optional% given in %s on line %d +Ok - SplFixedArray::__construct() expects parameter 1 to be integer, %unicode_string_optional% given diff --git a/ext/spl/tests/SplFixedArray_construct_param_SplFixedArray.phpt b/ext/spl/tests/SplFixedArray_construct_param_SplFixedArray.phpt index e91f110cd469b..10d4c64a0c524 100644 --- a/ext/spl/tests/SplFixedArray_construct_param_SplFixedArray.phpt +++ b/ext/spl/tests/SplFixedArray_construct_param_SplFixedArray.phpt @@ -4,10 +4,13 @@ Create an SplFixedArray using an SplFixedArray object. Philip Norton philipnorton42@gmail.com --FILE-- getMessage().PHP_EOL; +} + ?> --EXPECTF-- -Warning: SplFixedArray::__construct() expects parameter 1 to be integer, object given in %s on line %d -object(SplFixedArray)#1 (0) { -} \ No newline at end of file +Ok - SplFixedArray::__construct() expects parameter 1 to be integer, object given diff --git a/ext/spl/tests/fixedarray_005.phpt b/ext/spl/tests/fixedarray_005.phpt index fdd78f06a2502..72970a9a1f900 100644 --- a/ext/spl/tests/fixedarray_005.phpt +++ b/ext/spl/tests/fixedarray_005.phpt @@ -5,8 +5,14 @@ SPL: FixedArray: Trying to instantiate passing object to constructor parameter $b = new stdClass; -$a = new SplFixedArray($b); +try { + $a = new SplFixedArray($b); +} +catch(InvalidArgumentException $iae) { + echo "Ok - ".$iae->getMessage().PHP_EOL; +} + ?> --EXPECTF-- -Warning: SplFixedArray::__construct() expects parameter 1 to be integer, object given in %s on line %d +Ok - SplFixedArray::__construct() expects parameter 1 to be integer, object given diff --git a/ext/spl/tests/fixedarray_009.phpt b/ext/spl/tests/fixedarray_009.phpt index c386fc638e300..d67c7ccb6983b 100644 --- a/ext/spl/tests/fixedarray_009.phpt +++ b/ext/spl/tests/fixedarray_009.phpt @@ -3,8 +3,12 @@ SPL: FixedArray: Trying to instantiate passing string to construtor parameter --FILE-- getMessage().PHP_EOL; +} ?> --EXPECTF-- -Warning: SplFixedArray::__construct() expects parameter 1 to be integer, string given in %s on line %d +Ok - SplFixedArray::__construct() expects parameter 1 to be integer, string given diff --git a/ext/spl/tests/fixedarray_015.phpt b/ext/spl/tests/fixedarray_015.phpt index 6e31a42532c00..f12d83bb39188 100644 --- a/ext/spl/tests/fixedarray_015.phpt +++ b/ext/spl/tests/fixedarray_015.phpt @@ -3,47 +3,15 @@ SPL: FixedArray: accessing uninitialized array --FILE-- getMessage(), "\n"; -} -try { - $a[1] = 1; -} catch (Exception $e) { - echo $e->getMessage(), "\n"; -} try { - var_dump(count($a[1])); -} catch (Exception $e) { - echo $e->getMessage(), "\n"; + $a = new SplFixedArray(''); } -try { - var_dump($a->getSize()); -} catch (Exception $e) { - echo $e->getMessage(), "\n"; -} -try { - foreach ($a as $v) { - } -} catch (Exception $e) { - echo $e->getMessage(), "\n"; -} -try { - var_dump($a->setSize(10)); -} catch (Exception $e) { - echo $e->getMessage(), "\n"; +catch(InvalidArgumentException $iae) { + echo "Ok - ".$iae->getMessage().PHP_EOL; } echo "Done\n"; ?> --EXPECTF-- -Warning: SplFixedArray::__construct() expects parameter 1 to be integer, string given in %s on line %d -Index invalid or out of range -Index invalid or out of range -Index invalid or out of range -int(0) -bool(true) +Ok - SplFixedArray::__construct() expects parameter 1 to be integer, string given Done From 99dae96dc0041f856ae066b3f572495da609d28e Mon Sep 17 00:00:00 2001 From: Danack Date: Sun, 15 Mar 2015 13:59:48 +0000 Subject: [PATCH 05/11] Converted intl extension to use IntlException in constructors. --- .../breakiterator/breakiterator_methods.cpp | 4 +- .../calendar/gregoriancalendar_methods.cpp | 38 ++++++------ ext/intl/collator/collator_create.c | 12 ++-- ext/intl/dateformat/dateformat_create.cpp | 20 +++--- ext/intl/formatter/formatter_main.c | 14 ++--- ext/intl/intl_common.h | 2 + ext/intl/intl_data.h | 4 +- ext/intl/intl_error.c | 57 ++++++++++++++--- ext/intl/intl_error.h | 6 ++ ext/intl/msgformat/msgformat.c | 16 ++--- ext/intl/msgformat/msgformat_class.c | 4 +- .../resourcebundle/resourcebundle_class.c | 14 ++--- ext/intl/spoofchecker/spoofchecker_create.c | 2 +- ext/intl/tests/bug62017.phpt | 12 ++-- ext/intl/tests/formatter_fail.phpt | 53 +++++++++------- ext/intl/tests/formatter_format.phpt | 9 ++- .../gregoriancalendar___construct_error.phpt | 22 ++++--- ext/intl/tests/msgfmt_fail.phpt | 61 +++++++++++-------- ext/intl/tests/msgfmt_parse.phpt | 14 +++-- ext/intl/tests/resourcebundle_create.phpt | 29 +++++---- .../transliterator/transliterator_methods.c | 8 +-- 21 files changed, 250 insertions(+), 151 deletions(-) diff --git a/ext/intl/breakiterator/breakiterator_methods.cpp b/ext/intl/breakiterator/breakiterator_methods.cpp index baab5a682d68e..02d228653a61c 100644 --- a/ext/intl/breakiterator/breakiterator_methods.cpp +++ b/ext/intl/breakiterator/breakiterator_methods.cpp @@ -162,12 +162,12 @@ U_CFUNC PHP_FUNCTION(breakiter_set_text) BREAKITER_METHOD_FETCH_OBJECT; ut = utext_openUTF8(ut, text->val, text->len, BREAKITER_ERROR_CODE_P(bio)); - INTL_CTOR_CHECK_STATUS(bio, "breakiter_set_text: error opening UText"); + INTL_CTOR_CHECK_STATUS(bio, "breakiter_set_text: error opening UText", 0); bio->biter->setText(ut, BREAKITER_ERROR_CODE(bio)); utext_close(ut); /* ICU shallow clones the UText */ INTL_CTOR_CHECK_STATUS(bio, "breakiter_set_text: error calling " - "BreakIterator::setText()"); + "BreakIterator::setText()", 0); /* When ICU clones the UText, it does not copy the buffer, so we have to * keep the string buffer around by holding a reference to its zval. This diff --git a/ext/intl/calendar/gregoriancalendar_methods.cpp b/ext/intl/calendar/gregoriancalendar_methods.cpp index 2ae573722793d..69bae08e062bc 100644 --- a/ext/intl/calendar/gregoriancalendar_methods.cpp +++ b/ext/intl/calendar/gregoriancalendar_methods.cpp @@ -36,7 +36,7 @@ static inline GregorianCalendar *fetch_greg(Calendar_object *co) { return (GregorianCalendar*)co->ucal; } -static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS) +static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor) { zval *tz_object = NULL; zval args_a[6] = {0}, @@ -51,8 +51,8 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS) // parameter number validation / variant determination if (ZEND_NUM_ARGS() > 6 || zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) { - intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, - "intlgregcal_create_instance: too many arguments", 0); + intl_error_set_ex(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlgregcal_create_instance: too many arguments", 0, is_constructor); Z_OBJ_P(return_value) = NULL; return; } @@ -60,9 +60,9 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS) variant > 0 && Z_TYPE(args[variant - 1]) == IS_NULL; variant--) {} if (variant == 4) { - intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + intl_error_set_ex(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intlgregcal_create_instance: no variant with 4 arguments " - "(excluding trailing NULLs)", 0); + "(excluding trailing NULLs)", 0, is_constructor); Z_OBJ_P(return_value) = NULL; return; } @@ -71,8 +71,8 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS) if (variant <= 2) { if (zend_parse_parameters(MIN(ZEND_NUM_ARGS(), 2), "|z!s!", &tz_object, &locale, &locale_len) == FAILURE) { - intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, - "intlgregcal_create_instance: bad arguments", 0); + intl_error_set_ex(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlgregcal_create_instance: bad arguments", 0, is_constructor); Z_OBJ_P(return_value) = NULL; return; } @@ -80,8 +80,8 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS) if (variant > 2 && zend_parse_parameters(ZEND_NUM_ARGS(), "lll|lll", &largs[0], &largs[1], &largs[2], &largs[3], &largs[4], &largs[5]) == FAILURE) { - intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, - "intlgregcal_create_instance: bad arguments", 0); + intl_error_set_ex(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "intlgregcal_create_instance: bad arguments", 0, is_constructor); Z_OBJ_P(return_value) = NULL; return; } @@ -104,8 +104,8 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS) gcal = new GregorianCalendar(tz, Locale::createFromName(locale), status); if (U_FAILURE(status)) { - intl_error_set(NULL, status, "intlgregcal_create_instance: error " - "creating ICU GregorianCalendar from time zone and locale", 0); + intl_error_set_ex(NULL, status, "intlgregcal_create_instance: error " + "creating ICU GregorianCalendar from time zone and locale", 0, is_constructor); if (gcal) { delete gcal; } @@ -117,9 +117,9 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS) // From date/time (3, 5 or 6 arguments) for (int i = 0; i < variant; i++) { if (largs[i] < INT32_MIN || largs[i] > INT32_MAX) { - intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + intl_error_set_ex(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intlgregcal_create_instance: at least one of the arguments" - " has an absolute value that is too large", 0); + " has an absolute value that is too large", 0, is_constructor); Z_OBJ_P(return_value) = NULL; return; } @@ -137,8 +137,8 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS) status); } if (U_FAILURE(status)) { - intl_error_set(NULL, status, "intlgregcal_create_instance: error " - "creating ICU GregorianCalendar from date", 0); + intl_error_set_ex(NULL, status, "intlgregcal_create_instance: error " + "creating ICU GregorianCalendar from date", 0, is_constructor); if (gcal) { delete gcal; } @@ -154,10 +154,10 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS) strlen(tzinfo->name), US_INV); #endif if (tzstr.isBogus()) { - intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + intl_error_set_ex(NULL, U_ILLEGAL_ARGUMENT_ERROR, "intlgregcal_create_instance: could not create UTF-8 string " "from PHP's default timezone name (see date_default_timezone_get())", - 0); + 0, is_constructor); delete gcal; Z_OBJ_P(return_value) = NULL; return; @@ -179,7 +179,7 @@ U_CFUNC PHP_FUNCTION(intlgregcal_create_instance) object_init_ex(return_value, GregorianCalendar_ce_ptr); ZVAL_COPY_VALUE(&orig, return_value); - _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAM_PASSTHRU); + _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); if (Z_TYPE_P(return_value) == IS_OBJECT && Z_OBJ_P(return_value) == NULL) { zval_dtor(&orig); @@ -194,7 +194,7 @@ U_CFUNC PHP_METHOD(IntlGregorianCalendar, __construct) return_value = getThis(); //changes this to IS_NULL (without first destroying) if there's an error - _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAM_PASSTHRU); + _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); if (Z_TYPE_P(return_value) == IS_OBJECT && Z_OBJ_P(return_value) == NULL) { zend_object_store_ctor_failed(Z_OBJ(orig_this)); diff --git a/ext/intl/collator/collator_create.c b/ext/intl/collator/collator_create.c index b2058422c2282..7631d76ff426e 100644 --- a/ext/intl/collator/collator_create.c +++ b/ext/intl/collator/collator_create.c @@ -25,7 +25,7 @@ #include "intl_data.h" /* {{{ */ -static void collator_ctor(INTERNAL_FUNCTION_PARAMETERS) +static void collator_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor) { const char* locale; size_t locale_len = 0; @@ -38,8 +38,8 @@ static void collator_ctor(INTERNAL_FUNCTION_PARAMETERS) if( zend_parse_parameters( ZEND_NUM_ARGS(), "s", &locale, &locale_len ) == FAILURE ) { - intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, - "collator_create: unable to parse input params", 0 ); + intl_error_set_ex( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "collator_create: unable to parse input params", 0, is_constructor ); zval_dtor(return_value); RETURN_NULL(); } @@ -53,7 +53,7 @@ static void collator_ctor(INTERNAL_FUNCTION_PARAMETERS) /* Open ICU collator. */ co->ucoll = ucol_open( locale, COLLATOR_ERROR_CODE_P( co ) ); - INTL_CTOR_CHECK_STATUS(co, "collator_create: unable to open ICU collator"); + INTL_CTOR_CHECK_STATUS(co, "collator_create: unable to open ICU collator", is_constructor); } /* }}} */ @@ -63,7 +63,7 @@ static void collator_ctor(INTERNAL_FUNCTION_PARAMETERS) PHP_FUNCTION( collator_create ) { object_init_ex( return_value, Collator_ce_ptr ); - collator_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); + collator_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); } /* }}} */ @@ -75,7 +75,7 @@ PHP_METHOD( Collator, __construct ) zval orig_this = *getThis(); return_value = getThis(); - collator_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); + collator_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); if (Z_TYPE_P(return_value) == IS_OBJECT && Z_OBJ_P(return_value) == NULL) { zend_object_store_ctor_failed(Z_OBJ(orig_this)); diff --git a/ext/intl/dateformat/dateformat_create.cpp b/ext/intl/dateformat/dateformat_create.cpp index f8cf77641f6c9..9d6d5f4e97a38 100644 --- a/ext/intl/dateformat/dateformat_create.cpp +++ b/ext/intl/dateformat/dateformat_create.cpp @@ -36,7 +36,7 @@ extern "C" { #include "dateformat_helpers.h" /* {{{ */ -static void datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS) +static void datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor) { zval *object; @@ -64,8 +64,8 @@ static void datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS) if (zend_parse_parameters(ZEND_NUM_ARGS(), "sll|zzs", &locale_str, &locale_len, &date_type, &time_type, &timezone_zv, &calendar_zv, &pattern_str, &pattern_str_len) == FAILURE) { - intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_create: " - "unable to parse input parameters", 0); + intl_error_set_ex( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_create: " + "unable to parse input parameters", 0, is_constructor); Z_OBJ_P(return_value) = NULL; return; } @@ -79,6 +79,8 @@ static void datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS) DATE_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK; if (DATE_FORMAT_OBJECT(dfo) != NULL) { + // This is __construct being called on an instance - it is not + // a constructor. intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR, "datefmt_create: cannot call constructor twice", 0); return; @@ -110,8 +112,8 @@ static void datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS) pattern_str, pattern_str_len, &INTL_DATA_ERROR_CODE(dfo)); if (U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) { /* object construction -> only set global error */ - intl_error_set(NULL, INTL_DATA_ERROR_CODE(dfo), "datefmt_create: " - "error converting pattern to UTF-16", 0); + intl_error_set_ex(NULL, INTL_DATA_ERROR_CODE(dfo), "datefmt_create: " + "error converting pattern to UTF-16", 0, is_constructor); goto error; } } @@ -139,8 +141,8 @@ static void datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS) df->adoptTimeZone(timezone); } } else { - intl_error_set(NULL, INTL_DATA_ERROR_CODE(dfo), "datefmt_create: date " - "formatter creation failed", 0); + intl_error_set_ex(NULL, INTL_DATA_ERROR_CODE(dfo), "datefmt_create: date " + "formatter creation failed", 0, is_constructor); goto error; } @@ -175,7 +177,7 @@ static void datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS) U_CFUNC PHP_FUNCTION( datefmt_create ) { object_init_ex( return_value, IntlDateFormatter_ce_ptr ); - datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); + datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); if (Z_TYPE_P(return_value) == IS_OBJECT && Z_OBJ_P(return_value) == NULL) { RETURN_NULL(); } @@ -192,7 +194,7 @@ U_CFUNC PHP_METHOD( IntlDateFormatter, __construct ) /* return_value param is being changed, therefore we will always return * NULL here */ return_value = getThis(); - datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); + datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); if (Z_TYPE_P(return_value) == IS_OBJECT && Z_OBJ_P(return_value) == NULL) { zend_object_store_ctor_failed(Z_OBJ(orig_this)); diff --git a/ext/intl/formatter/formatter_main.c b/ext/intl/formatter/formatter_main.c index 60db673ff2849..c8ca65da272f2 100644 --- a/ext/intl/formatter/formatter_main.c +++ b/ext/intl/formatter/formatter_main.c @@ -25,7 +25,7 @@ #include "intl_convert.h" /* {{{ */ -static void numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) +static void numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor) { const char* locale; char* pattern = NULL; @@ -39,8 +39,8 @@ static void numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) if( zend_parse_parameters( ZEND_NUM_ARGS(), "sl|s", &locale, &locale_len, &style, &pattern, &pattern_len ) == FAILURE ) { - intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, - "numfmt_create: unable to parse input parameters", 0 ); + intl_error_set_ex( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "numfmt_create: unable to parse input parameters", 0, is_constructor ); Z_OBJ_P(return_value) = NULL; return; } @@ -52,7 +52,7 @@ static void numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) /* Convert pattern (if specified) to UTF-16. */ if(pattern && pattern_len) { intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(nfo)); - INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: error converting pattern to UTF-16"); + INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: error converting pattern to UTF-16", is_constructor); } if(locale_len == 0) { @@ -66,7 +66,7 @@ static void numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) efree(spattern); } - INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: number formatter creation failed"); + INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: number formatter creation failed", is_constructor); } /* }}} */ @@ -78,7 +78,7 @@ static void numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) PHP_FUNCTION( numfmt_create ) { object_init_ex( return_value, NumberFormatter_ce_ptr ); - numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); + numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); if (Z_TYPE_P(return_value) == IS_OBJECT && Z_OBJ_P(return_value) == NULL) { RETURN_NULL(); } @@ -93,7 +93,7 @@ PHP_METHOD( NumberFormatter, __construct ) zval orig_this = *getThis(); return_value = getThis(); - numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); + numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); if (Z_TYPE_P(return_value) == IS_OBJECT && Z_OBJ_P(return_value) == NULL) { zend_object_store_ctor_failed(Z_OBJ(orig_this)); diff --git a/ext/intl/intl_common.h b/ext/intl/intl_common.h index a49794648d9ac..61da2738d195a 100644 --- a/ext/intl/intl_common.h +++ b/ext/intl/intl_common.h @@ -41,4 +41,6 @@ #define INTL_Z_STRVAL_P(str) (UChar*) Z_STRVAL_P(str) #define INTL_Z_STRLEN_P(str) UCHARS( Z_STRLEN_P(str) ) +extern zend_class_entry *IntlException_ce_ptr; + #endif /* INTL_COMMON_H */ diff --git a/ext/intl/intl_data.h b/ext/intl/intl_data.h index 6001aa128513c..c49214ffcd622 100644 --- a/ext/intl/intl_data.h +++ b/ext/intl/intl_data.h @@ -64,11 +64,11 @@ typedef struct _intl_data { } /* Check status, if error - destroy value and exit */ -#define INTL_CTOR_CHECK_STATUS(obj, msg) \ +#define INTL_CTOR_CHECK_STATUS(obj, msg, forceException) \ intl_error_set_code( NULL, INTL_DATA_ERROR_CODE((obj)) ); \ if( U_FAILURE( INTL_DATA_ERROR_CODE((obj)) ) ) \ { \ - intl_errors_set_custom_msg( INTL_DATA_ERROR_P((obj)), msg, 0 ); \ + intl_errors_set_custom_msg_ex( INTL_DATA_ERROR_P((obj)), msg, 0, forceException ); \ /* yes, this is ugly, but it alreay is */ \ if (return_value != getThis()) { \ zval_dtor(return_value); \ diff --git a/ext/intl/intl_error.c b/ext/intl/intl_error.c index 28a2a244e245d..8ea5e7b4cf129 100644 --- a/ext/intl/intl_error.c +++ b/ext/intl/intl_error.c @@ -29,7 +29,7 @@ ZEND_EXTERN_MODULE_GLOBALS( intl ) -static zend_class_entry *IntlException_ce_ptr; +zend_class_entry *IntlException_ce_ptr; /* {{{ intl_error* intl_g_error_get() * Return global error structure. @@ -102,15 +102,26 @@ void intl_error_reset( intl_error* err ) * Set last error message to msg copying it if needed. */ void intl_error_set_custom_msg( intl_error* err, char* msg, int copyMsg ) +{ + intl_error_set_custom_msg_ex( err, msg, copyMsg, 0 ); +} +/* }}} */ + + +/* {{{ void intl_error_set_custom_msg( intl_error* err, char* msg, int copyMsg ) + * Set last error message to msg copying it if needed. + */ +void intl_error_set_custom_msg_ex( intl_error* err, char* msg, int copyMsg, int forceException ) { if( !msg ) return; if( !err ) { - if( INTL_G( error_level ) ) - php_error_docref( NULL, INTL_G( error_level ), "%s", msg ); - if( INTL_G( use_exceptions ) ) + if ( forceException || INTL_G( use_exceptions ) ) { zend_throw_exception_ex( IntlException_ce_ptr, 0, "%s", msg ); + } else if( INTL_G( error_level ) ) { + php_error_docref( NULL, INTL_G( error_level ), "%s", msg ); + } } if( !err && !( err = intl_g_error_get( ) ) ) return; @@ -181,19 +192,39 @@ UErrorCode intl_error_get_code( intl_error* err ) * Set error code and message. */ void intl_error_set( intl_error* err, UErrorCode code, char* msg, int copyMsg ) +{ + intl_error_set_ex( err, code, msg, copyMsg, 0 ); +} +/* }}} */ + + +/* {{{ void intl_error_set( intl_error* err, UErrorCode code, char* msg, int copyMsg ) + * Set error code and message. + */ +void intl_error_set_ex( intl_error* err, UErrorCode code, char* msg, int copyMsg, int forceException ) { intl_error_set_code( err, code ); - intl_error_set_custom_msg( err, msg, copyMsg ); + intl_error_set_custom_msg_ex( err, msg, copyMsg, forceException ); } /* }}} */ + /* {{{ void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg ) * Set error code and message. */ void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg ) +{ + intl_errors_set_ex( err, code, msg, copyMsg, 0 ); +} +/* }}} */ + +/* {{{ void intl_errors_set_ex( intl_error* err, UErrorCode code, char* msg, int copyMsg ) + * Set error code and message. + */ +void intl_errors_set_ex( intl_error* err, UErrorCode code, char* msg, int copyMsg, int forceException ) { intl_errors_set_code( err, code ); - intl_errors_set_custom_msg( err, msg, copyMsg ); + intl_errors_set_custom_msg_ex( err, msg, copyMsg, forceException ); } /* }}} */ @@ -211,14 +242,24 @@ void intl_errors_reset( intl_error* err ) /* {{{ void intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg ) */ void intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg ) +{ + intl_errors_set_custom_msg_ex( err, msg, copyMsg, 0 ); +} +/* }}} */ + + +/* {{{ void intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg ) + */ +void intl_errors_set_custom_msg_ex( intl_error* err, char* msg, int copyMsg, int forceException ) { if(err) { - intl_error_set_custom_msg( err, msg, copyMsg ); + intl_error_set_custom_msg_ex( err, msg, copyMsg, forceException ); } - intl_error_set_custom_msg( NULL, msg, copyMsg ); + intl_error_set_custom_msg_ex( NULL, msg, copyMsg, forceException ); } /* }}} */ + /* {{{ intl_errors_set_code( intl_error* err, UErrorCode err_code ) */ void intl_errors_set_code( intl_error* err, UErrorCode err_code ) diff --git a/ext/intl/intl_error.h b/ext/intl/intl_error.h index 02d62f0299666..4c9349b281493 100644 --- a/ext/intl/intl_error.h +++ b/ext/intl/intl_error.h @@ -36,15 +36,21 @@ void intl_error_init( intl_error* err ); void intl_error_reset( intl_error* err ); void intl_error_set_code( intl_error* err, UErrorCode err_code ); void intl_error_set_custom_msg( intl_error* err, char* msg, int copyMsg ); +void intl_error_set_custom_msg_ex( intl_error* err, char* msg, int copyMsg, int forceException ); void intl_error_set( intl_error* err, UErrorCode code, char* msg, int copyMsg ); +void intl_error_set_ex( intl_error* err, UErrorCode code, char* msg, int copyMsg, int forceException ); + + UErrorCode intl_error_get_code( intl_error* err ); zend_string* intl_error_get_message( intl_error* err ); // Wrappers to synchonize object's and global error structures. void intl_errors_reset( intl_error* err ); void intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg ); +void intl_errors_set_custom_msg_ex( intl_error* err, char* msg, int copyMsg, int forceException ); void intl_errors_set_code( intl_error* err, UErrorCode err_code ); void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg ); +void intl_errors_set_ex( intl_error* err, UErrorCode code, char* msg, int copyMsg, int forceException ); // Other error helpers smart_str intl_parse_error_to_string( UParseError* pe ); diff --git a/ext/intl/msgformat/msgformat.c b/ext/intl/msgformat/msgformat.c index 707b38bd95cd0..34923f92a5cc9 100644 --- a/ext/intl/msgformat/msgformat.c +++ b/ext/intl/msgformat/msgformat.c @@ -26,7 +26,7 @@ #include "intl_convert.h" /* {{{ */ -static void msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) +static void msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor) { const char* locale; char* pattern; @@ -42,8 +42,8 @@ static void msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) if( zend_parse_parameters( ZEND_NUM_ARGS(), "ss", &locale, &locale_len, &pattern, &pattern_len ) == FAILURE ) { - intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, - "msgfmt_create: unable to parse input parameters", 0 ); + intl_error_set_ex( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_create: unable to parse input parameters", 0, is_constructor ); Z_OBJ_P(return_value) = NULL; return; } @@ -54,7 +54,7 @@ static void msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) /* Convert pattern (if specified) to UTF-16. */ if(pattern && pattern_len) { intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo)); - INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to UTF-16"); + INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to UTF-16", is_constructor); } else { spattern_len = 0; spattern = NULL; @@ -66,7 +66,7 @@ static void msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) #ifdef MSG_FORMAT_QUOTE_APOS if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) { - INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to quote-friendly format"); + INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to quote-friendly format", is_constructor); } #endif @@ -84,7 +84,7 @@ static void msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) efree(spattern); } - INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed"); + INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed", is_constructor); } /* }}} */ @@ -96,7 +96,7 @@ static void msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) PHP_FUNCTION( msgfmt_create ) { object_init_ex( return_value, MessageFormatter_ce_ptr ); - msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); + msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); if (Z_TYPE_P(return_value) == IS_OBJECT && Z_OBJ_P(return_value) == NULL) { RETURN_NULL(); } @@ -111,7 +111,7 @@ PHP_METHOD( MessageFormatter, __construct ) zval orig_this = *getThis(); return_value = getThis(); - msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); + msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); if (Z_TYPE_P(return_value) == IS_OBJECT && Z_OBJ_P(return_value) == NULL) { zend_object_store_ctor_failed(Z_OBJ(orig_this)); diff --git a/ext/intl/msgformat/msgformat_class.c b/ext/intl/msgformat/msgformat_class.c index 8d464c6ca48d6..1296d087e1e38 100644 --- a/ext/intl/msgformat/msgformat_class.c +++ b/ext/intl/msgformat/msgformat_class.c @@ -87,10 +87,10 @@ zend_object *MessageFormatter_object_clone(zval *object) if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) { intl_errors_set(INTL_DATA_ERROR_P(mfo), INTL_DATA_ERROR_CODE(mfo), "Failed to clone MessageFormatter object", 0); - zend_throw_exception_ex(NULL, 0, "Failed to clone MessageFormatter object"); + zend_throw_exception_ex(IntlException_ce_ptr, 0, "Failed to clone MessageFormatter object"); } } else { - zend_throw_exception_ex(NULL, 0, "Cannot clone unconstructed MessageFormatter"); + zend_throw_exception_ex(IntlException_ce_ptr, 0, "Cannot clone unconstructed MessageFormatter"); } return new_obj; } diff --git a/ext/intl/resourcebundle/resourcebundle_class.c b/ext/intl/resourcebundle/resourcebundle_class.c index b8d27c940af6d..60800fe1a99ee 100644 --- a/ext/intl/resourcebundle/resourcebundle_class.c +++ b/ext/intl/resourcebundle/resourcebundle_class.c @@ -74,7 +74,7 @@ static zend_object *ResourceBundle_object_create( zend_class_entry *ce ) /* }}} */ /* {{{ ResourceBundle_ctor */ -static void resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS) +static void resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor) { const char *bundlename; size_t bundlename_len = 0; @@ -90,8 +90,8 @@ static void resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS) if( zend_parse_parameters( ZEND_NUM_ARGS(), "s!s!|b", &locale, &locale_len, &bundlename, &bundlename_len, &fallback ) == FAILURE ) { - intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, - "resourcebundle_ctor: unable to parse input parameters", 0 ); + intl_error_set_ex( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "resourcebundle_ctor: unable to parse input parameters", 0, is_constructor ); Z_OBJ_P(return_value) = NULL; return; } @@ -108,7 +108,7 @@ static void resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS) rb->me = ures_openDirect(bundlename, locale, &INTL_DATA_ERROR_CODE(rb)); } - INTL_CTOR_CHECK_STATUS(rb, "resourcebundle_ctor: Cannot load libICU resource bundle"); + INTL_CTOR_CHECK_STATUS(rb, "resourcebundle_ctor: Cannot load libICU resource bundle", is_constructor); if (!fallback && (INTL_DATA_ERROR_CODE(rb) == U_USING_FALLBACK_WARNING || INTL_DATA_ERROR_CODE(rb) == U_USING_DEFAULT_WARNING)) { @@ -119,7 +119,7 @@ static void resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS) bundlename ? bundlename : "(default data)", locale, ures_getLocaleByType( rb->me, ULOC_ACTUAL_LOCALE, &INTL_DATA_ERROR_CODE(rb))); - intl_errors_set_custom_msg(INTL_DATA_ERROR_P(rb), pbuf, 1); + intl_errors_set_custom_msg_ex(INTL_DATA_ERROR_P(rb), pbuf, 1, is_constructor); efree(pbuf); Z_OBJ_P(return_value) = NULL; } @@ -142,7 +142,7 @@ PHP_METHOD( ResourceBundle, __construct ) zval orig_this = *getThis(); return_value = getThis(); - resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); + resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); if (Z_TYPE_P(return_value) == IS_OBJECT && Z_OBJ_P(return_value) == NULL) { zend_object_store_ctor_failed(Z_OBJ(orig_this)); @@ -157,7 +157,7 @@ proto ResourceBundle resourcebundle_create( string $locale [, string $bundlename PHP_FUNCTION( resourcebundle_create ) { object_init_ex( return_value, ResourceBundle_ce_ptr ); - resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); + resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); if (Z_TYPE_P(return_value) == IS_OBJECT && Z_OBJ_P(return_value) == NULL) { RETURN_NULL(); } diff --git a/ext/intl/spoofchecker/spoofchecker_create.c b/ext/intl/spoofchecker/spoofchecker_create.c index d797015cf8de1..b56c840961df4 100644 --- a/ext/intl/spoofchecker/spoofchecker_create.c +++ b/ext/intl/spoofchecker/spoofchecker_create.c @@ -38,7 +38,7 @@ PHP_METHOD(Spoofchecker, __construct) SPOOFCHECKER_METHOD_FETCH_OBJECT_NO_CHECK; co->uspoof = uspoof_open(SPOOFCHECKER_ERROR_CODE_P(co)); - INTL_CTOR_CHECK_STATUS(co, "spoofchecker: unable to open ICU Spoof Checker"); + INTL_CTOR_CHECK_STATUS(co, "spoofchecker: unable to open ICU Spoof Checker", 1); /* Single-script enforcement is on by default. This fails for languages like Japanese that legally use multiple scripts within a single word, diff --git a/ext/intl/tests/bug62017.phpt b/ext/intl/tests/bug62017.phpt index 50aeae48061a4..e5c216c1e2a42 100644 --- a/ext/intl/tests/bug62017.phpt +++ b/ext/intl/tests/bug62017.phpt @@ -10,13 +10,15 @@ ini_set('intl.error_level', E_WARNING); var_dump( datefmt_create('', IntlDateFormatter::NONE, IntlDateFormatter::NONE, "\xFF", IntlDateFormatter::GREGORIAN, 'a')); -var_dump( +try { new IntlDateFormatter('', IntlDateFormatter::NONE, IntlDateFormatter::NONE, "Europe/Lisbon", - IntlDateFormatter::GREGORIAN, "\x80")); + IntlDateFormatter::GREGORIAN, "\x80"); +} +catch(IntlException $ie) { + echo $ie->getMessage().PHP_EOL; +} --EXPECTF-- Warning: datefmt_create(): datefmt_create: Time zone identifier given is not a valid UTF-8 string in %s on line %d NULL - -Warning: IntlDateFormatter::__construct(): datefmt_create: error converting pattern to UTF-16 in %s on line %d -NULL +datefmt_create: error converting pattern to UTF-16 diff --git a/ext/intl/tests/formatter_fail.phpt b/ext/intl/tests/formatter_fail.phpt index 295f011008d6c..c45b113bdba38 100644 --- a/ext/intl/tests/formatter_fail.phpt +++ b/ext/intl/tests/formatter_fail.phpt @@ -12,16 +12,23 @@ function err($fmt) { } function crt($t, $l, $s) { - switch(true) { - case $t == "O": - return new NumberFormatter($l, $s); - break; - case $t == "C": - return NumberFormatter::create($l, $s); - break; - case $t == "P": - return numfmt_create($l, $s); - break; + try { + $fmt = null; + switch(true) { + case $t == "O": + $fmt = new NumberFormatter($l, $s); + break; + case $t == "C": + $fmt = NumberFormatter::create($l, $s); + break; + case $t == "P": + $fmt = numfmt_create($l, $s); + break; + } + err($fmt); + } + catch(IntlException $ie) { + echo "IE: ".$ie->getMessage().PHP_EOL; } } @@ -33,8 +40,13 @@ $args = array( array("en_US", NumberFormatter::PATTERN_RULEBASED), ); -$fmt = new NumberFormatter(); -err($fmt); +try { + $fmt = new NumberFormatter(); +} +catch(IntlException $ie) { + echo $ie->getMessage().PHP_EOL; +} + $fmt = numfmt_create(); err($fmt); $fmt = NumberFormatter::create(); @@ -42,11 +54,8 @@ err($fmt); foreach($args as $arg) { $fmt = crt("O", $arg[0], $arg[1]); - err($fmt); $fmt = crt("C", $arg[0], $arg[1]); - err($fmt); $fmt = crt("P", $arg[0], $arg[1]); - err($fmt); } ?> @@ -70,10 +79,10 @@ Warning: NumberFormatter::create() expects parameter 1 to be string, array given 'numfmt_create: unable to parse input parameters: U_ILLEGAL_ARGUMENT_ERROR' Warning: numfmt_create() expects parameter 1 to be string, array given in %s on line %d -'numfmt_create: unable to parse input parameters: U_ILLEGAL_ARGUMENT_ERROR' -'numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR' -'numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR' -'numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR' -'numfmt_create: number formatter creation failed: U_MEMORY_ALLOCATION_ERROR' -'numfmt_create: number formatter creation failed: U_MEMORY_ALLOCATION_ERROR' -'numfmt_create: number formatter creation failed: U_MEMORY_ALLOCATION_ERROR' +IE: numfmt_create: unable to parse input parameters: U_ILLEGAL_ARGUMENT_ERROR +IE: numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR +IE: numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR +IE: numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR +IE: numfmt_create: number formatter creation failed: U_MEMORY_ALLOCATION_ERROR +IE: numfmt_create: number formatter creation failed: U_MEMORY_ALLOCATION_ERROR +IE: numfmt_create: number formatter creation failed: U_MEMORY_ALLOCATION_ERROR diff --git a/ext/intl/tests/formatter_format.phpt b/ext/intl/tests/formatter_format.phpt index 334ef49567cc2..0fa88681d95ae 100644 --- a/ext/intl/tests/formatter_format.phpt +++ b/ext/intl/tests/formatter_format.phpt @@ -50,7 +50,14 @@ function ut_main() $str_res .= "\nLocale is: $locale\n"; foreach( $styles as $style => $pattern ) { - $fmt = ut_nfmt_create( $locale, $style, $pattern ); + try { + $fmt = ut_nfmt_create( $locale, $style, $pattern ); + } + catch (IntlException $ie) { + //$str_res .= "IE:".$ie->getMessage()."\n"; + $str_res .= "Bad formatter!\n"; + continue; + } if(!$fmt) { $str_res .= "Bad formatter!\n"; diff --git a/ext/intl/tests/gregoriancalendar___construct_error.phpt b/ext/intl/tests/gregoriancalendar___construct_error.phpt index 45229cf000fec..a28fa715e4241 100644 --- a/ext/intl/tests/gregoriancalendar___construct_error.phpt +++ b/ext/intl/tests/gregoriancalendar___construct_error.phpt @@ -11,8 +11,18 @@ ini_set("intl.error_level", E_WARNING); var_dump(intlgregcal_create_instance(1,2,3,4,5,6,7)); var_dump(intlgregcal_create_instance(1,2,3,4,5,6,7,8)); var_dump(intlgregcal_create_instance(1,2,3,4)); -var_dump(new IntlGregorianCalendar(1,2,NULL,4)); -var_dump(new IntlGregorianCalendar(1,2,3,4,NULL,array())); +try { + new IntlGregorianCalendar(1,2,NULL,4); +} +catch (IntlException $ie) { + echo "IE: ".$ie->getMessage().PHP_EOL; +} +try { + new IntlGregorianCalendar(1,2,3,4,NULL,array()); +} +catch (IntlException $ie) { + echo "IE: ".$ie->getMessage().PHP_EOL; +} --EXPECTF-- @@ -26,10 +36,6 @@ NULL Warning: intlgregcal_create_instance(): intlgregcal_create_instance: no variant with 4 arguments (excluding trailing NULLs) in %s on line %d NULL -Warning: IntlGregorianCalendar::__construct(): intlgregcal_create_instance: no variant with 4 arguments (excluding trailing NULLs) in %s on line %d -NULL - +IE: IntlGregorianCalendar::__construct(): intlgregcal_create_instance: no variant with 4 arguments (excluding trailing NULLs) Warning: IntlGregorianCalendar::__construct() expects parameter 6 to be integer, array given in %s on line %d - -Warning: IntlGregorianCalendar::__construct(): intlgregcal_create_instance: bad arguments in %s on line %d -NULL +IE: IntlGregorianCalendar::__construct(): intlgregcal_create_instance: bad arguments diff --git a/ext/intl/tests/msgfmt_fail.phpt b/ext/intl/tests/msgfmt_fail.phpt index bffb71c56d3aa..4b60b35abc2cf 100644 --- a/ext/intl/tests/msgfmt_fail.phpt +++ b/ext/intl/tests/msgfmt_fail.phpt @@ -13,16 +13,24 @@ function err($fmt) { } function crt($t, $l, $s) { - switch(true) { - case $t == "O": - return new MessageFormatter($l, $s); - break; - case $t == "C": - return MessageFormatter::create($l, $s); - break; - case $t == "P": - return msgfmt_create($l, $s); - break; + + try { + $fmt = null; + switch(true) { + case $t == "O": + $fmt = new MessageFormatter($l, $s); + break; + case $t == "C": + $fmt = MessageFormatter::create($l, $s); + break; + case $t == "P": + $fmt = msgfmt_create($l, $s); + break; + } + err($fmt); + } + catch (IntlException $ie) { + echo "IE: ".$ie->getMessage().PHP_EOL; } } @@ -35,32 +43,36 @@ $args = array( array("en_US", "\xD0"), ); -$fmt = new MessageFormatter(); -err($fmt); +try { + $fmt = new MessageFormatter(); +} +catch(IntlException $ie) { + echo "IE: ".$ie->getMessage().PHP_EOL; +} $fmt = msgfmt_create(); err($fmt); $fmt = MessageFormatter::create(); -err($fmt); -$fmt = new MessageFormatter('en'); -err($fmt); +err($fmt); +try { + $fmt = new MessageFormatter('en'); +} +catch(IntlException $ie) { + echo "IE: ".$ie->getMessage().PHP_EOL; +} $fmt = msgfmt_create('en'); err($fmt); $fmt = MessageFormatter::create('en'); err($fmt); foreach($args as $arg) { - $fmt = crt("O", $arg[0], $arg[1]); - err($fmt); - $fmt = crt("C", $arg[0], $arg[1]); - err($fmt); - $fmt = crt("P", $arg[0], $arg[1]); - err($fmt); + crt("O", $arg[0], $arg[1]); + crt("C", $arg[0], $arg[1]); + crt("P", $arg[0], $arg[1]); } ?> --EXPECTF-- -Warning: MessageFormatter::__construct() expects exactly 2 parameters, 0 given in %s on line %d -'msgfmt_create: unable to parse input parameters: U_ILLEGAL_ARGUMENT_ERROR' +IE: msgfmt_create: unable to parse input parameters Warning: msgfmt_create() expects exactly 2 parameters, 0 given in %s on line %d 'msgfmt_create: unable to parse input parameters: U_ILLEGAL_ARGUMENT_ERROR' @@ -68,8 +80,7 @@ Warning: msgfmt_create() expects exactly 2 parameters, 0 given in %s on line %d Warning: MessageFormatter::create() expects exactly 2 parameters, 0 given in %s on line %d 'msgfmt_create: unable to parse input parameters: U_ILLEGAL_ARGUMENT_ERROR' -Warning: MessageFormatter::__construct() expects exactly 2 parameters, 1 given in %s on line %d -'msgfmt_create: unable to parse input parameters: U_ILLEGAL_ARGUMENT_ERROR' +IE: msgfmt_create: unable to parse input parameters Warning: msgfmt_create() expects exactly 2 parameters, 1 given in %s on line %d 'msgfmt_create: unable to parse input parameters: U_ILLEGAL_ARGUMENT_ERROR' diff --git a/ext/intl/tests/msgfmt_parse.phpt b/ext/intl/tests/msgfmt_parse.phpt index b9ec36374bcbd..2eaf630010399 100644 --- a/ext/intl/tests/msgfmt_parse.phpt +++ b/ext/intl/tests/msgfmt_parse.phpt @@ -36,9 +36,15 @@ function ut_main() foreach( $locales as $locale => $pattern ) { $str_res .= "\nLocale is: $locale\n"; - $fmt = ut_msgfmt_create( $locale, $pattern ); - if(!$fmt) { - $str_res .= dump(intl_get_error_message())."\n"; + try { + $fmt = ut_msgfmt_create( $locale, $pattern ); + if(!$fmt) { + $str_res .= dump(intl_get_error_message())."\n"; + continue; + } + } + catch (\IntlException $ie) { + $str_res .= "IE: ".$ie->getMessage().PHP_EOL; continue; } $str_res .= dump( ut_msgfmt_parse( $fmt, $results[$locale] ) ) . "\n"; @@ -103,7 +109,7 @@ array ( ) Locale is: root -'msgfmt_create: message formatter creation failed: U_ILLEGAL_ARGUMENT_ERROR' +IE: msgfmt_create: message formatter creation failed Locale is: fr array ( diff --git a/ext/intl/tests/resourcebundle_create.phpt b/ext/intl/tests/resourcebundle_create.phpt index 2bf4f556a8609..99492a698b0b3 100644 --- a/ext/intl/tests/resourcebundle_create.phpt +++ b/ext/intl/tests/resourcebundle_create.phpt @@ -14,6 +14,7 @@ function ut_main() { $str_res .= debug( $r1 ); $str_res .= print_r( $r1['teststring'], true)."\n"; + // non-root one $r1 = ut_resourcebundle_create( 'es', BUNDLE ); $str_res .= debug( $r1 ); @@ -24,14 +25,22 @@ function ut_main() { $str_res .= debug( $r1 ); $str_res .= print_r( $r1['testsring'], true); - // fall out - $r2 = ut_resourcebundle_create( 'en_US', BUNDLE, false ); - $str_res .= debug( $r2 ); + try { + // fall out + $r2 = ut_resourcebundle_create( 'en_US', BUNDLE, false ); + } + catch (\IntlException $ie) { + $str_res .= "ie: ".$ie->getMessage().PHP_EOL; + } + + try { + // missing + $r3 = ut_resourcebundle_create( 'en_US', 'nonexisting' ); + } + catch (\IntlException $ie) { + $str_res .= "ie: ".$ie->getMessage().PHP_EOL; + } - // missing - $r3 = ut_resourcebundle_create( 'en_US', 'nonexisting' ); - $str_res .= debug( $r3 ); - return $str_res; } @@ -56,7 +65,5 @@ ResourceBundle Object ) -127: U_USING_DEFAULT_WARNING -NULL - 2: resourcebundle_ctor: Cannot load libICU resource bundle: U_MISSING_RESOURCE_ERROR -NULL - 2: resourcebundle_ctor: Cannot load libICU resource bundle: U_MISSING_RESOURCE_ERROR +ie: resourcebundle_ctor: Cannot load libICU resource bundle +ie: resourcebundle_ctor: Cannot load libICU resource bundle diff --git a/ext/intl/transliterator/transliterator_methods.c b/ext/intl/transliterator/transliterator_methods.c index d8b030a6d14ee..7be1412b1328f 100644 --- a/ext/intl/transliterator/transliterator_methods.c +++ b/ext/intl/transliterator/transliterator_methods.c @@ -169,7 +169,7 @@ PHP_FUNCTION( transliterator_create_from_rules ) str_rules, str_rules_len, TRANSLITERATOR_ERROR_CODE_P( to ) ); /* (I'm not a big fan of non-obvious flow control macros ). * This one checks the error value, destroys object and returns false */ - INTL_CTOR_CHECK_STATUS( to, "String conversion of rules to UTF-16 failed" ); + INTL_CTOR_CHECK_STATUS( to, "String conversion of rules to UTF-16 failed", 0 ); /* Open ICU Transliterator. */ utrans = utrans_openU( id, ( sizeof( id ) - 1 ) / ( sizeof( *id ) ), (UTransDirection ) direction, @@ -197,7 +197,7 @@ PHP_FUNCTION( transliterator_create_from_rules ) } transliterator_object_construct( object, utrans, TRANSLITERATOR_ERROR_CODE_P( to ) ); /* no need to close the transliterator manually on construction error */ - INTL_CTOR_CHECK_STATUS( to, "transliterator_create_from_rules: internal constructor call failed" ); + INTL_CTOR_CHECK_STATUS( to, "transliterator_create_from_rules: internal constructor call failed", 0 ); } /* }}} */ @@ -228,10 +228,10 @@ PHP_FUNCTION( transliterator_create_inverse ) utrans = utrans_openInverse( to_orig->utrans, TRANSLITERATOR_ERROR_CODE_P( to ) ); INTL_CTOR_CHECK_STATUS( to, "transliterator_create_inverse: could not create " - "inverse ICU transliterator" ); + "inverse ICU transliterator", 0 ); transliterator_object_construct( object, utrans, TRANSLITERATOR_ERROR_CODE_P( to ) ); /* no need to close the transliterator manually on construction error */ - INTL_CTOR_CHECK_STATUS( to, "transliterator_create: internal constructor call failed" ); + INTL_CTOR_CHECK_STATUS( to, "transliterator_create: internal constructor call failed", 0 ); } /* }}} */ From 0922eca0c7fd6864b4b631a48ff366ed10bc24ca Mon Sep 17 00:00:00 2001 From: Danack Date: Sun, 15 Mar 2015 14:47:07 +0000 Subject: [PATCH 06/11] Made Phar throw exception on bad constructor. --- ext/phar/phar_object.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index 0b173d1e0bb82..797681698868b 100644 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -1120,17 +1120,26 @@ PHP_METHOD(Phar, __construct) phar_archive_object *phar_obj; phar_archive_data *phar_data; zval *zobj = getThis(), arg1, arg2; + zend_error_handling zeh; + int rv; phar_obj = (phar_archive_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset); is_data = instanceof_function(Z_OBJCE_P(zobj), phar_ce_data); if (is_data) { - if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ls!l", &fname, &fname_len, &flags, &alias, &alias_len, &format) == FAILURE) { + zend_replace_error_handling(EH_THROW, phar_ce_PharException, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "s|ls!l", &fname, &fname_len, &flags, &alias, &alias_len, &format); + zend_restore_error_handling(&zeh TSRMLS_CC); + if (rv == FAILURE) { return; } } else { - if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ls!", &fname, &fname_len, &flags, &alias, &alias_len) == FAILURE) { + zend_replace_error_handling(EH_THROW, phar_ce_PharException, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "s|ls!", &fname, &fname_len, &flags, &alias, &alias_len); + zend_restore_error_handling(&zeh TSRMLS_CC); + if (rv == FAILURE) { + //Exception was thrown already return; } } @@ -4343,8 +4352,14 @@ PHP_METHOD(PharFileInfo, __construct) phar_entry_info *entry_info; phar_archive_data *phar_data; zval *zobj = getThis(), arg1; + zend_error_handling zeh; + int rv; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &fname, &fname_len) == FAILURE) { + zend_replace_error_handling(EH_THROW, phar_ce_PharException, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "s", &fname, &fname_len); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (rv == FAILURE) { return; } From 78ebf83ad3299bdb2617da60c9be68ce2cf33fb4 Mon Sep 17 00:00:00 2001 From: Danack Date: Sun, 15 Mar 2015 14:54:49 +0000 Subject: [PATCH 07/11] Fixed fileinfo behaviour. --- ext/fileinfo/fileinfo.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ext/fileinfo/fileinfo.c b/ext/fileinfo/fileinfo.c index b4a556d57bcd5..33c67d1ec1dcc 100644 --- a/ext/fileinfo/fileinfo.c +++ b/ext/fileinfo/fileinfo.c @@ -301,8 +301,18 @@ PHP_FUNCTION(finfo_open) php_fileinfo *finfo; FILEINFO_DECLARE_INIT_OBJECT(object) char resolved_path[MAXPATHLEN]; + zend_error_handling zeh; + int rv; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lp", &options, &file, &file_len) == FAILURE) { + if (object) { + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "|lp", &options, &file, &file_len); + zend_restore_error_handling(&zeh TSRMLS_CC); + } else { + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "|lp", &options, &file, &file_len); + } + + if (rv == FAILURE) { FILEINFO_DESTROY_OBJECT(object); RETURN_FALSE; } From d3267d0092a09eb3a0da2bd953ad21cf5bae325a Mon Sep 17 00:00:00 2001 From: Danack Date: Sun, 15 Mar 2015 15:03:03 +0000 Subject: [PATCH 08/11] Fixed PDO constructor to not return null. --- ext/pdo/pdo_dbh.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c index de9a6abbdc2e4..1796435c5fb02 100644 --- a/ext/pdo/pdo_dbh.c +++ b/ext/pdo/pdo_dbh.c @@ -208,9 +208,15 @@ static PHP_METHOD(PDO, dbh_constructor) zval *options = NULL; char alt_dsn[512]; int call_factory = 1; + zend_error_handling zeh; + int rv; - if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!s!a!", &data_source, &data_source_len, - &username, &usernamelen, &password, &passwordlen, &options)) { + zend_replace_error_handling(EH_THROW, pdo_exception_ce, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!s!a!", &data_source, &data_source_len, + &username, &usernamelen, &password, &passwordlen, &options); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (FAILURE == rv) { ZEND_CTOR_MAKE_NULL(); return; } From 9803fccfbc1022e29dc89d04fcd3618ddf0c60ec Mon Sep 17 00:00:00 2001 From: Danack Date: Sun, 15 Mar 2015 15:30:25 +0000 Subject: [PATCH 09/11] Made UConverter throw an exception if the constructor fails. --- ext/intl/converter/converter.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ext/intl/converter/converter.c b/ext/intl/converter/converter.c index 9bba427409a9f..3b830bd440971 100644 --- a/ext/intl/converter/converter.c +++ b/ext/intl/converter/converter.c @@ -22,6 +22,7 @@ #include #include "../intl_error.h" +#include "../intl_common.h" typedef struct _php_converter_object { UConverter *src, *dest; @@ -556,11 +557,17 @@ static PHP_METHOD(UConverter, __construct) { size_t src_len = sizeof("utf-8") - 1; char *dest = src; size_t dest_len = src_len; + zend_error_handling zeh; + int rv; intl_error_reset(NULL); - if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!", - &dest, &dest_len, &src, &src_len) == FAILURE) { + zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!", + &dest, &dest_len, &src, &src_len); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (rv == FAILURE) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "UConverter::__construct(): bad arguments", 0); return; From 6b232643e5c3b8f20a968e040b7c75695608a15b Mon Sep 17 00:00:00 2001 From: Danack Date: Sun, 15 Mar 2015 18:03:08 +0000 Subject: [PATCH 10/11] Reverted change to function name and added note of why it is different from the class it is actually changing. --- ext/pdo/pdo_stmt.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c index 947428789321f..ace865899e457 100644 --- a/ext/pdo/pdo_stmt.c +++ b/ext/pdo/pdo_stmt.c @@ -113,9 +113,9 @@ ZEND_END_ARG_INFO() RETURN_FALSE; \ } \ -static PHP_FUNCTION(dbrow_constructor) /* {{{ */ +//The class is called PDORow in userland, it is called PDOStatement in the manual. +static PHP_FUNCTION(dbstmt_constructor) /* {{{ */ { -// php_error_docref(NULL, E_ERROR, "You should not create a PDOStatement manually"); zend_throw_exception_ex(php_pdo_get_exception(), 0, "You may not create a PDORow manually"); } /* }}} */ @@ -2641,7 +2641,8 @@ static union _zend_function *row_get_ctor(zend_object *object) ctor.type = ZEND_INTERNAL_FUNCTION; ctor.function_name = zend_string_init("__construct", sizeof("__construct") - 1, 0); ctor.scope = pdo_row_ce; - ctor.handler = ZEND_FN(dbrow_constructor); + //The class is called PDORow in userland, it is called PDOStatement in the manual. + ctor.handler = ZEND_FN(dbstmt_constructor); ctor.fn_flags = ZEND_ACC_PUBLIC; return (union _zend_function*)&ctor; From 910a3243063f0490a60e5c3c0e1467a6171f4e39 Mon Sep 17 00:00:00 2001 From: Danack Date: Mon, 16 Mar 2015 23:14:56 +0000 Subject: [PATCH 11/11] Fixed indentation. Fixed comment style. Fixed commented out code. --- ext/intl/converter/converter.c | 4 ++-- ext/intl/dateformat/dateformat_create.cpp | 4 ++-- ext/spl/spl_fixedarray.c | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ext/intl/converter/converter.c b/ext/intl/converter/converter.c index 3b830bd440971..760d763b83dc6 100644 --- a/ext/intl/converter/converter.c +++ b/ext/intl/converter/converter.c @@ -563,9 +563,9 @@ static PHP_METHOD(UConverter, __construct) { intl_error_reset(NULL); zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &zeh TSRMLS_CC); - rv = zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!", + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!", &dest, &dest_len, &src, &src_len); - zend_restore_error_handling(&zeh TSRMLS_CC); + zend_restore_error_handling(&zeh TSRMLS_CC); if (rv == FAILURE) { intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, diff --git a/ext/intl/dateformat/dateformat_create.cpp b/ext/intl/dateformat/dateformat_create.cpp index 9d6d5f4e97a38..31fcaeb7fcb90 100644 --- a/ext/intl/dateformat/dateformat_create.cpp +++ b/ext/intl/dateformat/dateformat_create.cpp @@ -79,8 +79,8 @@ static void datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor) DATE_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK; if (DATE_FORMAT_OBJECT(dfo) != NULL) { - // This is __construct being called on an instance - it is not - // a constructor. + /* This is __construct being called on an instance - it is not + a constructor. */ intl_errors_set(INTL_DATA_ERROR_P(dfo), U_ILLEGAL_ARGUMENT_ERROR, "datefmt_create: cannot call constructor twice", 0); return; diff --git a/ext/spl/spl_fixedarray.c b/ext/spl/spl_fixedarray.c index dcac83d60df5c..9dbfa61344edd 100644 --- a/ext/spl/spl_fixedarray.c +++ b/ext/spl/spl_fixedarray.c @@ -564,11 +564,11 @@ SPL_METHOD(SplFixedArray, __construct) int rv; zend_error_handling zeh; -// zend_replace_error_handling(EH_THROW, spl_ce_InvalidArgumentException, &zeh TSRMLS_CC); -// rv = zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &size); -// zend_restore_error_handling(&zeh TSRMLS_CC); + zend_replace_error_handling(EH_THROW, spl_ce_InvalidArgumentException, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &size); + zend_restore_error_handling(&zeh TSRMLS_CC); - if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &size) == FAILURE) { + if (rv == FAILURE) { return; }