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
@@ -908,6 +911,11 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
908
911
RETURN_THROWS ();
909
912
}
910
913
914
+ if (arg_pattern_len == 0 ) {
915
+ zend_argument_value_error (1 , "must not be empty" );
916
+ RETURN_THROWS ();
917
+ }
918
+
911
919
if (array != NULL ) {
912
920
array = zend_try_array_init (array );
913
921
if (!array ) {
@@ -920,20 +928,15 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
920
928
string_len ,
921
929
php_mb_regex_get_mbctype_encoding ()
922
930
)) {
923
- RETURN_FALSE ;
931
+ zend_argument_value_error (2 , "must be a valid string in '%s'" , php_mb_regex_get_mbctype ());
932
+ RETURN_THROWS ();
924
933
}
925
934
926
935
options = MBREX (regex_default_options );
927
936
if (icase ) {
928
937
options |= ONIG_OPTION_IGNORECASE ;
929
938
}
930
939
931
- if (arg_pattern_len == 0 ) {
932
- php_error_docref (NULL , E_WARNING , "Empty pattern" );
933
- RETVAL_FALSE ;
934
- goto out ;
935
- }
936
-
937
940
re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , options , MBREX (regex_default_syntax ));
938
941
if (re == NULL ) {
939
942
RETVAL_FALSE ;
@@ -1017,15 +1020,14 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1017
1020
smart_str out_buf = {0 };
1018
1021
smart_str eval_buf = {0 };
1019
1022
smart_str * pbuf ;
1020
- int err , eval , n ;
1023
+ int err , n ;
1021
1024
OnigUChar * pos ;
1022
1025
OnigUChar * string_lim ;
1023
1026
char * description = NULL ;
1024
1027
1025
1028
const mbfl_encoding * enc = php_mb_regex_get_mbctype_encoding ();
1026
1029
ZEND_ASSERT (enc != NULL );
1027
1030
1028
- eval = 0 ;
1029
1031
{
1030
1032
char * option_str = NULL ;
1031
1033
size_t option_str_len = 0 ;
@@ -1049,28 +1051,25 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1049
1051
}
1050
1052
1051
1053
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 ();
1053
1056
}
1054
1057
1055
1058
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
+ }
1057
1063
} else {
1058
1064
options |= MBREX (regex_default_options );
1059
1065
syntax = MBREX (regex_default_syntax );
1060
1066
}
1061
1067
}
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
- }
1070
1068
1071
1069
/* create regex pattern buffer */
1072
1070
re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , options , syntax );
1073
1071
if (re == NULL ) {
1072
+ // Should this be considered an error instead?
1074
1073
RETURN_FALSE ;
1075
1074
}
1076
1075
@@ -1132,7 +1131,9 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1132
1131
zval_ptr_dtor (& retval );
1133
1132
} else {
1134
1133
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 ();
1136
1137
}
1137
1138
}
1138
1139
zval_ptr_dtor (& subpats );
@@ -1164,6 +1165,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
1164
1165
}
1165
1166
smart_str_free (& eval_buf );
1166
1167
1168
+ // Need to investigate if failure in Oniguruma and if should throw.
1167
1169
if (err <= -2 ) {
1168
1170
smart_str_free (& out_buf );
1169
1171
RETVAL_FALSE ;
@@ -1224,11 +1226,13 @@ PHP_FUNCTION(mb_split)
1224
1226
}
1225
1227
1226
1228
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 ();
1228
1231
}
1229
1232
1230
1233
/* create regex pattern buffer */
1231
1234
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?
1232
1236
RETURN_FALSE ;
1233
1237
}
1234
1238
@@ -1265,6 +1269,7 @@ PHP_FUNCTION(mb_split)
1265
1269
onig_region_free (regs , 1 );
1266
1270
1267
1271
/* see if we encountered an error */
1272
+ // ToDo investigate if this can actually/should happen ...
1268
1273
if (err <= -2 ) {
1269
1274
OnigUChar err_str [ONIG_MAX_ERROR_MESSAGE_LEN ];
1270
1275
onig_error_code_to_str (err_str , err );
@@ -1310,18 +1315,22 @@ PHP_FUNCTION(mb_ereg_match)
1310
1315
}
1311
1316
1312
1317
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
+ }
1314
1321
} else {
1315
1322
option |= MBREX (regex_default_options );
1316
1323
syntax = MBREX (regex_default_syntax );
1317
1324
}
1318
1325
}
1319
1326
1320
1327
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 ();
1322
1330
}
1323
1331
1324
1332
if ((re = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , option , syntax )) == NULL ) {
1333
+ // TODO throw as invalid regex?
1325
1334
RETURN_FALSE ;
1326
1335
}
1327
1336
@@ -1367,7 +1376,9 @@ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
1367
1376
1368
1377
if (arg_options ) {
1369
1378
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
+ }
1371
1382
}
1372
1383
1373
1384
if (MBREX (search_regs )) {
@@ -1378,6 +1389,7 @@ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
1378
1389
if (arg_pattern ) {
1379
1390
/* create regex pattern buffer */
1380
1391
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?
1381
1393
RETURN_FALSE ;
1382
1394
}
1383
1395
}
@@ -1391,13 +1403,13 @@ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
1391
1403
}
1392
1404
1393
1405
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 () ;
1396
1408
}
1397
1409
1398
1410
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 () ;
1401
1413
}
1402
1414
1403
1415
MBREX (search_regs ) = onig_region_new ();
@@ -1500,21 +1512,24 @@ PHP_FUNCTION(mb_ereg_search_init)
1500
1512
}
1501
1513
1502
1514
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 () ;
1505
1517
}
1506
1518
1507
1519
option = MBREX (regex_default_options );
1508
1520
syntax = MBREX (regex_default_syntax );
1509
1521
1510
1522
if (ZEND_NUM_ARGS () == 3 ) {
1511
1523
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
+ }
1513
1527
}
1514
1528
1515
1529
if (ZEND_NUM_ARGS () > 1 ) {
1516
1530
/* create regex pattern buffer */
1517
1531
if ((MBREX (search_re ) = php_mbregex_compile_pattern (arg_pattern , arg_pattern_len , option , syntax )) == NULL ) {
1532
+ // TODO throw as invalid regex?
1518
1533
RETURN_FALSE ;
1519
1534
}
1520
1535
}
@@ -1525,18 +1540,15 @@ PHP_FUNCTION(mb_ereg_search_init)
1525
1540
1526
1541
ZVAL_STR_COPY (& MBREX (search_str ), arg_str );
1527
1542
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 ())) {
1536
1544
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 ();
1538
1547
}
1539
1548
1549
+ MBREX (search_pos ) = 0 ;
1550
+ RETVAL_TRUE ;
1551
+
1540
1552
if (MBREX (search_regs ) != NULL ) {
1541
1553
onig_region_free (MBREX (search_regs ), 1 );
1542
1554
MBREX (search_regs ) = NULL ;
@@ -1582,6 +1594,7 @@ PHP_FUNCTION(mb_ereg_search_getregs)
1582
1594
onig_foreach_name (MBREX (search_re ), mb_regex_groups_iter , & args );
1583
1595
}
1584
1596
} else {
1597
+ // TODO This seems to be some logical error, promote to Error
1585
1598
RETVAL_FALSE ;
1586
1599
}
1587
1600
}
@@ -1615,12 +1628,12 @@ PHP_FUNCTION(mb_ereg_search_setpos)
1615
1628
}
1616
1629
1617
1630
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 ();
1621
1633
}
1622
1634
1623
1635
MBREX (search_pos ) = position ;
1636
+ // TODO Return void
1624
1637
RETURN_TRUE ;
1625
1638
}
1626
1639
/* }}} */
@@ -1656,7 +1669,9 @@ PHP_FUNCTION(mb_regex_set_options)
1656
1669
if (string != NULL ) {
1657
1670
opt = 0 ;
1658
1671
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
+ }
1660
1675
_php_mb_regex_set_options (opt , syntax , & prev_opt , & prev_syntax );
1661
1676
opt = prev_opt ;
1662
1677
syntax = prev_syntax ;
0 commit comments