Skip to content

Commit 7340ba4

Browse files
committed
Use emalloc and friends in curl
This avoids memory leaks when bailing in unexpected places (e.g. due to memory limits).
1 parent 8d8cfe2 commit 7340ba4

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

ext/curl/interface.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,34 @@ PHP_MINFO_FUNCTION(curl)
378378
}
379379
/* }}} */
380380

381+
#if ZEND_DEBUG
382+
static void *zend_curl_malloc(size_t size) {
383+
return emalloc(size);
384+
}
385+
386+
static void zend_curl_free(void *ptr) {
387+
efree(ptr);
388+
}
389+
390+
static void *zend_curl_realloc(void *ptr, size_t size) {
391+
return erealloc(ptr, size);
392+
}
393+
394+
static char *zend_curl_strdup(const char *s) {
395+
return estrdup(s);
396+
}
397+
398+
static void *zend_curl_calloc(size_t nmemb, size_t size) {
399+
return ecalloc(nmemb, size);
400+
}
401+
#else
402+
# define zend_curl_malloc _emalloc
403+
# define zend_curl_free _efree
404+
# define zend_curl_realloc _erealloc
405+
# define zend_curl_strdup _estrdup
406+
# define zend_curl_calloc _ecalloc
407+
#endif
408+
381409
/* {{{ PHP_MINIT_FUNCTION */
382410
PHP_MINIT_FUNCTION(curl)
383411
{
@@ -403,7 +431,7 @@ PHP_MINIT_FUNCTION(curl)
403431
}
404432
#endif
405433

406-
if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) {
434+
if (curl_global_init_mem(CURL_GLOBAL_DEFAULT, zend_curl_malloc, zend_curl_free, zend_curl_realloc, zend_curl_strdup, zend_curl_calloc) != CURLE_OK) {
407435
return FAILURE;
408436
}
409437

ext/curl/tests/bug45161.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ for ($i = 0; $i < 100; $i++) {
2525
*/
2626

2727
// Start actual test
28-
$start = memory_get_usage() + 1024;
2928
for($i = 0; $i < 1024; $i++) {
3029
curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc");
3130
curl_setopt($ch, CURLOPT_FILE, $fp);
3231
curl_exec($ch);
32+
33+
if ($i === 0) {
34+
$start = memory_get_usage();
35+
}
3336
}
3437
if ($start < memory_get_usage()) {
3538
echo 'FAIL';

0 commit comments

Comments
 (0)