28
28
#include "php_mbregex.h"
29
29
#include "mbstring.h"
30
30
#include "libmbfl/filters/mbfilter_utf8.h"
31
+ #include <stdbool.h>
31
32
32
33
#include "php_onig_compat.h" /* must come prior to the oniguruma header */
33
34
#include <oniguruma.h>
@@ -600,8 +601,8 @@ static size_t _php_mb_regex_get_option_string(char *str, size_t len, OnigOptionT
600
601
/* }}} */
601
602
602
603
/* {{{ _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 )
605
606
{
606
607
size_t n ;
607
608
char c ;
@@ -660,14 +661,16 @@ _php_mb_regex_init_options(const char *parg, size_t narg, OnigOptionType *option
660
661
* syntax = ONIG_SYNTAX_POSIX_EXTENDED ;
661
662
break ;
662
663
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 ;
665
666
default :
667
+ // TODO Unsupported ValueError
666
668
break ;
667
669
}
668
670
}
669
671
if (option != NULL ) * option |=optm ;
670
672
}
673
+ return true;
671
674
}
672
675
/* }}} */
673
676
@@ -910,6 +913,11 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
910
913
RETURN_THROWS ();
911
914
}
912
915
916
+ if (arg_pattern_len == 0 ) {
917
+ zend_argument_value_error (1 , "must not be empty" );
918
+ RETURN_THROWS ();
919
+ }
920
+
913
921
if (array != NULL ) {
914
922
array = zend_try_array_init (array );
915
923
if (!array ) {
@@ -922,20 +930,15 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
922
930
string_len ,
923
931
php_mb_regex_get_mbctype_encoding ()
924
932
)) {
925
- RETURN_FALSE ;
933
+ zend_argument_value_error (2 , "must be a valid string in '%s'" , php_mb_regex_get_mbctype ());
934
+ RETURN_THROWS ();
926
935
}
927
936
928
937
options = MBREX (regex_default_options );
929
938
if (icase ) {
930
939
options |= ONIG_OPTION_IGNORECASE ;
931
940
}
932
941
933
- if (arg_pattern_len == 0 ) {
934
- php_error_docref (NULL , E_WARNING , "Empty pattern" );
935
- RETVAL_FALSE ;
936
- goto out ;
937
- }
938
-
939
942
re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , options , MBREX (regex_default_syntax ));
940
943
if (re == NULL ) {
941
944
RETVAL_FALSE ;
@@ -1019,15 +1022,14 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1019
1022
smart_str out_buf = {0 };
1020
1023
smart_str eval_buf = {0 };
1021
1024
smart_str * pbuf ;
1022
- int err , eval , n ;
1025
+ int err , n ;
1023
1026
OnigUChar * pos ;
1024
1027
OnigUChar * string_lim ;
1025
1028
char * description = NULL ;
1026
1029
1027
1030
const mbfl_encoding * enc = php_mb_regex_get_mbctype_encoding ();
1028
1031
ZEND_ASSERT (enc != NULL );
1029
1032
1030
- eval = 0 ;
1031
1033
{
1032
1034
char * option_str = NULL ;
1033
1035
size_t option_str_len = 0 ;
@@ -1051,28 +1053,25 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1051
1053
}
1052
1054
1053
1055
if (!php_mb_check_encoding (string , string_len , enc )) {
1054
- RETURN_NULL ();
1056
+ zend_argument_value_error (3 , "must be a valid string in '%s'" , php_mb_regex_get_mbctype ());
1057
+ RETURN_THROWS ();
1055
1058
}
1056
1059
1057
1060
if (option_str != NULL ) {
1058
- _php_mb_regex_init_options (option_str , option_str_len , & options , & syntax , & eval );
1061
+ /* Initialize option and in case of failure it means there is a value error */
1062
+ if (!_php_mb_regex_init_options (option_str , option_str_len , & options , & syntax , 4 )) {
1063
+ RETURN_THROWS ();
1064
+ }
1059
1065
} else {
1060
1066
options |= MBREX (regex_default_options );
1061
1067
syntax = MBREX (regex_default_syntax );
1062
1068
}
1063
1069
}
1064
- if (eval ) {
1065
- if (is_callable ) {
1066
- php_error_docref (NULL , E_WARNING , "Option 'e' cannot be used with replacement callback" );
1067
- } else {
1068
- php_error_docref (NULL , E_WARNING , "The 'e' option is no longer supported, use mb_ereg_replace_callback instead" );
1069
- }
1070
- RETURN_FALSE ;
1071
- }
1072
1070
1073
1071
/* create regex pattern buffer */
1074
1072
re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , options , syntax );
1075
1073
if (re == NULL ) {
1074
+ // Should this be considered an error instead?
1076
1075
RETURN_FALSE ;
1077
1076
}
1078
1077
@@ -1134,7 +1133,9 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1134
1133
zval_ptr_dtor (& retval );
1135
1134
} else {
1136
1135
if (!EG (exception )) {
1137
- php_error_docref (NULL , E_WARNING , "Unable to call custom replacement function" );
1136
+ zend_throw_error (NULL , "Unable to call custom replacement function" );
1137
+ zval_ptr_dtor (& subpats );
1138
+ RETURN_THROWS ();
1138
1139
}
1139
1140
}
1140
1141
zval_ptr_dtor (& subpats );
@@ -1166,6 +1167,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1166
1167
}
1167
1168
smart_str_free (& eval_buf );
1168
1169
1170
+ // Need to investigate if failure in Oniguruma and if should throw.
1169
1171
if (err <= -2 ) {
1170
1172
smart_str_free (& out_buf );
1171
1173
RETVAL_FALSE ;
@@ -1226,11 +1228,13 @@ PHP_FUNCTION(mb_split)
1226
1228
}
1227
1229
1228
1230
if (!php_mb_check_encoding (string , string_len , php_mb_regex_get_mbctype_encoding ())) {
1229
- RETURN_FALSE ;
1231
+ zend_argument_value_error (2 , "must be a valid string in '%s'" , php_mb_regex_get_mbctype ());
1232
+ RETURN_THROWS ();
1230
1233
}
1231
1234
1232
1235
/* create regex pattern buffer */
1233
1236
if ((re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , MBREX (regex_default_options ), MBREX (regex_default_syntax ))) == NULL ) {
1237
+ // TODO throw as invalid regex?
1234
1238
RETURN_FALSE ;
1235
1239
}
1236
1240
@@ -1267,6 +1271,7 @@ PHP_FUNCTION(mb_split)
1267
1271
onig_region_free (regs , 1 );
1268
1272
1269
1273
/* see if we encountered an error */
1274
+ // ToDo investigate if this can actually/should happen ...
1270
1275
if (err <= -2 ) {
1271
1276
OnigUChar err_str [ONIG_MAX_ERROR_MESSAGE_LEN ];
1272
1277
onig_error_code_to_str (err_str , err );
@@ -1312,18 +1317,22 @@ PHP_FUNCTION(mb_ereg_match)
1312
1317
}
1313
1318
1314
1319
if (option_str != NULL ) {
1315
- _php_mb_regex_init_options (option_str , option_str_len , & option , & syntax , NULL );
1320
+ if (!_php_mb_regex_init_options (option_str , option_str_len , & option , & syntax , 3 )) {
1321
+ RETURN_THROWS ();
1322
+ }
1316
1323
} else {
1317
1324
option |= MBREX (regex_default_options );
1318
1325
syntax = MBREX (regex_default_syntax );
1319
1326
}
1320
1327
}
1321
1328
1322
1329
if (!php_mb_check_encoding (string , string_len , php_mb_regex_get_mbctype_encoding ())) {
1323
- RETURN_FALSE ;
1330
+ zend_argument_value_error (2 , "must be a valid string in '%s'" , php_mb_regex_get_mbctype ());
1331
+ RETURN_THROWS ();
1324
1332
}
1325
1333
1326
1334
if ((re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , option , syntax )) == NULL ) {
1335
+ // TODO throw as invalid regex?
1327
1336
RETURN_FALSE ;
1328
1337
}
1329
1338
@@ -1369,7 +1378,9 @@ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
1369
1378
1370
1379
if (arg_options ) {
1371
1380
option = 0 ;
1372
- _php_mb_regex_init_options (arg_options , arg_options_len , & option , & syntax , NULL );
1381
+ if (!_php_mb_regex_init_options (arg_options , arg_options_len , & option , & syntax , 2 )) {
1382
+ RETURN_THROWS ();
1383
+ }
1373
1384
}
1374
1385
1375
1386
if (MBREX (search_regs )) {
@@ -1380,6 +1391,7 @@ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
1380
1391
if (arg_pattern ) {
1381
1392
/* create regex pattern buffer */
1382
1393
if ((MBREX (search_re ) = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , option , MBREX (regex_default_syntax ))) == NULL ) {
1394
+ // TODO throw as invalid regex?
1383
1395
RETURN_FALSE ;
1384
1396
}
1385
1397
}
@@ -1393,13 +1405,13 @@ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
1393
1405
}
1394
1406
1395
1407
if (MBREX (search_re ) == NULL ) {
1396
- php_error_docref (NULL , E_WARNING , "No regex given " );
1397
- RETURN_FALSE ;
1408
+ zend_throw_error (NULL , "No pattern was provided " );
1409
+ RETURN_THROWS () ;
1398
1410
}
1399
1411
1400
1412
if (str == NULL ) {
1401
- php_error_docref (NULL , E_WARNING , "No string given " );
1402
- RETURN_FALSE ;
1413
+ zend_throw_error (NULL , "No string was provided " );
1414
+ RETURN_THROWS () ;
1403
1415
}
1404
1416
1405
1417
MBREX (search_regs ) = onig_region_new ();
@@ -1502,21 +1514,24 @@ PHP_FUNCTION(mb_ereg_search_init)
1502
1514
}
1503
1515
1504
1516
if (ZEND_NUM_ARGS () > 1 && arg_pattern_len == 0 ) {
1505
- php_error_docref ( NULL , E_WARNING , "Empty pattern " );
1506
- RETURN_FALSE ;
1517
+ zend_argument_value_error ( 2 , "must not be empty " );
1518
+ RETURN_THROWS () ;
1507
1519
}
1508
1520
1509
1521
option = MBREX (regex_default_options );
1510
1522
syntax = MBREX (regex_default_syntax );
1511
1523
1512
1524
if (ZEND_NUM_ARGS () == 3 ) {
1513
1525
option = 0 ;
1514
- _php_mb_regex_init_options (arg_options , arg_options_len , & option , & syntax , NULL );
1526
+ if (!_php_mb_regex_init_options (arg_options , arg_options_len , & option , & syntax , 3 )) {
1527
+ RETURN_THROWS ();
1528
+ }
1515
1529
}
1516
1530
1517
1531
if (ZEND_NUM_ARGS () > 1 ) {
1518
1532
/* create regex pattern buffer */
1519
1533
if ((MBREX (search_re ) = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , option , syntax )) == NULL ) {
1534
+ // TODO throw as invalid regex?
1520
1535
RETURN_FALSE ;
1521
1536
}
1522
1537
}
@@ -1527,18 +1542,15 @@ PHP_FUNCTION(mb_ereg_search_init)
1527
1542
1528
1543
ZVAL_STR_COPY (& MBREX (search_str ), arg_str );
1529
1544
1530
- if (php_mb_check_encoding (
1531
- ZSTR_VAL (arg_str ),
1532
- ZSTR_LEN (arg_str ),
1533
- php_mb_regex_get_mbctype_encoding ()
1534
- )) {
1535
- MBREX (search_pos ) = 0 ;
1536
- RETVAL_TRUE ;
1537
- } else {
1545
+ if (!php_mb_check_encoding (ZSTR_VAL (arg_str ), ZSTR_LEN (arg_str ), php_mb_regex_get_mbctype_encoding ())) {
1538
1546
MBREX (search_pos ) = ZSTR_LEN (arg_str );
1539
- RETVAL_FALSE ;
1547
+ zend_argument_value_error (1 , "must be a valid string in '%s'" , php_mb_regex_get_mbctype ());
1548
+ RETURN_THROWS ();
1540
1549
}
1541
1550
1551
+ MBREX (search_pos ) = 0 ;
1552
+ RETVAL_TRUE ;
1553
+
1542
1554
if (MBREX (search_regs ) != NULL ) {
1543
1555
onig_region_free (MBREX (search_regs ), 1 );
1544
1556
MBREX (search_regs ) = NULL ;
@@ -1584,6 +1596,7 @@ PHP_FUNCTION(mb_ereg_search_getregs)
1584
1596
onig_foreach_name (MBREX (search_re ), mb_regex_groups_iter , & args );
1585
1597
}
1586
1598
} else {
1599
+ // TODO This seems to be some logical error, promote to Error
1587
1600
RETVAL_FALSE ;
1588
1601
}
1589
1602
}
@@ -1617,12 +1630,12 @@ PHP_FUNCTION(mb_ereg_search_setpos)
1617
1630
}
1618
1631
1619
1632
if (position < 0 || (!Z_ISUNDEF (MBREX (search_str )) && Z_TYPE (MBREX (search_str )) == IS_STRING && (size_t )position > Z_STRLEN (MBREX (search_str )))) {
1620
- php_error_docref (NULL , E_WARNING , "Position is out of range" );
1621
- MBREX (search_pos ) = 0 ;
1622
- RETURN_FALSE ;
1633
+ zend_argument_value_error (1 , "is out of range" );
1634
+ RETURN_THROWS ();
1623
1635
}
1624
1636
1625
1637
MBREX (search_pos ) = position ;
1638
+ // TODO Return void
1626
1639
RETURN_TRUE ;
1627
1640
}
1628
1641
/* }}} */
@@ -1658,7 +1671,9 @@ PHP_FUNCTION(mb_regex_set_options)
1658
1671
if (string != NULL ) {
1659
1672
opt = 0 ;
1660
1673
syntax = NULL ;
1661
- _php_mb_regex_init_options (string , string_len , & opt , & syntax , NULL );
1674
+ if (!_php_mb_regex_init_options (string , string_len , & opt , & syntax , 1 )) {
1675
+ RETURN_THROWS ();
1676
+ }
1662
1677
_php_mb_regex_set_options (opt , syntax , & prev_opt , & prev_syntax );
1663
1678
opt = prev_opt ;
1664
1679
syntax = prev_syntax ;
0 commit comments