Ensure RuleBasedBreakIterator constructor throws on failure

Constructors must throw on failure indepdendent of the configured
intl error mode.
This commit is contained in:
Nikita Popov 2020-08-25 15:28:58 +02:00
parent 6c8fb123d2
commit 647fb38d58
3 changed files with 17 additions and 20 deletions

View file

@ -52,33 +52,33 @@ static void _php_intlrbbi_constructor_body(INTERNAL_FUNCTION_PARAMETERS)
UParseError parseError = UParseError();
if (intl_stringFromChar(rulesStr, rules, rules_len, &status)
== FAILURE) {
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
"rbbi_create_instance: rules were not a valid UTF-8 string",
0);
RETURN_NULL();
zend_throw_exception(IntlException_ce_ptr,
"IntlRuleBasedBreakIterator::__construct(): "
"rules were not a valid UTF-8 string", 0);
RETURN_THROWS();
}
rbbi = new RuleBasedBreakIterator(rulesStr, parseError, status);
intl_error_set_code(NULL, status);
if (U_FAILURE(status)) {
char *msg;
smart_str parse_error_str;
parse_error_str = intl_parse_error_to_string(&parseError);
spprintf(&msg, 0, "rbbi_create_instance: unable to create "
"RuleBasedBreakIterator from rules (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "");
zend_throw_exception_ex(IntlException_ce_ptr, 0,
"IntlRuleBasedBreakIterator::__construct(): "
"unable to create RuleBasedBreakIterator from rules (%s)",
parse_error_str.s ? ZSTR_VAL(parse_error_str.s) : "");
smart_str_free(&parse_error_str);
intl_error_set_custom_msg(NULL, msg, 1);
efree(msg);
delete rbbi;
return;
RETURN_THROWS();
}
} else { // compiled
rbbi = new RuleBasedBreakIterator((uint8_t*)rules, rules_len, status);
if (U_FAILURE(status)) {
intl_error_set(NULL, status, "rbbi_create_instance: unable to "
"create instance from compiled rules", 0);
zend_throw_exception(IntlException_ce_ptr,
"IntlRuleBasedBreakIterator::__construct(): "
"unable to create instance from compiled rules", 0);
delete rbbi;
return;
RETURN_THROWS();
}
}

View file

@ -4,7 +4,6 @@ IntlRuleBasedBreakIterator::__construct(): arg errors
<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>
--FILE--
<?php
ini_set("intl.error_level", E_WARNING);
function print_exception($e) {
echo "\nException: " . $e->getMessage() . " in " . $e->getFile() . " on line " . $e->getLine() . "\n";
@ -38,7 +37,7 @@ try {
}
?>
--EXPECTF--
Exception: IntlRuleBasedBreakIterator::__construct(): rbbi_create_instance: unable to create RuleBasedBreakIterator from rules (parse error on line 1, offset 31) in %s on line %d
Exception: IntlRuleBasedBreakIterator::__construct(): unable to create RuleBasedBreakIterator from rules (parse error on line 1, offset 31) in %s on line %d
Exception: IntlRuleBasedBreakIterator::__construct() expects at least 1 parameter, 0 given in %s on line %d
@ -46,4 +45,4 @@ Exception: IntlRuleBasedBreakIterator::__construct() expects at most 2 parameter
Exception: IntlRuleBasedBreakIterator::__construct(): Argument #2 ($areCompiled) must be of type bool, array given in %s on line %d
Exception: IntlRuleBasedBreakIterator::__construct(): rbbi_create_instance: unable to create instance from compiled rules in %s on line %d
Exception: IntlRuleBasedBreakIterator::__construct(): unable to create instance from compiled rules in %s on line %d

View file

@ -6,7 +6,6 @@ if (!extension_loaded('intl'))
die('skip intl extension not enabled');
--FILE--
<?php
ini_set("intl.error_level", E_WARNING);
ini_set("intl.default_locale", "pt_PT");
$rules = <<<RULES
@ -28,11 +27,10 @@ var_dump(get_class($rbbi));
try {
$obj = new IntlRuleBasedBreakIterator('[\p{Letter}\uFFFD]+;[:number:]+', 'aoeu');
} catch (IntlException $e) {
var_dump(intl_get_error_code(), intl_get_error_message());
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
string(26) "IntlRuleBasedBreakIterator"
int(1)
string(93) "rbbi_create_instance: unable to create instance from compiled rules: U_ILLEGAL_ARGUMENT_ERROR"
IntlRuleBasedBreakIterator::__construct(): unable to create instance from compiled rules