Skip to content

Commit

Permalink
Merge branch 'PHP-8.4'
Browse files Browse the repository at this point in the history
* PHP-8.4:
  Fix various memory leaks in curl mime handling
  • Loading branch information
nielsdos committed Nov 26, 2024
2 parents be27cbd + 8206de6 commit 4591f04
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions ext/curl/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
postval = Z_STR_P(prop);

if (php_check_open_basedir(ZSTR_VAL(postval))) {
return FAILURE;
goto out_string;
}

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

part = curl_mime_addpart(mime);
if (part == NULL) {
if (stream) {
php_stream_close(stream);
}
goto out_string;
}

cb_arg = emalloc(sizeof *cb_arg);
cb_arg->filename = zend_string_copy(postval);
cb_arg->stream = stream;

part = curl_mime_addpart(mime);
if (part == NULL) {
zend_string_release_ex(string_key, 0);
return FAILURE;
}
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
|| (form_error = curl_mime_data_cb(part, filesize, read_cb, seekfunc, free_cb, cb_arg)) != CURLE_OK
|| (form_error = curl_mime_filename(part, filename ? filename : ZSTR_VAL(postval))) != CURLE_OK
Expand All @@ -1490,8 +1493,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo

prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "postname", sizeof("postname")-1, 0, &rv);
if (EG(exception)) {
zend_string_release_ex(string_key, 0);
return FAILURE;
goto out_string;
}
ZVAL_DEREF(prop);
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
Expand All @@ -1500,8 +1502,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo

prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "mime", sizeof("mime")-1, 0, &rv);
if (EG(exception)) {
zend_string_release_ex(string_key, 0);
return FAILURE;
goto out_string;
}
ZVAL_DEREF(prop);
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
Expand All @@ -1510,8 +1511,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo

prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "data", sizeof("data")-1, 0, &rv);
if (EG(exception)) {
zend_string_release_ex(string_key, 0);
return FAILURE;
goto out_string;
}
ZVAL_DEREF(prop);
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);
Expand All @@ -1523,8 +1523,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo

part = curl_mime_addpart(mime);
if (part == NULL) {
zend_string_release_ex(string_key, 0);
return FAILURE;
goto out_string;
}
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK
Expand Down Expand Up @@ -1555,7 +1554,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo

SAVE_CURL_ERROR(ch, error);
if (error != CURLE_OK) {
return FAILURE;
goto out_mime;
}

if ((*ch->clone) == 1) {
Expand All @@ -1566,6 +1565,12 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo

SAVE_CURL_ERROR(ch, error);
return error == CURLE_OK ? SUCCESS : FAILURE;

out_string:
zend_string_release_ex(string_key, false);
out_mime:
curl_mime_free(mime);
return FAILURE;
}
/* }}} */

Expand Down

0 comments on commit 4591f04

Please sign in to comment.