Skip to content

Commit 0e43563

Browse files
committed
Promote some warnings in MBString Regex
1 parent 7222315 commit 0e43563

File tree

11 files changed

+200
-163
lines changed

11 files changed

+200
-163
lines changed

ext/mbstring/php_mbregex.c

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "php_mbregex.h"
2727
#include "mbstring.h"
2828
#include "libmbfl/filters/mbfilter_utf8.h"
29+
#include <stdbool.h>
2930

3031
#include "php_onig_compat.h" /* must come prior to the oniguruma header */
3132
#include <oniguruma.h>
@@ -591,8 +592,8 @@ static size_t _php_mb_regex_get_option_string(char *str, size_t len, OnigOptionT
591592
/* }}} */
592593

593594
/* {{{ _php_mb_regex_init_options */
594-
static void
595-
_php_mb_regex_init_options(const char *parg, size_t narg, OnigOptionType *option, OnigSyntaxType **syntax, int *eval)
595+
static bool _php_mb_regex_init_options(const char *parg, size_t narg, OnigOptionType *option,
596+
OnigSyntaxType **syntax, uint32_t option_arg_num)
596597
{
597598
size_t n;
598599
char c;
@@ -651,14 +652,16 @@ _php_mb_regex_init_options(const char *parg, size_t narg, OnigOptionType *option
651652
*syntax = ONIG_SYNTAX_POSIX_EXTENDED;
652653
break;
653654
case 'e':
654-
if (eval != NULL) *eval = 1;
655-
break;
655+
zend_argument_value_error(option_arg_num, "option 'e' is not supported");
656+
return false;
656657
default:
658+
// TODO Unsupported ValueError
657659
break;
658660
}
659661
}
660662
if (option != NULL) *option|=optm;
661663
}
664+
return true;
662665
}
663666
/* }}} */
664667

@@ -900,6 +903,11 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
900903
RETURN_THROWS();
901904
}
902905

906+
if (arg_pattern_len == 0) {
907+
zend_argument_value_error(1, "must not be empty");
908+
RETURN_THROWS();
909+
}
910+
903911
if (array != NULL) {
904912
array = zend_try_array_init(array);
905913
if (!array) {
@@ -912,20 +920,15 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
912920
string_len,
913921
php_mb_regex_get_mbctype_encoding()
914922
)) {
915-
RETURN_FALSE;
923+
zend_argument_value_error(2, "must be a valid string in '%s'", php_mb_regex_get_mbctype());
924+
RETURN_THROWS();
916925
}
917926

918927
options = MBREX(regex_default_options);
919928
if (icase) {
920929
options |= ONIG_OPTION_IGNORECASE;
921930
}
922931

923-
if (arg_pattern_len == 0) {
924-
php_error_docref(NULL, E_WARNING, "Empty pattern");
925-
RETVAL_FALSE;
926-
goto out;
927-
}
928-
929932
re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, options, MBREX(regex_default_syntax));
930933
if (re == NULL) {
931934
RETVAL_FALSE;
@@ -1007,15 +1010,14 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
10071010
smart_str out_buf = {0};
10081011
smart_str eval_buf = {0};
10091012
smart_str *pbuf;
1010-
int err, eval, n;
1013+
int err, n;
10111014
OnigUChar *pos;
10121015
OnigUChar *string_lim;
10131016
char *description = NULL;
10141017

10151018
const mbfl_encoding *enc = php_mb_regex_get_mbctype_encoding();
10161019
ZEND_ASSERT(enc != NULL);
10171020

1018-
eval = 0;
10191021
{
10201022
char *option_str = NULL;
10211023
size_t option_str_len = 0;
@@ -1039,28 +1041,25 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
10391041
}
10401042

10411043
if (!php_mb_check_encoding(string, string_len, enc)) {
1042-
RETURN_NULL();
1044+
zend_argument_value_error(3, "must be a valid string in '%s'", php_mb_regex_get_mbctype());
1045+
RETURN_THROWS();
10431046
}
10441047

10451048
if (option_str != NULL) {
1046-
_php_mb_regex_init_options(option_str, option_str_len, &options, &syntax, &eval);
1049+
/* Initialize option and in case of failure it means there is a value error */
1050+
if(!_php_mb_regex_init_options(option_str, option_str_len, &options, &syntax, 4)) {
1051+
RETURN_THROWS();
1052+
}
10471053
} else {
10481054
options |= MBREX(regex_default_options);
10491055
syntax = MBREX(regex_default_syntax);
10501056
}
10511057
}
1052-
if (eval) {
1053-
if (is_callable) {
1054-
php_error_docref(NULL, E_WARNING, "Option 'e' cannot be used with replacement callback");
1055-
} else {
1056-
php_error_docref(NULL, E_WARNING, "The 'e' option is no longer supported, use mb_ereg_replace_callback instead");
1057-
}
1058-
RETURN_FALSE;
1059-
}
10601058

10611059
/* create regex pattern buffer */
10621060
re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, options, syntax);
10631061
if (re == NULL) {
1062+
// Should this be considered an error instead?
10641063
RETURN_FALSE;
10651064
}
10661065

@@ -1122,7 +1121,9 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
11221121
zval_ptr_dtor(&retval);
11231122
} else {
11241123
if (!EG(exception)) {
1125-
php_error_docref(NULL, E_WARNING, "Unable to call custom replacement function");
1124+
zend_throw_error(NULL, "Unable to call custom replacement function");
1125+
zval_ptr_dtor(&subpats);
1126+
RETURN_THROWS();
11261127
}
11271128
}
11281129
zval_ptr_dtor(&subpats);
@@ -1154,6 +1155,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
11541155
}
11551156
smart_str_free(&eval_buf);
11561157

1158+
// Need to investigate if failure in Oniguruma and if should throw.
11571159
if (err <= -2) {
11581160
smart_str_free(&out_buf);
11591161
RETVAL_FALSE;
@@ -1210,11 +1212,13 @@ PHP_FUNCTION(mb_split)
12101212
}
12111213

12121214
if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype_encoding())) {
1213-
RETURN_FALSE;
1215+
zend_argument_value_error(2, "must be a valid string in '%s'", php_mb_regex_get_mbctype());
1216+
RETURN_THROWS();
12141217
}
12151218

12161219
/* create regex pattern buffer */
12171220
if ((re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, MBREX(regex_default_options), MBREX(regex_default_syntax))) == NULL) {
1221+
// TODO throw as invalid regex?
12181222
RETURN_FALSE;
12191223
}
12201224

@@ -1251,6 +1255,7 @@ PHP_FUNCTION(mb_split)
12511255
onig_region_free(regs, 1);
12521256

12531257
/* see if we encountered an error */
1258+
// ToDo investigate if this can actually/should happen ...
12541259
if (err <= -2) {
12551260
OnigUChar err_str[ONIG_MAX_ERROR_MESSAGE_LEN];
12561261
onig_error_code_to_str(err_str, err);
@@ -1295,18 +1300,22 @@ PHP_FUNCTION(mb_ereg_match)
12951300
}
12961301

12971302
if (option_str != NULL) {
1298-
_php_mb_regex_init_options(option_str, option_str_len, &option, &syntax, NULL);
1303+
if(!_php_mb_regex_init_options(option_str, option_str_len, &option, &syntax, 3)) {
1304+
RETURN_THROWS();
1305+
}
12991306
} else {
13001307
option |= MBREX(regex_default_options);
13011308
syntax = MBREX(regex_default_syntax);
13021309
}
13031310
}
13041311

13051312
if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype_encoding())) {
1306-
RETURN_FALSE;
1313+
zend_argument_value_error(2, "must be a valid string in '%s'", php_mb_regex_get_mbctype());
1314+
RETURN_THROWS();
13071315
}
13081316

13091317
if ((re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, option, syntax)) == NULL) {
1318+
// TODO throw as invalid regex?
13101319
RETURN_FALSE;
13111320
}
13121321

@@ -1362,6 +1371,7 @@ static void _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mod
13621371
if (arg_pattern) {
13631372
/* create regex pattern buffer */
13641373
if ((MBREX(search_re) = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, option, syntax)) == NULL) {
1374+
// TODO throw as invalid regex?
13651375
RETURN_FALSE;
13661376
}
13671377
}
@@ -1375,13 +1385,13 @@ static void _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mod
13751385
}
13761386

13771387
if (MBREX(search_re) == NULL) {
1378-
php_error_docref(NULL, E_WARNING, "No regex given");
1379-
RETURN_FALSE;
1388+
zend_throw_error(NULL, "No pattern was provided");
1389+
RETURN_THROWS();
13801390
}
13811391

13821392
if (str == NULL) {
1383-
php_error_docref(NULL, E_WARNING, "No string given");
1384-
RETURN_FALSE;
1393+
zend_throw_error(NULL, "No string was provided");
1394+
RETURN_THROWS();
13851395
}
13861396

13871397
MBREX(search_regs) = onig_region_new();
@@ -1480,8 +1490,8 @@ PHP_FUNCTION(mb_ereg_search_init)
14801490
}
14811491

14821492
if (arg_pattern && arg_pattern_len == 0) {
1483-
php_error_docref(NULL, E_WARNING, "Empty pattern");
1484-
RETURN_FALSE;
1493+
zend_argument_value_error(2, "must not be empty");
1494+
RETURN_THROWS();
14851495
}
14861496

14871497
if (arg_options) {
@@ -1495,6 +1505,7 @@ PHP_FUNCTION(mb_ereg_search_init)
14951505
if (arg_pattern) {
14961506
/* create regex pattern buffer */
14971507
if ((MBREX(search_re) = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, option, syntax)) == NULL) {
1508+
// TODO throw as invalid regex?
14981509
RETURN_FALSE;
14991510
}
15001511
}
@@ -1510,9 +1521,13 @@ PHP_FUNCTION(mb_ereg_search_init)
15101521
RETVAL_TRUE;
15111522
} else {
15121523
MBREX(search_pos) = ZSTR_LEN(arg_str);
1513-
RETVAL_FALSE;
1524+
zend_argument_value_error(1, "must be a valid string in '%s'", php_mb_regex_get_mbctype());
1525+
RETURN_THROWS();
15141526
}
15151527

1528+
MBREX(search_pos) = 0;
1529+
RETVAL_TRUE;
1530+
15161531
if (MBREX(search_regs) != NULL) {
15171532
onig_region_free(MBREX(search_regs), 1);
15181533
MBREX(search_regs) = NULL;
@@ -1557,6 +1572,7 @@ PHP_FUNCTION(mb_ereg_search_getregs)
15571572
onig_foreach_name(MBREX(search_re), mb_regex_groups_iter, &args);
15581573
}
15591574
} else {
1575+
// TODO This seems to be some logical error, promote to Error
15601576
RETVAL_FALSE;
15611577
}
15621578
}
@@ -1588,12 +1604,12 @@ PHP_FUNCTION(mb_ereg_search_setpos)
15881604
}
15891605

15901606
if (position < 0 || (!Z_ISUNDEF(MBREX(search_str)) && Z_TYPE(MBREX(search_str)) == IS_STRING && (size_t)position > Z_STRLEN(MBREX(search_str)))) {
1591-
php_error_docref(NULL, E_WARNING, "Position is out of range");
1592-
MBREX(search_pos) = 0;
1593-
RETURN_FALSE;
1607+
zend_argument_value_error(1, "is out of range");
1608+
RETURN_THROWS();
15941609
}
15951610

15961611
MBREX(search_pos) = position;
1612+
// TODO Return void
15971613
RETURN_TRUE;
15981614
}
15991615
/* }}} */
@@ -1628,7 +1644,9 @@ PHP_FUNCTION(mb_regex_set_options)
16281644
if (string != NULL) {
16291645
opt = 0;
16301646
syntax = NULL;
1631-
_php_mb_regex_init_options(string, string_len, &opt, &syntax, NULL);
1647+
if(!_php_mb_regex_init_options(string, string_len, &opt, &syntax, 1)) {
1648+
RETURN_THROWS();
1649+
}
16321650
_php_mb_regex_set_options(opt, syntax, &prev_opt, &prev_syntax);
16331651
opt = prev_opt;
16341652
syntax = prev_syntax;

0 commit comments

Comments
 (0)