Skip to content

Commit 8092994

Browse files
committed
Promote some warnings in MBString Regex
1 parent 3eba187 commit 8092994

File tree

11 files changed

+207
-173
lines changed

11 files changed

+207
-173
lines changed

ext/mbstring/php_mbregex.c

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "php_mbregex.h"
2929
#include "mbstring.h"
3030
#include "libmbfl/filters/mbfilter_utf8.h"
31+
#include <stdbool.h>
3132

3233
#include "php_onig_compat.h" /* must come prior to the oniguruma header */
3334
#include <oniguruma.h>
@@ -600,8 +601,8 @@ static size_t _php_mb_regex_get_option_string(char *str, size_t len, OnigOptionT
600601
/* }}} */
601602

602603
/* {{{ _php_mb_regex_init_options */
603-
static void
604-
_php_mb_regex_init_options(const char *parg, size_t narg, OnigOptionType *option, OnigSyntaxType **syntax, int *eval)
604+
static bool _php_mb_regex_init_options(const char *parg, size_t narg, OnigOptionType *option,
605+
OnigSyntaxType **syntax, uint32_t option_arg_num)
605606
{
606607
size_t n;
607608
char c;
@@ -660,14 +661,16 @@ _php_mb_regex_init_options(const char *parg, size_t narg, OnigOptionType *option
660661
*syntax = ONIG_SYNTAX_POSIX_EXTENDED;
661662
break;
662663
case 'e':
663-
if (eval != NULL) *eval = 1;
664-
break;
664+
zend_argument_value_error(option_arg_num, "option 'e' is not supported");
665+
return false;
665666
default:
667+
// TODO Unsupported ValueError
666668
break;
667669
}
668670
}
669671
if (option != NULL) *option|=optm;
670672
}
673+
return true;
671674
}
672675
/* }}} */
673676

@@ -908,6 +911,11 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
908911
RETURN_THROWS();
909912
}
910913

914+
if (arg_pattern_len == 0) {
915+
zend_argument_value_error(1, "must not be empty");
916+
RETURN_THROWS();
917+
}
918+
911919
if (array != NULL) {
912920
array = zend_try_array_init(array);
913921
if (!array) {
@@ -920,20 +928,15 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
920928
string_len,
921929
php_mb_regex_get_mbctype_encoding()
922930
)) {
923-
RETURN_FALSE;
931+
zend_argument_value_error(2, "must be a valid string in '%s'", php_mb_regex_get_mbctype());
932+
RETURN_THROWS();
924933
}
925934

926935
options = MBREX(regex_default_options);
927936
if (icase) {
928937
options |= ONIG_OPTION_IGNORECASE;
929938
}
930939

931-
if (arg_pattern_len == 0) {
932-
php_error_docref(NULL, E_WARNING, "Empty pattern");
933-
RETVAL_FALSE;
934-
goto out;
935-
}
936-
937940
re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, options, MBREX(regex_default_syntax));
938941
if (re == NULL) {
939942
RETVAL_FALSE;
@@ -1017,15 +1020,14 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
10171020
smart_str out_buf = {0};
10181021
smart_str eval_buf = {0};
10191022
smart_str *pbuf;
1020-
int err, eval, n;
1023+
int err, n;
10211024
OnigUChar *pos;
10221025
OnigUChar *string_lim;
10231026
char *description = NULL;
10241027

10251028
const mbfl_encoding *enc = php_mb_regex_get_mbctype_encoding();
10261029
ZEND_ASSERT(enc != NULL);
10271030

1028-
eval = 0;
10291031
{
10301032
char *option_str = NULL;
10311033
size_t option_str_len = 0;
@@ -1049,28 +1051,25 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
10491051
}
10501052

10511053
if (!php_mb_check_encoding(string, string_len, enc)) {
1052-
RETURN_NULL();
1054+
zend_argument_value_error(3, "must be a valid string in '%s'", php_mb_regex_get_mbctype());
1055+
RETURN_THROWS();
10531056
}
10541057

10551058
if (option_str != NULL) {
1056-
_php_mb_regex_init_options(option_str, option_str_len, &options, &syntax, &eval);
1059+
/* Initialize option and in case of failure it means there is a value error */
1060+
if(!_php_mb_regex_init_options(option_str, option_str_len, &options, &syntax, 4)) {
1061+
RETURN_THROWS();
1062+
}
10571063
} else {
10581064
options |= MBREX(regex_default_options);
10591065
syntax = MBREX(regex_default_syntax);
10601066
}
10611067
}
1062-
if (eval) {
1063-
if (is_callable) {
1064-
php_error_docref(NULL, E_WARNING, "Option 'e' cannot be used with replacement callback");
1065-
} else {
1066-
php_error_docref(NULL, E_WARNING, "The 'e' option is no longer supported, use mb_ereg_replace_callback instead");
1067-
}
1068-
RETURN_FALSE;
1069-
}
10701068

10711069
/* create regex pattern buffer */
10721070
re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, options, syntax);
10731071
if (re == NULL) {
1072+
// Should this be considered an error instead?
10741073
RETURN_FALSE;
10751074
}
10761075

@@ -1132,7 +1131,9 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
11321131
zval_ptr_dtor(&retval);
11331132
} else {
11341133
if (!EG(exception)) {
1135-
php_error_docref(NULL, E_WARNING, "Unable to call custom replacement function");
1134+
zend_throw_error(NULL, "Unable to call custom replacement function");
1135+
zval_ptr_dtor(&subpats);
1136+
RETURN_THROWS();
11361137
}
11371138
}
11381139
zval_ptr_dtor(&subpats);
@@ -1164,6 +1165,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
11641165
}
11651166
smart_str_free(&eval_buf);
11661167

1168+
// Need to investigate if failure in Oniguruma and if should throw.
11671169
if (err <= -2) {
11681170
smart_str_free(&out_buf);
11691171
RETVAL_FALSE;
@@ -1224,11 +1226,13 @@ PHP_FUNCTION(mb_split)
12241226
}
12251227

12261228
if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype_encoding())) {
1227-
RETURN_FALSE;
1229+
zend_argument_value_error(2, "must be a valid string in '%s'", php_mb_regex_get_mbctype());
1230+
RETURN_THROWS();
12281231
}
12291232

12301233
/* create regex pattern buffer */
12311234
if ((re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, MBREX(regex_default_options), MBREX(regex_default_syntax))) == NULL) {
1235+
// TODO throw as invalid regex?
12321236
RETURN_FALSE;
12331237
}
12341238

@@ -1265,6 +1269,7 @@ PHP_FUNCTION(mb_split)
12651269
onig_region_free(regs, 1);
12661270

12671271
/* see if we encountered an error */
1272+
// ToDo investigate if this can actually/should happen ...
12681273
if (err <= -2) {
12691274
OnigUChar err_str[ONIG_MAX_ERROR_MESSAGE_LEN];
12701275
onig_error_code_to_str(err_str, err);
@@ -1310,18 +1315,22 @@ PHP_FUNCTION(mb_ereg_match)
13101315
}
13111316

13121317
if (option_str != NULL) {
1313-
_php_mb_regex_init_options(option_str, option_str_len, &option, &syntax, NULL);
1318+
if(!_php_mb_regex_init_options(option_str, option_str_len, &option, &syntax, 3)) {
1319+
RETURN_THROWS();
1320+
}
13141321
} else {
13151322
option |= MBREX(regex_default_options);
13161323
syntax = MBREX(regex_default_syntax);
13171324
}
13181325
}
13191326

13201327
if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype_encoding())) {
1321-
RETURN_FALSE;
1328+
zend_argument_value_error(2, "must be a valid string in '%s'", php_mb_regex_get_mbctype());
1329+
RETURN_THROWS();
13221330
}
13231331

13241332
if ((re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, option, syntax)) == NULL) {
1333+
// TODO throw as invalid regex?
13251334
RETURN_FALSE;
13261335
}
13271336

@@ -1367,7 +1376,9 @@ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
13671376

13681377
if (arg_options) {
13691378
option = 0;
1370-
_php_mb_regex_init_options(arg_options, arg_options_len, &option, &syntax, NULL);
1379+
if(!_php_mb_regex_init_options(arg_options, arg_options_len, &option, &syntax, 2)) {
1380+
RETURN_THROWS();
1381+
}
13711382
}
13721383

13731384
if (MBREX(search_regs)) {
@@ -1378,6 +1389,7 @@ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
13781389
if (arg_pattern) {
13791390
/* create regex pattern buffer */
13801391
if ((MBREX(search_re) = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, option, MBREX(regex_default_syntax))) == NULL) {
1392+
// TODO throw as invalid regex?
13811393
RETURN_FALSE;
13821394
}
13831395
}
@@ -1391,13 +1403,13 @@ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
13911403
}
13921404

13931405
if (MBREX(search_re) == NULL) {
1394-
php_error_docref(NULL, E_WARNING, "No regex given");
1395-
RETURN_FALSE;
1406+
zend_throw_error(NULL, "No pattern was provided");
1407+
RETURN_THROWS();
13961408
}
13971409

13981410
if (str == NULL) {
1399-
php_error_docref(NULL, E_WARNING, "No string given");
1400-
RETURN_FALSE;
1411+
zend_throw_error(NULL, "No string was provided");
1412+
RETURN_THROWS();
14011413
}
14021414

14031415
MBREX(search_regs) = onig_region_new();
@@ -1500,21 +1512,24 @@ PHP_FUNCTION(mb_ereg_search_init)
15001512
}
15011513

15021514
if (ZEND_NUM_ARGS() > 1 && arg_pattern_len == 0) {
1503-
php_error_docref(NULL, E_WARNING, "Empty pattern");
1504-
RETURN_FALSE;
1515+
zend_argument_value_error(2, "must not be empty");
1516+
RETURN_THROWS();
15051517
}
15061518

15071519
option = MBREX(regex_default_options);
15081520
syntax = MBREX(regex_default_syntax);
15091521

15101522
if (ZEND_NUM_ARGS() == 3) {
15111523
option = 0;
1512-
_php_mb_regex_init_options(arg_options, arg_options_len, &option, &syntax, NULL);
1524+
if(!_php_mb_regex_init_options(arg_options, arg_options_len, &option, &syntax, 3)) {
1525+
RETURN_THROWS();
1526+
}
15131527
}
15141528

15151529
if (ZEND_NUM_ARGS() > 1) {
15161530
/* create regex pattern buffer */
15171531
if ((MBREX(search_re) = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, option, syntax)) == NULL) {
1532+
// TODO throw as invalid regex?
15181533
RETURN_FALSE;
15191534
}
15201535
}
@@ -1525,18 +1540,15 @@ PHP_FUNCTION(mb_ereg_search_init)
15251540

15261541
ZVAL_STR_COPY(&MBREX(search_str), arg_str);
15271542

1528-
if (php_mb_check_encoding(
1529-
ZSTR_VAL(arg_str),
1530-
ZSTR_LEN(arg_str),
1531-
php_mb_regex_get_mbctype_encoding()
1532-
)) {
1533-
MBREX(search_pos) = 0;
1534-
RETVAL_TRUE;
1535-
} else {
1543+
if (!php_mb_check_encoding(ZSTR_VAL(arg_str), ZSTR_LEN(arg_str), php_mb_regex_get_mbctype_encoding())) {
15361544
MBREX(search_pos) = ZSTR_LEN(arg_str);
1537-
RETVAL_FALSE;
1545+
zend_argument_value_error(1, "must be a valid string in '%s'", php_mb_regex_get_mbctype());
1546+
RETURN_THROWS();
15381547
}
15391548

1549+
MBREX(search_pos) = 0;
1550+
RETVAL_TRUE;
1551+
15401552
if (MBREX(search_regs) != NULL) {
15411553
onig_region_free(MBREX(search_regs), 1);
15421554
MBREX(search_regs) = NULL;
@@ -1582,6 +1594,7 @@ PHP_FUNCTION(mb_ereg_search_getregs)
15821594
onig_foreach_name(MBREX(search_re), mb_regex_groups_iter, &args);
15831595
}
15841596
} else {
1597+
// TODO This seems to be some logical error, promote to Error
15851598
RETVAL_FALSE;
15861599
}
15871600
}
@@ -1615,12 +1628,12 @@ PHP_FUNCTION(mb_ereg_search_setpos)
16151628
}
16161629

16171630
if (position < 0 || (!Z_ISUNDEF(MBREX(search_str)) && Z_TYPE(MBREX(search_str)) == IS_STRING && (size_t)position > Z_STRLEN(MBREX(search_str)))) {
1618-
php_error_docref(NULL, E_WARNING, "Position is out of range");
1619-
MBREX(search_pos) = 0;
1620-
RETURN_FALSE;
1631+
zend_argument_value_error(1, "is out of range");
1632+
RETURN_THROWS();
16211633
}
16221634

16231635
MBREX(search_pos) = position;
1636+
// TODO Return void
16241637
RETURN_TRUE;
16251638
}
16261639
/* }}} */
@@ -1656,7 +1669,9 @@ PHP_FUNCTION(mb_regex_set_options)
16561669
if (string != NULL) {
16571670
opt = 0;
16581671
syntax = NULL;
1659-
_php_mb_regex_init_options(string, string_len, &opt, &syntax, NULL);
1672+
if(!_php_mb_regex_init_options(string, string_len, &opt, &syntax, 1)) {
1673+
RETURN_THROWS();
1674+
}
16601675
_php_mb_regex_set_options(opt, syntax, &prev_opt, &prev_syntax);
16611676
opt = prev_opt;
16621677
syntax = prev_syntax;

0 commit comments

Comments
 (0)