Skip to content

Commit 033ee1a

Browse files
committed
Fix various memory leaks in curl mime handling
1 parent 994e866 commit 033ee1a

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

ext/curl/interface.c

Lines changed: 22 additions & 15 deletions
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,16 @@ 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+
php_stream_close(stream);
1413+
goto out_string;
1414+
}
1415+
14101416
cb_arg = emalloc(sizeof *cb_arg);
14111417
cb_arg->filename = zend_string_copy(postval);
14121418
cb_arg->stream = stream;
14131419

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

14501451
prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "postname", sizeof("postname")-1, 0, &rv);
14511452
if (EG(exception)) {
1452-
zend_string_release_ex(string_key, 0);
1453-
return FAILURE;
1453+
goto out_string;
14541454
}
14551455
ZVAL_DEREF(prop);
14561456
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
@@ -1459,8 +1459,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14591459

14601460
prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "mime", sizeof("mime")-1, 0, &rv);
14611461
if (EG(exception)) {
1462-
zend_string_release_ex(string_key, 0);
1463-
return FAILURE;
1462+
goto out_string;
14641463
}
14651464
ZVAL_DEREF(prop);
14661465
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
@@ -1469,8 +1468,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14691468

14701469
prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "data", sizeof("data")-1, 0, &rv);
14711470
if (EG(exception)) {
1472-
zend_string_release_ex(string_key, 0);
1473-
return FAILURE;
1471+
goto out_string;
14741472
}
14751473
ZVAL_DEREF(prop);
14761474
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
@@ -1483,8 +1481,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14831481

14841482
part = curl_mime_addpart(mime);
14851483
if (part == NULL) {
1486-
zend_string_release_ex(string_key, 0);
1487-
return FAILURE;
1484+
goto out_string;
14881485
}
14891486
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
14901487
|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK
@@ -1540,7 +1537,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
15401537

15411538
SAVE_CURL_ERROR(ch, error);
15421539
if (error != CURLE_OK) {
1543-
return FAILURE;
1540+
goto out_mime;
15441541
}
15451542

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

15571554
SAVE_CURL_ERROR(ch, error);
15581555
return error == CURLE_OK ? SUCCESS : FAILURE;
1556+
1557+
out_string:
1558+
zend_string_release_ex(string_key, false);
1559+
out_mime:
1560+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
1561+
curl_mime_free(mime);
1562+
#else
1563+
curl_formfree(first);
1564+
#endif
1565+
return FAILURE;
15591566
}
15601567
/* }}} */
15611568

0 commit comments

Comments
 (0)