Skip to content

Commit a2f79ac

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

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

ext/curl/interface.c

Lines changed: 17 additions & 12 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);
@@ -1413,8 +1413,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14131413

14141414
part = curl_mime_addpart(mime);
14151415
if (part == NULL) {
1416-
zend_string_release_ex(string_key, 0);
1417-
return FAILURE;
1416+
goto out_string;
14181417
}
14191418
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
14201419
|| (form_error = curl_mime_data_cb(part, filesize, read_cb, seekfunc, free_cb, cb_arg)) != CURLE_OK
@@ -1449,8 +1448,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14491448

14501449
prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "postname", sizeof("postname")-1, 0, &rv);
14511450
if (EG(exception)) {
1452-
zend_string_release_ex(string_key, 0);
1453-
return FAILURE;
1451+
goto out_string;
14541452
}
14551453
ZVAL_DEREF(prop);
14561454
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
@@ -1459,8 +1457,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14591457

14601458
prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "mime", sizeof("mime")-1, 0, &rv);
14611459
if (EG(exception)) {
1462-
zend_string_release_ex(string_key, 0);
1463-
return FAILURE;
1460+
goto out_string;
14641461
}
14651462
ZVAL_DEREF(prop);
14661463
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
@@ -1469,8 +1466,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14691466

14701467
prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "data", sizeof("data")-1, 0, &rv);
14711468
if (EG(exception)) {
1472-
zend_string_release_ex(string_key, 0);
1473-
return FAILURE;
1469+
goto out_string;
14741470
}
14751471
ZVAL_DEREF(prop);
14761472
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
@@ -1483,8 +1479,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
14831479

14841480
part = curl_mime_addpart(mime);
14851481
if (part == NULL) {
1486-
zend_string_release_ex(string_key, 0);
1487-
return FAILURE;
1482+
goto out_string;
14881483
}
14891484
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
14901485
|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK
@@ -1540,7 +1535,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
15401535

15411536
SAVE_CURL_ERROR(ch, error);
15421537
if (error != CURLE_OK) {
1543-
return FAILURE;
1538+
goto out_mime;
15441539
}
15451540

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

15571552
SAVE_CURL_ERROR(ch, error);
15581553
return error == CURLE_OK ? SUCCESS : FAILURE;
1554+
1555+
out_string:
1556+
zend_string_release_ex(string_key, false);
1557+
out_mime:
1558+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
1559+
curl_mime_free(mime);
1560+
#else
1561+
curl_formfree(first);
1562+
#endif
1563+
return FAILURE;
15591564
}
15601565
/* }}} */
15611566

0 commit comments

Comments
 (0)