Skip to content

Commit 83748ac

Browse files
author
Jani Taskinen
committed
MFH: Fixed bug #48203 (crash when CURLOPT_STDERR is set to regular file)
1 parent f10d399 commit 83748ac

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

ext/curl/interface.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ PHP_MINIT_FUNCTION(curl)
459459
le_curl_multi_handle = zend_register_list_destructors_ex(_php_curl_multi_close, NULL, "curl", module_number);
460460

461461
/* See http://curl.haxx.se/lxr/source/docs/libcurl/symbols-in-versions
462-
or curl src/docs/libcurl/symbols-in-versions for a (almost) complete list
462+
or curl src/docs/libcurl/symbols-in-versions for a (almost) complete list
463463
of options and which version they were introduced */
464464

465465
/* Constants for curl_setopt() */
@@ -1673,6 +1673,20 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue, zval *retu
16731673
ch->handlers->read->fp = fp;
16741674
ch->handlers->read->fd = Z_LVAL_PP(zvalue);
16751675
break;
1676+
case CURLOPT_STDERR:
1677+
if (((php_stream *) what)->mode[0] != 'r') {
1678+
if (ch->handlers->stderr) {
1679+
zval_ptr_dtor(&ch->handlers->stderr);
1680+
}
1681+
zval_add_ref(zvalue);
1682+
ch->handlers->stderr = *zvalue;
1683+
zend_list_addref(Z_LVAL_PP(zvalue));
1684+
} else {
1685+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
1686+
RETVAL_FALSE;
1687+
return 1;
1688+
}
1689+
/* break omitted intentionally */
16761690
default:
16771691
error = curl_easy_setopt(ch->cp, option, fp);
16781692
break;
@@ -2268,6 +2282,11 @@ static void _php_curl_close_ex(php_curl *ch TSRMLS_DC)
22682282
fprintf(stderr, "DTOR CALLED, ch = %x\n", ch);
22692283
#endif
22702284

2285+
/* Prevent crash inside cURL if passed file has already been closed */
2286+
if (ch->handlers->stderr && Z_REFCOUNT_P(ch->handlers->stderr) <= 0) {
2287+
curl_easy_setopt(ch->cp, CURLOPT_STDERR, stderr);
2288+
}
2289+
22712290
curl_easy_cleanup(ch->cp);
22722291
#if LIBCURL_VERSION_NUM < 0x071101
22732292
zend_llist_clean(&ch->to_free.str);
@@ -2287,12 +2306,15 @@ static void _php_curl_close_ex(php_curl *ch TSRMLS_DC)
22872306
if (ch->handlers->write_header->func_name) {
22882307
zval_ptr_dtor(&ch->handlers->write_header->func_name);
22892308
}
2290-
if(ch->handlers->progress->func_name) {
2309+
if (ch->handlers->progress->func_name) {
22912310
zval_ptr_dtor(&ch->handlers->progress->func_name);
22922311
}
22932312
if (ch->handlers->passwd) {
22942313
zval_ptr_dtor(&ch->handlers->passwd);
22952314
}
2315+
if (ch->handlers->stderr) {
2316+
zval_ptr_dtor(&ch->handlers->stderr);
2317+
}
22962318
if (ch->header.str_len > 0) {
22972319
efree(ch->header.str);
22982320
}

ext/curl/php_curl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ typedef struct {
107107
php_curl_write *write_header;
108108
php_curl_read *read;
109109
zval *passwd;
110+
zval *stderr;
110111
php_curl_progress *progress;
111112
} php_curl_handlers;
112113

0 commit comments

Comments
 (0)