@@ -1499,8 +1499,29 @@ ZEND_FRAMELESS_FUNCTION(preg_match, 2)
14991499{
15001500 zval regex_tmp , subject_tmp ;
15011501 zend_string * regex , * subject ;
1502+ zval arg1_copy , arg2_copy ;
1503+ bool arg1_protected = false, arg2_protected = false;
1504+
1505+ if (EXPECTED (Z_TYPE_P (arg1 ) == IS_STRING && Z_TYPE_P (arg2 ) == IS_STRING )) {
1506+ /* Compile regex or get it from cache. */
1507+ pcre_cache_entry * pce ;
1508+ if ((pce = pcre_get_compiled_regex_cache (Z_STR_P (arg1 ))) == NULL ) {
1509+ RETVAL_FALSE ;
1510+ return ;
1511+ }
1512+
1513+ pce -> refcount ++ ;
1514+ php_pcre_match_impl (pce , Z_STR_P (arg2 ), return_value , /* subpats */ NULL ,
1515+ /* global */ false, /* flags */ 0 , /* start_offset */ 0 );
1516+ pce -> refcount -- ;
1517+ return ;
1518+ }
15021519
1520+ Z_FLF_PROTECT_LATER_ARG_FOR_STR_PARSE (1 , 2 , arg2_copy , arg2_protected );
15031521 Z_FLF_PARAM_STR (1 , regex , regex_tmp );
1522+ if (arg1 != & regex_tmp ) {
1523+ Z_FLF_PROTECT_LATER_ARG_FOR_STR_PARSE (2 , 1 , arg1_copy , arg1_protected );
1524+ }
15041525 Z_FLF_PARAM_STR (2 , subject , subject_tmp );
15051526
15061527 /* Compile regex or get it from cache. */
@@ -1518,6 +1539,8 @@ ZEND_FRAMELESS_FUNCTION(preg_match, 2)
15181539flf_clean :
15191540 Z_FLF_PARAM_FREE_STR (1 , regex_tmp );
15201541 Z_FLF_PARAM_FREE_STR (2 , subject_tmp );
1542+ Z_FLF_FREE_PROTECTED_ARG (arg2_copy , arg2_protected );
1543+ Z_FLF_FREE_PROTECTED_ARG (arg1_copy , arg1_protected );
15211544}
15221545
15231546/* {{{ Perform a Perl-style global regular expression match */
@@ -2404,9 +2427,33 @@ ZEND_FRAMELESS_FUNCTION(preg_replace, 3)
24042427 zend_string * regex_str , * replace_str , * subject_str ;
24052428 HashTable * regex_ht , * replace_ht , * subject_ht ;
24062429 zval regex_tmp , replace_tmp , subject_tmp ;
2430+ zval arg1_copy , arg2_copy , arg3_copy ;
2431+ bool arg1_protected = false, arg2_protected = false, arg3_protected = false;
2432+
2433+ if (EXPECTED (Z_TYPE_P (arg1 ) == IS_STRING && Z_TYPE_P (arg2 ) == IS_STRING && Z_TYPE_P (arg3 ) == IS_STRING )) {
2434+ _preg_replace_common (
2435+ return_value ,
2436+ /* regex_ht */ NULL , Z_STR_P (arg1 ),
2437+ /* replace_ht */ NULL , Z_STR_P (arg2 ),
2438+ /* subject_ht */ NULL , Z_STR_P (arg3 ),
2439+ /* limit */ -1 , /* zcount */ NULL , /* is_filter */ false);
2440+ return ;
2441+ }
24072442
2443+ Z_FLF_PROTECT_LATER_ARG_FOR_STR_PARSE (1 , 2 , arg2_copy , arg2_protected );
2444+ Z_FLF_PROTECT_LATER_ARG_FOR_STR_PARSE (1 , 3 , arg3_copy , arg3_protected );
24082445 Z_FLF_PARAM_ARRAY_HT_OR_STR (1 , regex_ht , regex_str , regex_tmp );
2446+ if (arg1 != & regex_tmp ) {
2447+ Z_FLF_PROTECT_LATER_ARG_FOR_STR_PARSE (2 , 1 , arg1_copy , arg1_protected );
2448+ }
2449+ Z_FLF_PROTECT_LATER_ARG_FOR_STR_PARSE (2 , 3 , arg3_copy , arg3_protected );
24092450 Z_FLF_PARAM_ARRAY_HT_OR_STR (2 , replace_ht , replace_str , replace_tmp );
2451+ if (arg1 != & regex_tmp ) {
2452+ Z_FLF_PROTECT_LATER_ARG_FOR_STR_PARSE (3 , 1 , arg1_copy , arg1_protected );
2453+ }
2454+ if (arg2 != & replace_tmp ) {
2455+ Z_FLF_PROTECT_LATER_ARG_FOR_STR_PARSE (3 , 2 , arg2_copy , arg2_protected );
2456+ }
24102457 Z_FLF_PARAM_ARRAY_HT_OR_STR (3 , subject_ht , subject_str , subject_tmp );
24112458
24122459 _preg_replace_common (
@@ -2420,6 +2467,9 @@ flf_clean:;
24202467 Z_FLF_PARAM_FREE_STR (1 , regex_tmp );
24212468 Z_FLF_PARAM_FREE_STR (2 , replace_tmp );
24222469 Z_FLF_PARAM_FREE_STR (3 , subject_tmp );
2470+ Z_FLF_FREE_PROTECTED_ARG (arg3_copy , arg3_protected );
2471+ Z_FLF_FREE_PROTECTED_ARG (arg2_copy , arg2_protected );
2472+ Z_FLF_FREE_PROTECTED_ARG (arg1_copy , arg1_protected );
24232473}
24242474
24252475/* {{{ Perform Perl-style regular expression replacement using replacement callback. */
0 commit comments