26
26
#include "php_mbregex.h"
27
27
#include "mbstring.h"
28
28
#include "libmbfl/filters/mbfilter_utf8.h"
29
+ #include <stdbool.h>
29
30
30
31
#include "php_onig_compat.h" /* must come prior to the oniguruma header */
31
32
#include <oniguruma.h>
@@ -591,8 +592,8 @@ static size_t _php_mb_regex_get_option_string(char *str, size_t len, OnigOptionT
591
592
/* }}} */
592
593
593
594
/* {{{ _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 )
596
597
{
597
598
size_t n ;
598
599
char c ;
@@ -651,14 +652,16 @@ _php_mb_regex_init_options(const char *parg, size_t narg, OnigOptionType *option
651
652
* syntax = ONIG_SYNTAX_POSIX_EXTENDED ;
652
653
break ;
653
654
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 ;
656
657
default :
658
+ // TODO Unsupported ValueError
657
659
break ;
658
660
}
659
661
}
660
662
if (option != NULL ) * option |=optm ;
661
663
}
664
+ return true;
662
665
}
663
666
/* }}} */
664
667
@@ -900,6 +903,11 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
900
903
RETURN_THROWS ();
901
904
}
902
905
906
+ if (arg_pattern_len == 0 ) {
907
+ zend_argument_value_error (1 , "must not be empty" );
908
+ RETURN_THROWS ();
909
+ }
910
+
903
911
if (array != NULL ) {
904
912
array = zend_try_array_init (array );
905
913
if (!array ) {
@@ -912,20 +920,15 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
912
920
string_len ,
913
921
php_mb_regex_get_mbctype_encoding ()
914
922
)) {
915
- RETURN_FALSE ;
923
+ zend_argument_value_error (2 , "must be a valid string in '%s'" , php_mb_regex_get_mbctype ());
924
+ RETURN_THROWS ();
916
925
}
917
926
918
927
options = MBREX (regex_default_options );
919
928
if (icase ) {
920
929
options |= ONIG_OPTION_IGNORECASE ;
921
930
}
922
931
923
- if (arg_pattern_len == 0 ) {
924
- php_error_docref (NULL , E_WARNING , "Empty pattern" );
925
- RETVAL_FALSE ;
926
- goto out ;
927
- }
928
-
929
932
re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , options , MBREX (regex_default_syntax ));
930
933
if (re == NULL ) {
931
934
RETVAL_FALSE ;
@@ -1007,15 +1010,14 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1007
1010
smart_str out_buf = {0 };
1008
1011
smart_str eval_buf = {0 };
1009
1012
smart_str * pbuf ;
1010
- int err , eval , n ;
1013
+ int err , n ;
1011
1014
OnigUChar * pos ;
1012
1015
OnigUChar * string_lim ;
1013
1016
char * description = NULL ;
1014
1017
1015
1018
const mbfl_encoding * enc = php_mb_regex_get_mbctype_encoding ();
1016
1019
ZEND_ASSERT (enc != NULL );
1017
1020
1018
- eval = 0 ;
1019
1021
{
1020
1022
char * option_str = NULL ;
1021
1023
size_t option_str_len = 0 ;
@@ -1039,28 +1041,25 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1039
1041
}
1040
1042
1041
1043
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 ();
1043
1046
}
1044
1047
1045
1048
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
+ }
1047
1053
} else {
1048
1054
options |= MBREX (regex_default_options );
1049
1055
syntax = MBREX (regex_default_syntax );
1050
1056
}
1051
1057
}
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
- }
1060
1058
1061
1059
/* create regex pattern buffer */
1062
1060
re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , options , syntax );
1063
1061
if (re == NULL ) {
1062
+ // Should this be considered an error instead?
1064
1063
RETURN_FALSE ;
1065
1064
}
1066
1065
@@ -1122,7 +1121,9 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1122
1121
zval_ptr_dtor (& retval );
1123
1122
} else {
1124
1123
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 ();
1126
1127
}
1127
1128
}
1128
1129
zval_ptr_dtor (& subpats );
@@ -1154,6 +1155,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1154
1155
}
1155
1156
smart_str_free (& eval_buf );
1156
1157
1158
+ // Need to investigate if failure in Oniguruma and if should throw.
1157
1159
if (err <= -2 ) {
1158
1160
smart_str_free (& out_buf );
1159
1161
RETVAL_FALSE ;
@@ -1210,11 +1212,13 @@ PHP_FUNCTION(mb_split)
1210
1212
}
1211
1213
1212
1214
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 ();
1214
1217
}
1215
1218
1216
1219
/* create regex pattern buffer */
1217
1220
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?
1218
1222
RETURN_FALSE ;
1219
1223
}
1220
1224
@@ -1251,6 +1255,7 @@ PHP_FUNCTION(mb_split)
1251
1255
onig_region_free (regs , 1 );
1252
1256
1253
1257
/* see if we encountered an error */
1258
+ // ToDo investigate if this can actually/should happen ...
1254
1259
if (err <= -2 ) {
1255
1260
OnigUChar err_str [ONIG_MAX_ERROR_MESSAGE_LEN ];
1256
1261
onig_error_code_to_str (err_str , err );
@@ -1295,18 +1300,22 @@ PHP_FUNCTION(mb_ereg_match)
1295
1300
}
1296
1301
1297
1302
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
+ }
1299
1306
} else {
1300
1307
option |= MBREX (regex_default_options );
1301
1308
syntax = MBREX (regex_default_syntax );
1302
1309
}
1303
1310
}
1304
1311
1305
1312
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 ();
1307
1315
}
1308
1316
1309
1317
if ((re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , option , syntax )) == NULL ) {
1318
+ // TODO throw as invalid regex?
1310
1319
RETURN_FALSE ;
1311
1320
}
1312
1321
@@ -1362,6 +1371,7 @@ static void _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mod
1362
1371
if (arg_pattern ) {
1363
1372
/* create regex pattern buffer */
1364
1373
if ((MBREX (search_re ) = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , option , syntax )) == NULL ) {
1374
+ // TODO throw as invalid regex?
1365
1375
RETURN_FALSE ;
1366
1376
}
1367
1377
}
@@ -1375,13 +1385,13 @@ static void _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mod
1375
1385
}
1376
1386
1377
1387
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 () ;
1380
1390
}
1381
1391
1382
1392
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 () ;
1385
1395
}
1386
1396
1387
1397
MBREX (search_regs ) = onig_region_new ();
@@ -1480,8 +1490,8 @@ PHP_FUNCTION(mb_ereg_search_init)
1480
1490
}
1481
1491
1482
1492
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 () ;
1485
1495
}
1486
1496
1487
1497
if (arg_options ) {
@@ -1495,6 +1505,7 @@ PHP_FUNCTION(mb_ereg_search_init)
1495
1505
if (arg_pattern ) {
1496
1506
/* create regex pattern buffer */
1497
1507
if ((MBREX (search_re ) = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , option , syntax )) == NULL ) {
1508
+ // TODO throw as invalid regex?
1498
1509
RETURN_FALSE ;
1499
1510
}
1500
1511
}
@@ -1510,9 +1521,13 @@ PHP_FUNCTION(mb_ereg_search_init)
1510
1521
RETVAL_TRUE ;
1511
1522
} else {
1512
1523
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 ();
1514
1526
}
1515
1527
1528
+ MBREX (search_pos ) = 0 ;
1529
+ RETVAL_TRUE ;
1530
+
1516
1531
if (MBREX (search_regs ) != NULL ) {
1517
1532
onig_region_free (MBREX (search_regs ), 1 );
1518
1533
MBREX (search_regs ) = NULL ;
@@ -1557,6 +1572,7 @@ PHP_FUNCTION(mb_ereg_search_getregs)
1557
1572
onig_foreach_name (MBREX (search_re ), mb_regex_groups_iter , & args );
1558
1573
}
1559
1574
} else {
1575
+ // TODO This seems to be some logical error, promote to Error
1560
1576
RETVAL_FALSE ;
1561
1577
}
1562
1578
}
@@ -1588,12 +1604,12 @@ PHP_FUNCTION(mb_ereg_search_setpos)
1588
1604
}
1589
1605
1590
1606
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 ();
1594
1609
}
1595
1610
1596
1611
MBREX (search_pos ) = position ;
1612
+ // TODO Return void
1597
1613
RETURN_TRUE ;
1598
1614
}
1599
1615
/* }}} */
@@ -1628,7 +1644,9 @@ PHP_FUNCTION(mb_regex_set_options)
1628
1644
if (string != NULL ) {
1629
1645
opt = 0 ;
1630
1646
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
+ }
1632
1650
_php_mb_regex_set_options (opt , syntax , & prev_opt , & prev_syntax );
1633
1651
opt = prev_opt ;
1634
1652
syntax = prev_syntax ;
0 commit comments