]> granicus.if.org Git - php/commitdiff
More error conditions
authorXinchen Hui <laruence@php.net>
Sat, 21 Mar 2015 08:54:45 +0000 (16:54 +0800)
committerXinchen Hui <laruence@php.net>
Sat, 21 Mar 2015 08:54:45 +0000 (16:54 +0800)
ext/pcre/php_pcre.c
ext/pcre/tests/preg_replace_callback_array.phpt
ext/pcre/tests/preg_replace_callback_array2.phpt

index 57b0eb921edeab3dea493780060452a112a8e1b2..99ef02059fc1a0f37d47c9d4b355f53a45e6e01d 100644 (file)
@@ -1275,8 +1275,8 @@ static zend_string *php_replace_in_subject(zval *regex, zval *replace, zval *sub
                                *replace_value,
                                 empty_replace;
        zend_string *result;
-       zend_string     *subject_str = zval_get_string(subject);
        uint32_t replace_idx;
+       zend_string     *subject_str = zval_get_string(subject);
 
        /* FIXME: This might need to be changed to STR_EMPTY_ALLOC(). Check if this zval could be dtor()'ed somehow */
        ZVAL_EMPTY_STRING(&empty_replace);
@@ -1512,6 +1512,7 @@ static PHP_FUNCTION(preg_replace_callback_array)
        ZEND_PARSE_PARAMETERS_END();
 #endif
        
+       ZVAL_UNDEF(&zv);
        ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(pattern), num_idx, str_idx, replace) {
                if (str_idx) {
                        ZVAL_STR_COPY(&regex, str_idx);
@@ -1537,8 +1538,18 @@ static PHP_FUNCTION(preg_replace_callback_array)
                        zval_ptr_dtor(return_value);
                }
 
-               ZVAL_COPY_VALUE(return_value, &zv);
                zval_ptr_dtor(&regex);
+
+               if (Z_ISUNDEF(zv)) {
+                       RETURN_NULL();  
+               }
+
+               ZVAL_COPY_VALUE(return_value, &zv);
+
+               if (UNEXPECTED(EG(exception))) {
+                       zval_dtor(return_value);
+                       RETURN_NULL();  
+               }
        } ZEND_HASH_FOREACH_END();
 
        if (zcount) {
index 529f704db6c43f4e1f018ed3557b357c64c970e4..14a4656eb18b39328796329458f41a602700699f 100644 (file)
@@ -4,12 +4,19 @@ preg_replace_callback_array() basic functions
 <?php
 
 function f() {
-       throw new Exception();
+       throw new Exception('f');
+}
+
+function a() {
+       return __FUNCTION__;
 }
 
 try {
-var_dump(preg_replace_callback_array(array('/\w/' => 'f'), 'z'));
-} catch(Exception $e) {}
+       var_dump($c = preg_replace_callback_array(array('/\w*/' => 'f', '/\w/' => 'a'), 'z'));
+} catch(Exception $e) {
+       var_dump($e->getMessage());
+}
+var_dump($c);
 
 function g($x) {
        return "'$x[0]'";
@@ -22,6 +29,10 @@ var_dump(preg_replace_callback_array(array('~\A.~' => 'g'), array(array('xyz')))
 var_dump(preg_replace_callback_array(array('~\A.~' => create_function('$m', 'return strtolower($m[0]);')), 'ABC'));
 ?>
 --EXPECTF--
+string(1) "f"
+
+Notice: Undefined variable: c in %spreg_replace_callback_array.php on line %d
+NULL
 array(3) {
   [0]=>
   string(12) "'a' 'b3' bcd"
index c3d3acf0a8ad759a7a7ea088bbaed5c1d6f2ea9e..fca71673c99274783a81d36249cef8c15841cfad 100644 (file)
@@ -18,7 +18,10 @@ var_dump($b);
 $b = "";
 var_dump(preg_replace_callback_array(array("xx" => "s"), $a, -1, $b));
 var_dump($b);
+function f() {
+}
 
+var_dump(preg_replace_callback_array(array('/\w' => 'f'), 'z'));
 echo "Done\n";
 ?>
 --EXPECTF--    
@@ -44,8 +47,11 @@ Warning: preg_replace_callback() expects parameter 4 to be integer, array given
 NULL
 string(0) ""
 
-Warning: preg_replace_callback_array(): 's' is not a valid callback in %s on line %d
+Warning: preg_replace_callback_array(): 's' is not a valid callback in %spreg_replace_callback_array2.php on line %d
 array(0) {
 }
 string(0) ""
+
+Warning: preg_replace_callback_array(): No ending delimiter '/' found in %spreg_replace_callback_array2.php on line %d
+NULL
 Done