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
@@ -909,6 +912,11 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
909
912
RETURN_THROWS ();
910
913
}
911
914
915
+ if (arg_pattern_len == 0 ) {
916
+ zend_argument_value_error (1 , "must not be empty" );
917
+ RETURN_THROWS ();
918
+ }
919
+
912
920
if (array != NULL ) {
913
921
array = zend_try_array_init (array );
914
922
if (!array ) {
@@ -921,20 +929,15 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
921
929
string_len ,
922
930
php_mb_regex_get_mbctype_encoding ()
923
931
)) {
924
- RETURN_FALSE ;
932
+ zend_argument_value_error (2 , "must be a valid string in '%s'" , php_mb_regex_get_mbctype ());
933
+ RETURN_THROWS ();
925
934
}
926
935
927
936
options = MBREX (regex_default_options );
928
937
if (icase ) {
929
938
options |= ONIG_OPTION_IGNORECASE ;
930
939
}
931
940
932
- if (arg_pattern_len == 0 ) {
933
- php_error_docref (NULL , E_WARNING , "Empty pattern" );
934
- RETVAL_FALSE ;
935
- goto out ;
936
- }
937
-
938
941
re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , options , MBREX (regex_default_syntax ));
939
942
if (re == NULL ) {
940
943
RETVAL_FALSE ;
@@ -1016,15 +1019,14 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1016
1019
smart_str out_buf = {0 };
1017
1020
smart_str eval_buf = {0 };
1018
1021
smart_str * pbuf ;
1019
- int err , eval , n ;
1022
+ int err , n ;
1020
1023
OnigUChar * pos ;
1021
1024
OnigUChar * string_lim ;
1022
1025
char * description = NULL ;
1023
1026
1024
1027
const mbfl_encoding * enc = php_mb_regex_get_mbctype_encoding ();
1025
1028
ZEND_ASSERT (enc != NULL );
1026
1029
1027
- eval = 0 ;
1028
1030
{
1029
1031
char * option_str = NULL ;
1030
1032
size_t option_str_len = 0 ;
@@ -1048,28 +1050,25 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1048
1050
}
1049
1051
1050
1052
if (!php_mb_check_encoding (string , string_len , enc )) {
1051
- RETURN_NULL ();
1053
+ zend_argument_value_error (3 , "must be a valid string in '%s'" , php_mb_regex_get_mbctype ());
1054
+ RETURN_THROWS ();
1052
1055
}
1053
1056
1054
1057
if (option_str != NULL ) {
1055
- _php_mb_regex_init_options (option_str , option_str_len , & options , & syntax , & eval );
1058
+ /* Initialize option and in case of failure it means there is a value error */
1059
+ if (!_php_mb_regex_init_options (option_str , option_str_len , & options , & syntax , 4 )) {
1060
+ RETURN_THROWS ();
1061
+ }
1056
1062
} else {
1057
1063
options |= MBREX (regex_default_options );
1058
1064
syntax = MBREX (regex_default_syntax );
1059
1065
}
1060
1066
}
1061
- if (eval ) {
1062
- if (is_callable ) {
1063
- php_error_docref (NULL , E_WARNING , "Option 'e' cannot be used with replacement callback" );
1064
- } else {
1065
- php_error_docref (NULL , E_WARNING , "The 'e' option is no longer supported, use mb_ereg_replace_callback instead" );
1066
- }
1067
- RETURN_FALSE ;
1068
- }
1069
1067
1070
1068
/* create regex pattern buffer */
1071
1069
re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , options , syntax );
1072
1070
if (re == NULL ) {
1071
+ // Should this be considered an error instead?
1073
1072
RETURN_FALSE ;
1074
1073
}
1075
1074
@@ -1131,7 +1130,9 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1131
1130
zval_ptr_dtor (& retval );
1132
1131
} else {
1133
1132
if (!EG (exception )) {
1134
- php_error_docref (NULL , E_WARNING , "Unable to call custom replacement function" );
1133
+ zend_throw_error (NULL , "Unable to call custom replacement function" );
1134
+ zval_ptr_dtor (& subpats );
1135
+ RETURN_THROWS ();
1135
1136
}
1136
1137
}
1137
1138
zval_ptr_dtor (& subpats );
@@ -1163,6 +1164,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1163
1164
}
1164
1165
smart_str_free (& eval_buf );
1165
1166
1167
+ // Need to investigate if failure in Oniguruma and if should throw.
1166
1168
if (err <= -2 ) {
1167
1169
smart_str_free (& out_buf );
1168
1170
RETVAL_FALSE ;
@@ -1219,11 +1221,13 @@ PHP_FUNCTION(mb_split)
1219
1221
}
1220
1222
1221
1223
if (!php_mb_check_encoding (string , string_len , php_mb_regex_get_mbctype_encoding ())) {
1222
- RETURN_FALSE ;
1224
+ zend_argument_value_error (2 , "must be a valid string in '%s'" , php_mb_regex_get_mbctype ());
1225
+ RETURN_THROWS ();
1223
1226
}
1224
1227
1225
1228
/* create regex pattern buffer */
1226
1229
if ((re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , MBREX (regex_default_options ), MBREX (regex_default_syntax ))) == NULL ) {
1230
+ // TODO throw as invalid regex?
1227
1231
RETURN_FALSE ;
1228
1232
}
1229
1233
@@ -1260,6 +1264,7 @@ PHP_FUNCTION(mb_split)
1260
1264
onig_region_free (regs , 1 );
1261
1265
1262
1266
/* see if we encountered an error */
1267
+ // ToDo investigate if this can actually/should happen ...
1263
1268
if (err <= -2 ) {
1264
1269
OnigUChar err_str [ONIG_MAX_ERROR_MESSAGE_LEN ];
1265
1270
onig_error_code_to_str (err_str , err );
@@ -1304,18 +1309,22 @@ PHP_FUNCTION(mb_ereg_match)
1304
1309
}
1305
1310
1306
1311
if (option_str != NULL ) {
1307
- _php_mb_regex_init_options (option_str , option_str_len , & option , & syntax , NULL );
1312
+ if (!_php_mb_regex_init_options (option_str , option_str_len , & option , & syntax , 3 )) {
1313
+ RETURN_THROWS ();
1314
+ }
1308
1315
} else {
1309
1316
option |= MBREX (regex_default_options );
1310
1317
syntax = MBREX (regex_default_syntax );
1311
1318
}
1312
1319
}
1313
1320
1314
1321
if (!php_mb_check_encoding (string , string_len , php_mb_regex_get_mbctype_encoding ())) {
1315
- RETURN_FALSE ;
1322
+ zend_argument_value_error (2 , "must be a valid string in '%s'" , php_mb_regex_get_mbctype ());
1323
+ RETURN_THROWS ();
1316
1324
}
1317
1325
1318
1326
if ((re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , option , syntax )) == NULL ) {
1327
+ // TODO throw as invalid regex?
1319
1328
RETURN_FALSE ;
1320
1329
}
1321
1330
@@ -1371,6 +1380,7 @@ static void _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mod
1371
1380
if (arg_pattern ) {
1372
1381
/* create regex pattern buffer */
1373
1382
if ((MBREX (search_re ) = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , option , syntax )) == NULL ) {
1383
+ // TODO throw as invalid regex?
1374
1384
RETURN_FALSE ;
1375
1385
}
1376
1386
}
@@ -1384,13 +1394,13 @@ static void _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mod
1384
1394
}
1385
1395
1386
1396
if (MBREX (search_re ) == NULL ) {
1387
- php_error_docref (NULL , E_WARNING , "No regex given " );
1388
- RETURN_FALSE ;
1397
+ zend_throw_error (NULL , "No pattern was provided " );
1398
+ RETURN_THROWS () ;
1389
1399
}
1390
1400
1391
1401
if (str == NULL ) {
1392
- php_error_docref (NULL , E_WARNING , "No string given " );
1393
- RETURN_FALSE ;
1402
+ zend_throw_error (NULL , "No string was provided " );
1403
+ RETURN_THROWS () ;
1394
1404
}
1395
1405
1396
1406
MBREX (search_regs ) = onig_region_new ();
@@ -1489,8 +1499,8 @@ PHP_FUNCTION(mb_ereg_search_init)
1489
1499
}
1490
1500
1491
1501
if (arg_pattern && arg_pattern_len == 0 ) {
1492
- php_error_docref ( NULL , E_WARNING , "Empty pattern " );
1493
- RETURN_FALSE ;
1502
+ zend_argument_value_error ( 2 , "must not be empty " );
1503
+ RETURN_THROWS () ;
1494
1504
}
1495
1505
1496
1506
if (arg_options ) {
@@ -1504,6 +1514,7 @@ PHP_FUNCTION(mb_ereg_search_init)
1504
1514
if (arg_pattern ) {
1505
1515
/* create regex pattern buffer */
1506
1516
if ((MBREX (search_re ) = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , option , syntax )) == NULL ) {
1517
+ // TODO throw as invalid regex?
1507
1518
RETURN_FALSE ;
1508
1519
}
1509
1520
}
@@ -1519,9 +1530,13 @@ PHP_FUNCTION(mb_ereg_search_init)
1519
1530
RETVAL_TRUE ;
1520
1531
} else {
1521
1532
MBREX (search_pos ) = ZSTR_LEN (arg_str );
1522
- RETVAL_FALSE ;
1533
+ zend_argument_value_error (1 , "must be a valid string in '%s'" , php_mb_regex_get_mbctype ());
1534
+ RETURN_THROWS ();
1523
1535
}
1524
1536
1537
+ MBREX (search_pos ) = 0 ;
1538
+ RETVAL_TRUE ;
1539
+
1525
1540
if (MBREX (search_regs ) != NULL ) {
1526
1541
onig_region_free (MBREX (search_regs ), 1 );
1527
1542
MBREX (search_regs ) = NULL ;
@@ -1566,6 +1581,7 @@ PHP_FUNCTION(mb_ereg_search_getregs)
1566
1581
onig_foreach_name (MBREX (search_re ), mb_regex_groups_iter , & args );
1567
1582
}
1568
1583
} else {
1584
+ // TODO This seems to be some logical error, promote to Error
1569
1585
RETVAL_FALSE ;
1570
1586
}
1571
1587
}
@@ -1597,12 +1613,12 @@ PHP_FUNCTION(mb_ereg_search_setpos)
1597
1613
}
1598
1614
1599
1615
if (position < 0 || (!Z_ISUNDEF (MBREX (search_str )) && Z_TYPE (MBREX (search_str )) == IS_STRING && (size_t )position > Z_STRLEN (MBREX (search_str )))) {
1600
- php_error_docref (NULL , E_WARNING , "Position is out of range" );
1601
- MBREX (search_pos ) = 0 ;
1602
- RETURN_FALSE ;
1616
+ zend_argument_value_error (1 , "is out of range" );
1617
+ RETURN_THROWS ();
1603
1618
}
1604
1619
1605
1620
MBREX (search_pos ) = position ;
1621
+ // TODO Return void
1606
1622
RETURN_TRUE ;
1607
1623
}
1608
1624
/* }}} */
@@ -1637,7 +1653,9 @@ PHP_FUNCTION(mb_regex_set_options)
1637
1653
if (string != NULL ) {
1638
1654
opt = 0 ;
1639
1655
syntax = NULL ;
1640
- _php_mb_regex_init_options (string , string_len , & opt , & syntax , NULL );
1656
+ if (!_php_mb_regex_init_options (string , string_len , & opt , & syntax , 1 )) {
1657
+ RETURN_THROWS ();
1658
+ }
1641
1659
_php_mb_regex_set_options (opt , syntax , & prev_opt , & prev_syntax );
1642
1660
opt = prev_opt ;
1643
1661
syntax = prev_syntax ;
0 commit comments