summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJani Taskinen <jani@php.net>2008-12-17 14:20:30 +0000
committerJani Taskinen <jani@php.net>2008-12-17 14:20:30 +0000
commit3c1bcc2c8c33b348a192f4d28345648e53f7debf (patch)
tree6c5dd375b3e97729fc91279ac9c604ca6dcb26b7
parent1cd04d8739ffafe513c5e22dc76bc72f4ca8215f (diff)
downloadphp-git-3c1bcc2c8c33b348a192f4d28345648e53f7debf.tar.gz
MFH:- Fixed bug #45161 (Reusing a curl handle leaks memory)
-rw-r--r--NEWS1
-rw-r--r--ext/curl/interface.c39
-rw-r--r--ext/curl/php_curl.h2
3 files changed, 35 insertions, 7 deletions
diff --git a/NEWS b/NEWS
index eaa1e4378d..321b477f2b 100644
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,7 @@ PHP NEWS
- Fixed bug #46739 (array returned by curl_getinfo should contain content_type key).
(Mikko)
- Fixed bug #46699 (xml_parse crash when parser is namespace aware). (Rob)
+- Fixed bug #45161 (Reusing a curl handle leaks memory). (Mark Karpeles, Jani)
- Fixed bug #35975 (Session cookie expires date format isn't the most compatible.
Now matches that of setcookie()). (Scott)
diff --git a/ext/curl/interface.c b/ext/curl/interface.c
index acb038e13d..182f6f08cc 100644
--- a/ext/curl/interface.c
+++ b/ext/curl/interface.c
@@ -996,6 +996,7 @@ static size_t curl_passwd(void *ctx, char *prompt, char *buf, int buflen)
/* }}} */
#endif
+#if LIBCURL_VERSION_NUM < 0x071101
/* {{{ curl_free_string
*/
static void curl_free_string(void **string)
@@ -1003,6 +1004,7 @@ static void curl_free_string(void **string)
efree(*string);
}
/* }}} */
+#endif
/* {{{ curl_free_post
*/
@@ -1077,7 +1079,9 @@ static void alloc_curl_handle(php_curl **ch)
memset(&(*ch)->err, 0, sizeof((*ch)->err));
+#if LIBCURL_VERSION_NUM < 0x071101
zend_llist_init(&(*ch)->to_free.str, sizeof(char *), (llist_dtor_func_t) curl_free_string, 0);
+#endif
zend_llist_init(&(*ch)->to_free.slist, sizeof(struct curl_slist), (llist_dtor_func_t) curl_free_slist, 0);
zend_llist_init(&(*ch)->to_free.post, sizeof(struct HttpPost), (llist_dtor_func_t) curl_free_post, 0);
}
@@ -1136,11 +1140,15 @@ PHP_FUNCTION(curl_init)
#endif
if (argc > 0) {
+#if LIBCURL_VERSION_NUM >= 0x071100
+ curl_easy_setopt(ch->cp, CURLOPT_URL, Z_STRVAL_PP(url));
+#else
char *urlcopy;
urlcopy = estrndup(Z_STRVAL_PP(url), Z_STRLEN_PP(url));
curl_easy_setopt(ch->cp, CURLOPT_URL, urlcopy);
zend_llist_add_element(&ch->to_free.str, &urlcopy);
+#endif
}
ZEND_REGISTER_RESOURCE(return_value, ch, le_curl);
@@ -1206,9 +1214,11 @@ PHP_FUNCTION(curl_copy_handle)
curl_easy_setopt(dupch->cp, CURLOPT_INFILE, (void *) dupch);
curl_easy_setopt(dupch->cp, CURLOPT_WRITEHEADER, (void *) dupch);
+#if LIBCURL_VERSION_NUM < 0x071101
zend_llist_copy(&dupch->to_free.str, &ch->to_free.str);
/* Don't try to free copied strings, they're free'd when the original handle is destroyed */
dupch->to_free.str.dtor = NULL;
+#endif
zend_llist_copy(&dupch->to_free.slist, &ch->to_free.slist);
zend_llist_copy(&dupch->to_free.post, &ch->to_free.post);
@@ -1331,7 +1341,9 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue, zval *retu
case CURLOPT_SSLENGINE_DEFAULT:
case CURLOPT_SSLCERTTYPE:
case CURLOPT_ENCODING: {
+#if LIBCURL_VERSION_NUM < 0x071100
char *copystr = NULL;
+#endif
convert_to_string_ex(zvalue);
@@ -1339,9 +1351,14 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue, zval *retu
PHP_CURL_CHECK_OPEN_BASEDIR(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue), 1);
}
+#if LIBCURL_VERSION_NUM >= 0x071100
+ /* Strings passed to libcurl as ’char *’ arguments, are copied by the library... NOTE: before 7.17.0 strings were not copied. */
+ error = curl_easy_setopt(ch->cp, option, Z_STRVAL_PP(zvalue));
+#else
copystr = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue));
error = curl_easy_setopt(ch->cp, option, copystr);
zend_llist_add_element(&ch->to_free.str, &copystr);
+#endif
break;
}
@@ -1532,6 +1549,11 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue, zval *retu
error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
} else {
+#if LIBCURL_VERSION_NUM >= 0x071101
+ /* with curl 7.17.0 and later, we can use COPYPOSTFIELDS, but we have to provide size before */
+ error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue));
+ error = curl_easy_setopt(ch->cp, CURLOPT_COPYPOSTFIELDS, Z_STRVAL_PP(zvalue));
+#else
char *post = NULL;
convert_to_string_ex(zvalue);
@@ -1540,6 +1562,7 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue, zval *retu
error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, post);
error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue));
+#endif
}
break;
case CURLOPT_HTTPHEADER:
@@ -1561,20 +1584,15 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue, zval *retu
zend_hash_get_current_data(ph, (void **) &current) == SUCCESS;
zend_hash_move_forward(ph)
) {
- char *indiv = NULL;
-
SEPARATE_ZVAL(current);
convert_to_string_ex(current);
- indiv = estrndup(Z_STRVAL_PP(current), Z_STRLEN_PP(current) + 1);
- slist = curl_slist_append(slist, indiv);
+ slist = curl_slist_append(slist, Z_STRVAL_PP(current));
if (!slist) {
- efree(indiv);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not build curl_slist");
RETVAL_FALSE;
return 1;
}
- zend_llist_add_element(&ch->to_free.str, &indiv);
}
zend_llist_add_element(&ch->to_free.slist, &slist);
@@ -1589,7 +1607,9 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue, zval *retu
case CURLOPT_SSLCERT:
case CURLOPT_RANDOM_FILE:
case CURLOPT_COOKIEFILE: {
+#if LIBCURL_VERSION_NUM < 0x071100
char *copystr = NULL;
+#endif
convert_to_string_ex(zvalue);
@@ -1598,11 +1618,14 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue, zval *retu
return 1;
}
+#if LIBCURL_VERSION_NUM >= 0x071100
+ error = curl_easy_setopt(ch->cp, option, Z_STRVAL_PP(zvalue));
+#else
copystr = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue));
error = curl_easy_setopt(ch->cp, option, copystr);
zend_llist_add_element(&ch->to_free.str, &copystr);
-
+#endif
break;
}
case CURLINFO_HEADER_OUT:
@@ -1968,7 +1991,9 @@ static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC)
#endif
curl_easy_cleanup(ch->cp);
+#if LIBCURL_VERSION_NUM < 0x071101
zend_llist_clean(&ch->to_free.str);
+#endif
zend_llist_clean(&ch->to_free.slist);
zend_llist_clean(&ch->to_free.post);
diff --git a/ext/curl/php_curl.h b/ext/curl/php_curl.h
index 64d11fbab3..25ee080111 100644
--- a/ext/curl/php_curl.h
+++ b/ext/curl/php_curl.h
@@ -114,7 +114,9 @@ struct _php_curl_send_headers {
};
struct _php_curl_free {
+#if LIBCURL_VERSION_NUM < 0x071100
zend_llist str;
+#endif
zend_llist post;
zend_llist slist;
};