com php-src: Fixed memory leak in str_pad: ext/standard/s tring.c
| From: | Dmitry Stogov | Date: | Mon, 24 Feb 2014 09:44:42 +0000 |
| Subject: | com php-src: Fixed memory leak in str_pad: ext/standard/s tring.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-77939@lists.php.net to get a copy of this message | ||
Commit: 9e9f6c7123599a022a9acfeb52d0ad2152200827
Author: Xinchen Hui <laruence@gmail.com> Mon, 24 Feb 2014 17:44:42 +0800
Parents: b5291556352ecd7a3675cbdfb133e0f7f49984a1
Branches: phpng
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=9e9f6c7123599a022a9acfeb52d0ad2152200827
Log:
Fixed memory leak in str_pad
Changed paths:
M ext/standard/string.c
Diff:
diff --git a/ext/standard/string.c b/ext/standard/string.c
index 40618f8..1e8e55b 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -5158,13 +5158,12 @@ PHP_FUNCTION(str_pad)
long pad_length; /* Length to pad to */
/* Helper variables */
- size_t num_pad_chars; /* Number of padding characters (total - input size) */
- char *result = NULL; /* Resulting string */
- int result_len = 0; /* Length of the resulting string */
+ size_t num_pad_chars; /* Number of padding characters (total - input size) */
char *pad_str_val = " "; /* Pointer to padding string */
int pad_str_len = 1; /* Length of the padding string */
long pad_type_val = STR_PAD_RIGHT; /* The padding type value */
int i, left_pad=0, right_pad=0;
+ zend_string *result = NULL; /* Resulting string */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|sl", &input,
&input_len, &pad_length,
&pad_str_val, &pad_str_len, &pad_type_val) == FAILURE) {
@@ -5192,7 +5191,9 @@ PHP_FUNCTION(str_pad)
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Padding length is too long");
return;
}
- result = (char *)emalloc(input_len + num_pad_chars + 1);
+
+ result = STR_ALLOC(input_len + num_pad_chars, 0);
+ result->len = 0;
/* We need to figure out the left/right padding lengths. */
switch (pad_type_val) {
@@ -5214,20 +5215,19 @@ PHP_FUNCTION(str_pad)
/* First we pad on the left. */
for (i = 0; i < left_pad; i++)
- result[result_len++] = pad_str_val[i % pad_str_len];
+ result->val[result->len++] = pad_str_val[i % pad_str_len];
/* Then we copy the input string. */
- memcpy(result + result_len, input, input_len);
- result_len += input_len;
+ memcpy(result->val + result->len, input, input_len);
+ result->len += input_len;
/* Finally, we pad on the right. */
for (i = 0; i < right_pad; i++)
- result[result_len++] = pad_str_val[i % pad_str_len];
+ result->val[result->len++] = pad_str_val[i % pad_str_len];
- result[result_len] = '\0';
+ result->val[result->len] = '\0';
-//??? RETURN_STRINGL(result, result_len, 0);
- RETURN_STRINGL(result, result_len);
+ RETURN_STR(result);
}
/* }}} */