Skip to content

Commit 42f8776

Browse files
committed
Fix GH-16359 curl write callback crash on FCC usage w/o user function.
close GH-16362
1 parent 2577b89 commit 42f8776

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ PHP NEWS
1414
- Curl:
1515
. Fixed bug GH-16302 (CurlMultiHandle holds a reference to CurlHandle if
1616
curl_multi_add_handle fails). (timwolla)
17+
. Fixed bug GH-16359 (crash with curl_setopt* CURLOPT_WRITEFUNCTION
18+
without null callback). (David Carlier)
1719

1820
- DOM:
1921
. Fixed bug GH-16316 (DOMXPath breaks when not initialized properly).

ext/curl/interface.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,12 +1631,17 @@ static bool php_curl_set_callable_handler(zend_fcall_info_cache *const handler_f
16311631
}
16321632

16331633

1634-
#define HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(curl_ptr, constant_no_function, handler_type) \
1634+
#define HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(curl_ptr, constant_no_function, handler_type, default_method) \
16351635
case constant_no_function##FUNCTION: { \
16361636
bool result = php_curl_set_callable_handler(&curl_ptr->handlers.handler_type->fcc, zvalue, is_array_config, #constant_no_function "FUNCTION"); \
16371637
if (!result) { \
1638+
curl_ptr->handlers.handler_type->method = default_method; \
16381639
return FAILURE; \
16391640
} \
1641+
if (!ZEND_FCC_INITIALIZED(curl_ptr->handlers.handler_type->fcc)) { \
1642+
curl_ptr->handlers.handler_type->method = default_method; \
1643+
return SUCCESS; \
1644+
} \
16401645
curl_ptr->handlers.handler_type->method = PHP_CURL_USER; \
16411646
break; \
16421647
}
@@ -1659,9 +1664,9 @@ static zend_result _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue
16591664

16601665
switch (option) {
16611666
/* Callable options */
1662-
HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(ch, CURLOPT_WRITE, write);
1663-
HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(ch, CURLOPT_HEADER, write_header);
1664-
HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(ch, CURLOPT_READ, read);
1667+
HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(ch, CURLOPT_WRITE, write, PHP_CURL_STDOUT);
1668+
HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(ch, CURLOPT_HEADER, write_header, PHP_CURL_IGNORE);
1669+
HANDLE_CURL_OPTION_CALLABLE_PHP_CURL_USER(ch, CURLOPT_READ, read, PHP_CURL_DIRECT);
16651670

16661671
HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_PROGRESS, handlers.progress, curl_progress);
16671672
HANDLE_CURL_OPTION_CALLABLE(ch, CURLOPT_XFERINFO, handlers.xferinfo, curl_xferinfo);

ext/curl/tests/gh16359.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
GH-16359 - curl_setopt with CURLOPT_WRITEFUNCTION and no user fn
3+
--EXTENSIONS--
4+
curl
5+
--FILE--
6+
<?php
7+
$log_file = tempnam(sys_get_temp_dir(), 'php-curl-CURLOPT_WRITEFUNCTION-trampoline');
8+
$fp = fopen($log_file, 'w+');
9+
fwrite($fp, "test");
10+
$ch = curl_init();
11+
curl_setopt($ch, CURLOPT_WRITEFUNCTION, null);
12+
curl_setopt($ch, CURLOPT_URL, 'file://' . $log_file);
13+
curl_exec($ch);
14+
?>
15+
--EXPECT--
16+
test

0 commit comments

Comments
 (0)