Skip to content

Commit 8906021

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: Fix various memory leaks in curl mime handling
2 parents 02b1056 + a80f0b5 commit 8906021

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PHP NEWS
1818

1919
- Curl:
2020
. Fixed bug GH-16802 (open_basedir bypass using curl extension). (nielsdos)
21+
. Fix various memory leaks in curl mime handling. (nielsdos)
2122

2223
- DOM:
2324
. Fixed bug GH-16777 (Calling the constructor again on a DOM object after it

ext/curl/interface.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14431443
postval = Z_STR_P(prop);
14441444

14451445
if (php_check_open_basedir(ZSTR_VAL(postval))) {
1446-
return FAILURE;
1446+
goto out_string;
14471447
}
14481448

14491449
prop = zend_read_property(curl_CURLFile_class, Z_OBJ_P(current), "mime", sizeof("mime")-1, 0, &rv);
@@ -1469,15 +1469,18 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14691469
seekfunc = NULL;
14701470
}
14711471

1472+
part = curl_mime_addpart(mime);
1473+
if (part == NULL) {
1474+
if (stream) {
1475+
php_stream_close(stream);
1476+
}
1477+
goto out_string;
1478+
}
1479+
14721480
cb_arg = emalloc(sizeof *cb_arg);
14731481
cb_arg->filename = zend_string_copy(postval);
14741482
cb_arg->stream = stream;
14751483

1476-
part = curl_mime_addpart(mime);
1477-
if (part == NULL) {
1478-
zend_string_release_ex(string_key, 0);
1479-
return FAILURE;
1480-
}
14811484
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
14821485
|| (form_error = curl_mime_data_cb(part, filesize, read_cb, seekfunc, free_cb, cb_arg)) != CURLE_OK
14831486
|| (form_error = curl_mime_filename(part, filename ? filename : ZSTR_VAL(postval))) != CURLE_OK
@@ -1511,8 +1514,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
15111514

15121515
prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "postname", sizeof("postname")-1, 0, &rv);
15131516
if (EG(exception)) {
1514-
zend_string_release_ex(string_key, 0);
1515-
return FAILURE;
1517+
goto out_string;
15161518
}
15171519
ZVAL_DEREF(prop);
15181520
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
@@ -1521,8 +1523,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
15211523

15221524
prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "mime", sizeof("mime")-1, 0, &rv);
15231525
if (EG(exception)) {
1524-
zend_string_release_ex(string_key, 0);
1525-
return FAILURE;
1526+
goto out_string;
15261527
}
15271528
ZVAL_DEREF(prop);
15281529
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
@@ -1531,8 +1532,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
15311532

15321533
prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "data", sizeof("data")-1, 0, &rv);
15331534
if (EG(exception)) {
1534-
zend_string_release_ex(string_key, 0);
1535-
return FAILURE;
1535+
goto out_string;
15361536
}
15371537
ZVAL_DEREF(prop);
15381538
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
@@ -1545,8 +1545,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
15451545

15461546
part = curl_mime_addpart(mime);
15471547
if (part == NULL) {
1548-
zend_string_release_ex(string_key, 0);
1549-
return FAILURE;
1548+
goto out_string;
15501549
}
15511550
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
15521551
|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK
@@ -1602,7 +1601,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
16021601

16031602
SAVE_CURL_ERROR(ch, error);
16041603
if (error != CURLE_OK) {
1605-
return FAILURE;
1604+
goto out_mime;
16061605
}
16071606

16081607
if ((*ch->clone) == 1) {
@@ -1618,6 +1617,16 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
16181617

16191618
SAVE_CURL_ERROR(ch, error);
16201619
return error == CURLE_OK ? SUCCESS : FAILURE;
1620+
1621+
out_string:
1622+
zend_string_release_ex(string_key, false);
1623+
out_mime:
1624+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
1625+
curl_mime_free(mime);
1626+
#else
1627+
curl_formfree(first);
1628+
#endif
1629+
return FAILURE;
16211630
}
16221631
/* }}} */
16231632

0 commit comments

Comments
 (0)