Skip to content

Commit a80f0b5

Browse files
committed
Fix various memory leaks in curl mime handling
Closes phpGH-16745.
1 parent 18674e3 commit a80f0b5

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ PHP NEWS
1616
(nielsdos)
1717
. Fix is_zend_ptr() huge block comparison. (nielsdos)
1818

19+
- Curl:
20+
. Fix various memory leaks in curl mime handling. (nielsdos)
21+
1922
- FPM:
2023
. Fixed GH-16432 (PHP-FPM 8.2 SIGSEGV in fpm_get_status). (Jakub Zelenka)
2124

ext/curl/interface.c

+24-15
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
13811381
postval = Z_STR_P(prop);
13821382

13831383
if (php_check_open_basedir(ZSTR_VAL(postval))) {
1384-
return FAILURE;
1384+
goto out_string;
13851385
}
13861386

13871387
prop = zend_read_property(curl_CURLFile_class, Z_OBJ_P(current), "mime", sizeof("mime")-1, 0, &rv);
@@ -1407,15 +1407,18 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14071407
seekfunc = NULL;
14081408
}
14091409

1410+
part = curl_mime_addpart(mime);
1411+
if (part == NULL) {
1412+
if (stream) {
1413+
php_stream_close(stream);
1414+
}
1415+
goto out_string;
1416+
}
1417+
14101418
cb_arg = emalloc(sizeof *cb_arg);
14111419
cb_arg->filename = zend_string_copy(postval);
14121420
cb_arg->stream = stream;
14131421

1414-
part = curl_mime_addpart(mime);
1415-
if (part == NULL) {
1416-
zend_string_release_ex(string_key, 0);
1417-
return FAILURE;
1418-
}
14191422
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
14201423
|| (form_error = curl_mime_data_cb(part, filesize, read_cb, seekfunc, free_cb, cb_arg)) != CURLE_OK
14211424
|| (form_error = curl_mime_filename(part, filename ? filename : ZSTR_VAL(postval))) != CURLE_OK
@@ -1449,8 +1452,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14491452

14501453
prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "postname", sizeof("postname")-1, 0, &rv);
14511454
if (EG(exception)) {
1452-
zend_string_release_ex(string_key, 0);
1453-
return FAILURE;
1455+
goto out_string;
14541456
}
14551457
ZVAL_DEREF(prop);
14561458
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
@@ -1459,8 +1461,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14591461

14601462
prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "mime", sizeof("mime")-1, 0, &rv);
14611463
if (EG(exception)) {
1462-
zend_string_release_ex(string_key, 0);
1463-
return FAILURE;
1464+
goto out_string;
14641465
}
14651466
ZVAL_DEREF(prop);
14661467
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
@@ -1469,8 +1470,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14691470

14701471
prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "data", sizeof("data")-1, 0, &rv);
14711472
if (EG(exception)) {
1472-
zend_string_release_ex(string_key, 0);
1473-
return FAILURE;
1473+
goto out_string;
14741474
}
14751475
ZVAL_DEREF(prop);
14761476
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
@@ -1483,8 +1483,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14831483

14841484
part = curl_mime_addpart(mime);
14851485
if (part == NULL) {
1486-
zend_string_release_ex(string_key, 0);
1487-
return FAILURE;
1486+
goto out_string;
14881487
}
14891488
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
14901489
|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK
@@ -1540,7 +1539,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
15401539

15411540
SAVE_CURL_ERROR(ch, error);
15421541
if (error != CURLE_OK) {
1543-
return FAILURE;
1542+
goto out_mime;
15441543
}
15451544

15461545
if ((*ch->clone) == 1) {
@@ -1556,6 +1555,16 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
15561555

15571556
SAVE_CURL_ERROR(ch, error);
15581557
return error == CURLE_OK ? SUCCESS : FAILURE;
1558+
1559+
out_string:
1560+
zend_string_release_ex(string_key, false);
1561+
out_mime:
1562+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
1563+
curl_mime_free(mime);
1564+
#else
1565+
curl_formfree(first);
1566+
#endif
1567+
return FAILURE;
15591568
}
15601569
/* }}} */
15611570

0 commit comments

Comments
 (0)