b74969
From 5b597a2e5b28e2d5a52fc1be13f425f08f47cb62 Mon Sep 17 00:00:00 2001
b74969
From: Stanislav Malyshev <stas@php.net>
b74969
Date: Sat, 18 Jun 2016 21:48:39 -0700
b74969
Subject: [PATCH] Fix bug #72402: _php_mb_regex_ereg_replace_exec - double free
b74969
b74969
---
b74969
 ext/mbstring/php_mbregex.c       | 65 ++++++++++++++++++++--------------------
b74969
 ext/mbstring/tests/bug72402.phpt | 17 +++++++++++
b74969
 2 files changed, 49 insertions(+), 33 deletions(-)
b74969
 create mode 100644 ext/mbstring/tests/bug72402.phpt
b74969
b74969
diff --git a/ext/mbstring/php_mbregex.c b/ext/mbstring/php_mbregex.c
b74969
index d73c848..6cdee23 100644
b74969
--- a/ext/mbstring/php_mbregex.c
b74969
+++ b/ext/mbstring/php_mbregex.c
b74969
@@ -32,7 +32,7 @@
b74969
 #include "ext/standard/info.h"
b74969
 #include "php_mbregex.h"
b74969
 #include "mbstring.h"
b74969
- 
b74969
+
b74969
 #include "php_onig_compat.h" /* must come prior to the oniguruma header */
b74969
 #include <oniguruma.h>
b74969
 #undef UChar
b74969
@@ -55,7 +55,7 @@ struct _zend_mb_regex_globals {
b74969
 #define MBREX(g) (MBSTRG(mb_regex_globals)->g)
b74969
 
b74969
 /* {{{ static void php_mb_regex_free_cache() */
b74969
-static void php_mb_regex_free_cache(php_mb_regex_t **pre) 
b74969
+static void php_mb_regex_free_cache(php_mb_regex_t **pre)
b74969
 {
b74969
 	onig_free(*pre);
b74969
 }
b74969
@@ -78,7 +78,7 @@ static int _php_mb_regex_globals_ctor(zend_mb_regex_globals *pglobals TSRMLS_DC)
b74969
 /* }}} */
b74969
 
b74969
 /* {{{ _php_mb_regex_globals_dtor */
b74969
-static void _php_mb_regex_globals_dtor(zend_mb_regex_globals *pglobals TSRMLS_DC) 
b74969
+static void _php_mb_regex_globals_dtor(zend_mb_regex_globals *pglobals TSRMLS_DC)
b74969
 {
b74969
 	zend_hash_destroy(&pglobals->ht_rc);
b74969
 }
b74969
@@ -466,7 +466,7 @@ static php_mb_regex_t *php_mbregex_compile_pattern(const char *pattern, int patl
b74969
 		retval = *rc;
b74969
 	}
b74969
 out:
b74969
-	return retval; 
b74969
+	return retval;
b74969
 }
b74969
 /* }}} */
b74969
 
b74969
@@ -483,7 +483,7 @@ static size_t _php_mb_regex_get_option_string(char *str, size_t len, OnigOptionT
b74969
 			--len_left;
b74969
 			*(p++) = 'i';
b74969
 		}
b74969
-		++len_req;	
b74969
+		++len_req;
b74969
 	}
b74969
 
b74969
 	if ((option & ONIG_OPTION_EXTEND) != 0) {
b74969
@@ -491,7 +491,7 @@ static size_t _php_mb_regex_get_option_string(char *str, size_t len, OnigOptionT
b74969
 			--len_left;
b74969
 			*(p++) = 'x';
b74969
 		}
b74969
-		++len_req;	
b74969
+		++len_req;
b74969
 	}
b74969
 
b74969
 	if ((option & (ONIG_OPTION_MULTILINE | ONIG_OPTION_SINGLELINE)) ==
b74969
@@ -500,14 +500,14 @@ static size_t _php_mb_regex_get_option_string(char *str, size_t len, OnigOptionT
b74969
 			--len_left;
b74969
 			*(p++) = 'p';
b74969
 		}
b74969
-		++len_req;	
b74969
+		++len_req;
b74969
 	} else {
b74969
 		if ((option & ONIG_OPTION_MULTILINE) != 0) {
b74969
 			if (len_left > 0) {
b74969
 				--len_left;
b74969
 				*(p++) = 'm';
b74969
 			}
b74969
-			++len_req;	
b74969
+			++len_req;
b74969
 		}
b74969
 
b74969
 		if ((option & ONIG_OPTION_SINGLELINE) != 0) {
b74969
@@ -515,22 +515,22 @@ static size_t _php_mb_regex_get_option_string(char *str, size_t len, OnigOptionT
b74969
 				--len_left;
b74969
 				*(p++) = 's';
b74969
 			}
b74969
-			++len_req;	
b74969
+			++len_req;
b74969
 		}
b74969
-	}	
b74969
+	}
b74969
 	if ((option & ONIG_OPTION_FIND_LONGEST) != 0) {
b74969
 		if (len_left > 0) {
b74969
 			--len_left;
b74969
 			*(p++) = 'l';
b74969
 		}
b74969
-		++len_req;	
b74969
+		++len_req;
b74969
 	}
b74969
 	if ((option & ONIG_OPTION_FIND_NOT_EMPTY) != 0) {
b74969
 		if (len_left > 0) {
b74969
 			--len_left;
b74969
 			*(p++) = 'n';
b74969
 		}
b74969
-		++len_req;	
b74969
+		++len_req;
b74969
 	}
b74969
 
b74969
 	c = 0;
b74969
@@ -566,7 +566,7 @@ static size_t _php_mb_regex_get_option_string(char *str, size_t len, OnigOptionT
b74969
 		--len_left;
b74969
 		*(p++) = '\0';
b74969
 	}
b74969
-	++len_req;	
b74969
+	++len_req;
b74969
 	if (len < len_req) {
b74969
 		return len_req;
b74969
 	}
b74969
@@ -577,11 +577,11 @@ static size_t _php_mb_regex_get_option_string(char *str, size_t len, OnigOptionT
b74969
 
b74969
 /* {{{ _php_mb_regex_init_options */
b74969
 static void
b74969
-_php_mb_regex_init_options(const char *parg, int narg, OnigOptionType *option, OnigSyntaxType **syntax, int *eval) 
b74969
+_php_mb_regex_init_options(const char *parg, int narg, OnigOptionType *option, OnigSyntaxType **syntax, int *eval)
b74969
 {
b74969
 	int n;
b74969
 	char c;
b74969
-	int optm = 0; 
b74969
+	int optm = 0;
b74969
 
b74969
 	*syntax = ONIG_SYNTAX_RUBY;
b74969
 
b74969
@@ -636,13 +636,13 @@ _php_mb_regex_init_options(const char *parg, int narg, OnigOptionType *option, O
b74969
 					*syntax = ONIG_SYNTAX_POSIX_EXTENDED;
b74969
 					break;
b74969
 				case 'e':
b74969
-					if (eval != NULL) *eval = 1; 
b74969
+					if (eval != NULL) *eval = 1;
b74969
 					break;
b74969
 				default:
b74969
 					break;
b74969
 			}
b74969
 		}
b74969
-		if (option != NULL) *option|=optm; 
b74969
+		if (option != NULL) *option|=optm;
b74969
 	}
b74969
 }
b74969
 /* }}} */
b74969
@@ -860,11 +860,11 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
b74969
 	} else {
b74969
 		/* FIXME: this code is not multibyte aware! */
b74969
 		convert_to_long_ex(arg_pattern_zval);
b74969
-		pat_buf[0] = (char)Z_LVAL_PP(arg_pattern_zval);	
b74969
+		pat_buf[0] = (char)Z_LVAL_PP(arg_pattern_zval);
b74969
 		pat_buf[1] = '\0';
b74969
 
b74969
 		arg_pattern = pat_buf;
b74969
-		arg_pattern_len = 1;	
b74969
+		arg_pattern_len = 1;
b74969
 	}
b74969
 	/* create regex pattern buffer */
b74969
 	re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, options, MBREX(current_mbctype), syntax TSRMLS_CC);
b74969
@@ -934,7 +934,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
b74969
 					}
b74969
 				}
b74969
 			}
b74969
-				
b74969
+
b74969
 			if (eval) {
b74969
 				zval v;
b74969
 				/* null terminate buffer */
b74969
@@ -953,32 +953,31 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
b74969
 				eval_buf.len = 0;
b74969
 				zval_dtor(&v);
b74969
 			} else if (is_callable) {
b74969
-				zval *retval_ptr;
b74969
+				zval *retval_ptr = NULL;
b74969
 				zval **args[1];
b74969
 				zval *subpats;
b74969
 				int i;
b74969
-				
b74969
+
b74969
 				MAKE_STD_ZVAL(subpats);
b74969
 				array_init(subpats);
b74969
-				
b74969
+
b74969
 				for (i = 0; i < regs->num_regs; i++) {
b74969
 					add_next_index_stringl(subpats, string + regs->beg[i], regs->end[i] - regs->beg[i], 1);
b74969
-				}				
b74969
-				
b74969
+				}
b74969
+
b74969
 				args[0] = &subpats;
b74969
 				/* null terminate buffer */
b74969
 				smart_str_0(&eval_buf);
b74969
-				
b74969
+
b74969
 				arg_replace_fci.param_count = 1;
b74969
 				arg_replace_fci.params = args;
b74969
 				arg_replace_fci.retval_ptr_ptr = &retval_ptr;
b74969
-				if (zend_call_function(&arg_replace_fci, &arg_replace_fci_cache TSRMLS_CC) == SUCCESS && arg_replace_fci.retval_ptr_ptr) {
b74969
+				if (zend_call_function(&arg_replace_fci, &arg_replace_fci_cache TSRMLS_CC) == SUCCESS && arg_replace_fci.retval_ptr_ptr && retval_ptr) {
b74969
 					convert_to_string_ex(&retval_ptr);
b74969
 					smart_str_appendl(&out_buf, Z_STRVAL_P(retval_ptr), Z_STRLEN_P(retval_ptr));
b74969
 					eval_buf.len = 0;
b74969
 					zval_ptr_dtor(&retval_ptr);
b74969
 				} else {
b74969
-					efree(description);
b74969
 					if (!EG(exception)) {
b74969
 						php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to call custom replacement function");
b74969
 					}
b74969
@@ -991,7 +990,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
b74969
 				pos = (OnigUChar *)string + n;
b74969
 			} else {
b74969
 				if (pos < string_lim) {
b74969
-					smart_str_appendl(&out_buf, pos, 1); 
b74969
+					smart_str_appendl(&out_buf, pos, 1);
b74969
 				}
b74969
 				pos++;
b74969
 			}
b74969
@@ -1013,7 +1012,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
b74969
 	smart_str_free(&eval_buf);
b74969
 
b74969
 	if (err <= -2) {
b74969
-		smart_str_free(&out_buf);	
b74969
+		smart_str_free(&out_buf);
b74969
 		RETVAL_FALSE;
b74969
 	} else {
b74969
 		smart_str_appendc(&out_buf, '\0');
b74969
@@ -1063,7 +1062,7 @@ PHP_FUNCTION(mb_split)
b74969
 
b74969
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &arg_pattern, &arg_pattern_len, &string, &string_len, &count) == FAILURE) {
b74969
 		RETURN_FALSE;
b74969
-	} 
b74969
+	}
b74969
 
b74969
 	if (count > 0) {
b74969
 		count--;
b74969
@@ -1317,7 +1316,7 @@ PHP_FUNCTION(mb_ereg_search_init)
b74969
 	if (zend_parse_parameters(argc TSRMLS_CC, "z|ss", &arg_str, &arg_pattern, &arg_pattern_len, &arg_options, &arg_options_len) == FAILURE) {
b74969
 		return;
b74969
 	}
b74969
-	
b74969
+
b74969
 	if (argc > 1 && arg_pattern_len == 0) {
b74969
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty pattern");
b74969
 		RETURN_FALSE;
b74969
@@ -1416,7 +1415,7 @@ PHP_FUNCTION(mb_ereg_search_setpos)
b74969
 /* }}} */
b74969
 
b74969
 /* {{{ php_mb_regex_set_options */
b74969
-static void _php_mb_regex_set_options(OnigOptionType options, OnigSyntaxType *syntax, OnigOptionType *prev_options, OnigSyntaxType **prev_syntax TSRMLS_DC) 
b74969
+static void _php_mb_regex_set_options(OnigOptionType options, OnigSyntaxType *syntax, OnigOptionType *prev_options, OnigSyntaxType **prev_syntax TSRMLS_DC)
b74969
 {
b74969
 	if (prev_options != NULL) {
b74969
 		*prev_options = MBREX(regex_default_options);
b74969
diff --git a/ext/mbstring/tests/bug72402.phpt b/ext/mbstring/tests/bug72402.phpt
b74969
new file mode 100644
b74969
index 0000000..abb290b
b74969
--- /dev/null
b74969
+++ b/ext/mbstring/tests/bug72402.phpt
b74969
@@ -0,0 +1,17 @@
b74969
+--TEST--
b74969
+Bug #72402: _php_mb_regex_ereg_replace_exec - double free
b74969
+--SKIPIF--
b74969
+
b74969
+--FILE--
b74969
+
b74969
+function throwit() {
b74969
+	throw new Exception('it');
b74969
+}
b74969
+$var10 = "throwit";
b74969
+try {
b74969
+	$var14 = mb_ereg_replace_callback("", $var10, "");
b74969
+} catch(Exception $e) {}
b74969
+?>
b74969
+DONE
b74969
+--EXPECT--
b74969
+DONE
b74969
\ No newline at end of file