Skip to content

curl: Prevent a CurlMultiHandle from holding onto a CurlHandle if add_handle fails #16302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.2.26

- Curl:
. Fixed bug GH-16302 (CurlMultiHandle holds a reference to CurlHandle if
curl_multi_add_handle fails). (timwolla)

- XMLReader:
. Fixed bug GH-16292 (Segmentation fault in ext/xmlreader/php_xmlreader.c).
(nielsdos)
Expand Down
14 changes: 9 additions & 5 deletions ext/curl/multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ PHP_FUNCTION(curl_multi_add_handle)

_php_curl_cleanup_handle(ch);

Z_ADDREF_P(z_ch);
zend_llist_add_element(&mh->easyh, z_ch);

error = curl_multi_add_handle(mh->multi, ch->cp);
SAVE_CURLM_ERROR(mh, error);

if (error == CURLM_OK) {
Z_ADDREF_P(z_ch);
zend_llist_add_element(&mh->easyh, z_ch);
}

RETURN_LONG((zend_long) error);
}
/* }}} */
Expand Down Expand Up @@ -164,9 +166,11 @@ PHP_FUNCTION(curl_multi_remove_handle)
error = curl_multi_remove_handle(mh->multi, ch->cp);
SAVE_CURLM_ERROR(mh, error);

RETVAL_LONG((zend_long) error);
zend_llist_del_element(&mh->easyh, z_ch, (int (*)(void *, void *))curl_compare_objects);
if (error == CURLM_OK) {
zend_llist_del_element(&mh->easyh, z_ch, (int (*)(void *, void *))curl_compare_objects);
}

RETURN_LONG((zend_long) error);
}
/* }}} */

Expand Down
49 changes: 49 additions & 0 deletions ext/curl/tests/curl_multi_add_handle_lifecycle.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--TEST--
curl_multi_add_handle does not hold onto the handle on failure
--EXTENSIONS--
curl
--FILE--
<?php

class MyClass {
public function __destruct() {
echo __METHOD__, PHP_EOL;
}
}

$urls = [
"file://".__DIR__."/curl_testdata1.txt",
"file://".__DIR__."/curl_testdata2.txt",
];

$mh = curl_multi_init();

$toRemove = [];
foreach ($urls as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PRIVATE, new MyClass());

if (curl_multi_add_handle($mh, $ch) == CURLM_OK) {
$toRemove[] = $ch;
}
if (curl_multi_add_handle($mh, $ch) == CURLM_OK) {
$toRemove[] = $ch;
}

unset($ch);
}

echo "Removing", PHP_EOL;
foreach ($toRemove as $i => $ch) {
curl_multi_remove_handle($mh, $ch);
unset($ch);
unset($toRemove[$i]);
}
echo "Removed", PHP_EOL;

?>
--EXPECTF--
Removing
MyClass::__destruct
MyClass::__destruct
Removed
Loading