Skip to content

Commit 647fb38

Browse files
committed
Ensure RuleBasedBreakIterator constructor throws on failure
Constructors must throw on failure indepdendent of the configured intl error mode.
1 parent 6c8fb12 commit 647fb38

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

ext/intl/breakiterator/rulebasedbreakiterator_methods.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,33 @@ static void _php_intlrbbi_constructor_body(INTERNAL_FUNCTION_PARAMETERS)
5252
UParseError parseError = UParseError();
5353
if (intl_stringFromChar(rulesStr, rules, rules_len, &status)
5454
== FAILURE) {
55-
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
56-
"rbbi_create_instance: rules were not a valid UTF-8 string",
57-
0);
58-
RETURN_NULL();
55+
zend_throw_exception(IntlException_ce_ptr,
56+
"IntlRuleBasedBreakIterator::__construct(): "
57+
"rules were not a valid UTF-8 string", 0);
58+
RETURN_THROWS();
5959
}
6060

6161
rbbi = new RuleBasedBreakIterator(rulesStr, parseError, status);
6262
intl_error_set_code(NULL, status);
6363
if (U_FAILURE(status)) {
64-
char *msg;
6564
smart_str parse_error_str;
6665
parse_error_str = intl_parse_error_to_string(&parseError);
67-
spprintf(&msg, 0, "rbbi_create_instance: unable to create "
68-
"RuleBasedBreakIterator from rules (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "");
66+
zend_throw_exception_ex(IntlException_ce_ptr, 0,
67+
"IntlRuleBasedBreakIterator::__construct(): "
68+
"unable to create RuleBasedBreakIterator from rules (%s)",
69+
parse_error_str.s ? ZSTR_VAL(parse_error_str.s) : "");
6970
smart_str_free(&parse_error_str);
70-
intl_error_set_custom_msg(NULL, msg, 1);
71-
efree(msg);
7271
delete rbbi;
73-
return;
72+
RETURN_THROWS();
7473
}
7574
} else { // compiled
7675
rbbi = new RuleBasedBreakIterator((uint8_t*)rules, rules_len, status);
7776
if (U_FAILURE(status)) {
78-
intl_error_set(NULL, status, "rbbi_create_instance: unable to "
79-
"create instance from compiled rules", 0);
77+
zend_throw_exception(IntlException_ce_ptr,
78+
"IntlRuleBasedBreakIterator::__construct(): "
79+
"unable to create instance from compiled rules", 0);
8080
delete rbbi;
81-
return;
81+
RETURN_THROWS();
8282
}
8383
}
8484

ext/intl/tests/breakiter___construct_error.phpt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ IntlRuleBasedBreakIterator::__construct(): arg errors
44
<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>
55
--FILE--
66
<?php
7-
ini_set("intl.error_level", E_WARNING);
87

98
function print_exception($e) {
109
echo "\nException: " . $e->getMessage() . " in " . $e->getFile() . " on line " . $e->getLine() . "\n";
@@ -38,12 +37,12 @@ try {
3837
}
3938
?>
4039
--EXPECTF--
41-
Exception: IntlRuleBasedBreakIterator::__construct(): rbbi_create_instance: unable to create RuleBasedBreakIterator from rules (parse error on line 1, offset 31) in %s on line %d
40+
Exception: IntlRuleBasedBreakIterator::__construct(): unable to create RuleBasedBreakIterator from rules (parse error on line 1, offset 31) in %s on line %d
4241

4342
Exception: IntlRuleBasedBreakIterator::__construct() expects at least 1 parameter, 0 given in %s on line %d
4443

4544
Exception: IntlRuleBasedBreakIterator::__construct() expects at most 2 parameters, 3 given in %s on line %d
4645

4746
Exception: IntlRuleBasedBreakIterator::__construct(): Argument #2 ($areCompiled) must be of type bool, array given in %s on line %d
4847

49-
Exception: IntlRuleBasedBreakIterator::__construct(): rbbi_create_instance: unable to create instance from compiled rules in %s on line %d
48+
Exception: IntlRuleBasedBreakIterator::__construct(): unable to create instance from compiled rules in %s on line %d

ext/intl/tests/rbbiter___construct_basic.phpt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ if (!extension_loaded('intl'))
66
die('skip intl extension not enabled');
77
--FILE--
88
<?php
9-
ini_set("intl.error_level", E_WARNING);
109
ini_set("intl.default_locale", "pt_PT");
1110

1211
$rules = <<<RULES
@@ -28,11 +27,10 @@ var_dump(get_class($rbbi));
2827
try {
2928
$obj = new IntlRuleBasedBreakIterator('[\p{Letter}\uFFFD]+;[:number:]+', 'aoeu');
3029
} catch (IntlException $e) {
31-
var_dump(intl_get_error_code(), intl_get_error_message());
30+
echo $e->getMessage(), "\n";
3231
}
3332

3433
?>
3534
--EXPECT--
3635
string(26) "IntlRuleBasedBreakIterator"
37-
int(1)
38-
string(93) "rbbi_create_instance: unable to create instance from compiled rules: U_ILLEGAL_ARGUMENT_ERROR"
36+
IntlRuleBasedBreakIterator::__construct(): unable to create instance from compiled rules

0 commit comments

Comments
 (0)