Skip to content

Commit 7903ef5

Browse files
committed
Various things
1 parent e89b490 commit 7903ef5

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

ext/mbstring/php_mbregex.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,6 @@ static php_mb_regex_t *php_mbregex_compile_pattern(const char *pattern, size_t p
465465
OnigEncoding enc = MBREX(current_mbctype);
466466

467467
if (!php_mb_check_encoding(pattern, patlen, php_mb_regex_get_mbctype_encoding())) {
468-
// TODO convert to ValueError after adding pattern_arg_num pass through argument
469468
php_error_docref(NULL, E_WARNING,
470469
"Pattern is not valid under %s encoding", _php_mb_regex_mbctype2name(enc));
471470
return NULL;
@@ -475,7 +474,6 @@ static php_mb_regex_t *php_mbregex_compile_pattern(const char *pattern, size_t p
475474
if (!rc || onig_get_options(rc) != options || onig_get_encoding(rc) != enc || onig_get_syntax(rc) != syntax) {
476475
if ((err_code = onig_new(&retval, (OnigUChar *)pattern, (OnigUChar *)(pattern + patlen), options, enc, syntax, &err_info)) != ONIG_NORMAL) {
477476
onig_error_code_to_str(err_str, err_code, &err_info);
478-
// Convert to error?
479477
php_error_docref(NULL, E_WARNING, "mbregex compile err: %s", err_str);
480478
return NULL;
481479
}
@@ -929,6 +927,7 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
929927
string_len,
930928
php_mb_regex_get_mbctype_encoding()
931929
)) {
930+
// TODO return throw because invalid encoding?
932931
RETURN_FALSE;
933932
}
934933

@@ -1058,6 +1057,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
10581057
string_len,
10591058
php_mb_regex_get_mbctype_encoding()
10601059
)) {
1060+
// TODO return throw because invalid encoding?
10611061
RETURN_NULL();
10621062
}
10631063

@@ -1080,6 +1080,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
10801080
/* create regex pattern buffer */
10811081
re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, options, syntax);
10821082
if (re == NULL) {
1083+
// Should this be considered an error instead?
10831084
RETURN_FALSE;
10841085
}
10851086

@@ -1131,7 +1132,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
11311132
onig_region_free(regs, 1);
11321133
smart_str_free(&out_buf);
11331134
smart_str_free(&eval_buf);
1134-
RETURN_FALSE;
1135+
RETURN_THROWS();
11351136
}
11361137

11371138
/* result of eval */
@@ -1169,8 +1170,9 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
11691170
zval_ptr_dtor(&retval);
11701171
} else {
11711172
if (!EG(exception)) {
1172-
// TODO Is this a return throw?
1173-
php_error_docref(NULL, E_WARNING, "Unable to call custom replacement function");
1173+
zend_throw_error(NULL, "Unable to call custom replacement function");
1174+
zval_ptr_dtor(&subpats);
1175+
RETURN_THROWS();
11741176
}
11751177
}
11761178
zval_ptr_dtor(&subpats);
@@ -1202,6 +1204,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
12021204
}
12031205
smart_str_free(&eval_buf);
12041206

1207+
// Need to investigate if failure in Oniguruma and if should throw.
12051208
if (err <= -2) {
12061209
smart_str_free(&out_buf);
12071210
RETVAL_FALSE;
@@ -1262,11 +1265,13 @@ PHP_FUNCTION(mb_split)
12621265
}
12631266

12641267
if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype_encoding())) {
1268+
// TODO return throw because invalid encoding?
12651269
RETURN_FALSE;
12661270
}
12671271

12681272
/* create regex pattern buffer */
12691273
if ((re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, MBREX(regex_default_options), MBREX(regex_default_syntax))) == NULL) {
1274+
// TODO throw as invalid regex?
12701275
RETURN_FALSE;
12711276
}
12721277

@@ -1303,6 +1308,7 @@ PHP_FUNCTION(mb_split)
13031308
onig_region_free(regs, 1);
13041309

13051310
/* see if we encountered an error */
1311+
// ToDo investigate if this can actually/should happen ...
13061312
if (err <= -2) {
13071313
OnigUChar err_str[ONIG_MAX_ERROR_MESSAGE_LEN];
13081314
onig_error_code_to_str(err_str, err);
@@ -1356,10 +1362,12 @@ PHP_FUNCTION(mb_ereg_match)
13561362
}
13571363

13581364
if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype_encoding())) {
1365+
// TODO return throw because invalid encoding?
13591366
RETURN_FALSE;
13601367
}
13611368

13621369
if ((re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, option, syntax)) == NULL) {
1370+
// TODO throw as invalid regex?
13631371
RETURN_FALSE;
13641372
}
13651373

@@ -1416,6 +1424,7 @@ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
14161424
if (arg_pattern) {
14171425
/* create regex pattern buffer */
14181426
if ((MBREX(search_re) = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, option, MBREX(regex_default_syntax))) == NULL) {
1427+
// TODO throw as invalid regex?
14191428
RETURN_FALSE;
14201429
}
14211430
}
@@ -1429,15 +1438,13 @@ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
14291438
}
14301439

14311440
if (MBREX(search_re) == NULL) {
1432-
// TODO which Error/Exception to use, as this seems to be a logical error in function call ordering
1433-
php_error_docref(NULL, E_WARNING, "No regex given");
1434-
RETURN_FALSE;
1441+
zend_throw_error(NULL, "No pattern was provided");
1442+
RETURN_THROWS();
14351443
}
14361444

14371445
if (str == NULL) {
1438-
// TODO which Error/Exception to use, as this seems to be a logical error in function call ordering
1439-
php_error_docref(NULL, E_WARNING, "No string given");
1440-
RETURN_FALSE;
1446+
zend_throw_error(NULL, "No string was provided");
1447+
RETURN_THROWS();
14411448
}
14421449

14431450
MBREX(search_regs) = onig_region_new();
@@ -1555,6 +1562,7 @@ PHP_FUNCTION(mb_ereg_search_init)
15551562
if (ZEND_NUM_ARGS() > 1) {
15561563
/* create regex pattern buffer */
15571564
if ((MBREX(search_re) = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, option, syntax)) == NULL) {
1565+
// TODO throw as invalid regex?
15581566
RETURN_FALSE;
15591567
}
15601568
}
@@ -1573,6 +1581,7 @@ PHP_FUNCTION(mb_ereg_search_init)
15731581
MBREX(search_pos) = 0;
15741582
RETVAL_TRUE;
15751583
} else {
1584+
// TODO return throw because invalid encoding?
15761585
MBREX(search_pos) = ZSTR_LEN(arg_str);
15771586
RETVAL_FALSE;
15781587
}
@@ -1622,6 +1631,7 @@ PHP_FUNCTION(mb_ereg_search_getregs)
16221631
onig_foreach_name(MBREX(search_re), mb_regex_groups_iter, &args);
16231632
}
16241633
} else {
1634+
// TODO This seems to be some logical error, promote to Error
16251635
RETVAL_FALSE;
16261636
}
16271637
}

ext/mbstring/tests/bug72399.phpt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@ if (!function_exists('mb_ereg')) die('skip mbregex support not available');
88
--FILE--
99
<?php
1010
$var5 = mb_ereg_search_init("","2");
11+
var_dump($var5);
1112
$var6 = mb_eregi_replace("2","","");
12-
$var13 = mb_ereg_search_pos();
13+
var_dump($var6);
14+
15+
try {
16+
$var13 = mb_ereg_search_pos();
17+
} catch (\Error $e) {
18+
echo $e->getMessage() . \PHP_EOL;
19+
}
1320
?>
14-
--EXPECTF--
15-
Warning: mb_ereg_search_pos(): No regex given in %sbug72399.php on line %d
21+
--EXPECT--
22+
bool(true)
23+
string(0) ""
24+
No pattern was provided

ext/mbstring/tests/empty_pattern.phpt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ mb_split("","");
1818

1919
try {
2020
mb_ereg_search_regs();
21-
} catch (\ValueError $e) {
21+
} catch (\Error $e) {
2222
echo $e->getMessage() . \PHP_EOL;
2323
}
2424

2525
?>
26-
--EXPECTF--
26+
--EXPECT--
2727
mb_ereg_search_init(): Argument #2 ($pattern) must not be empty
28-
29-
Warning: mb_ereg_search_regs(): No regex given in %s on line %d
28+
No pattern was provided

0 commit comments

Comments
 (0)