Promote warnings to exceptions in ext/curl

Closes GH-5963
This commit is contained in:
Máté Kocsis 2020-08-09 11:05:26 +02:00
parent 124260c165
commit 517c9938af
No known key found for this signature in database
GPG key ID: FD055E41728BF310
12 changed files with 104 additions and 54 deletions

View file

@ -104,7 +104,7 @@ static int php_curl_option_str(php_curl *ch, zend_long option, const char *str,
CURLcode error = CURLE_OK; CURLcode error = CURLE_OK;
if (strlen(str) != len) { if (strlen(str) != len) {
php_error_docref(NULL, E_WARNING, "Curl option contains invalid characters (\\0)"); zend_type_error("%s(): cURL option cannot contain any null-bytes", get_active_function_name());
return FAILURE; return FAILURE;
} }
@ -2204,7 +2204,7 @@ PHP_FUNCTION(curl_copy_handle)
} }
/* }}} */ /* }}} */
static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{ */ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue, bool is_array_config) /* {{{ */
{ {
CURLcode error = CURLE_OK; CURLcode error = CURLE_OK;
zend_long lval; zend_long lval;
@ -2381,7 +2381,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
break; break;
case CURLOPT_SAFE_UPLOAD: case CURLOPT_SAFE_UPLOAD:
if (!zend_is_true(zvalue)) { if (!zend_is_true(zvalue)) {
php_error_docref(NULL, E_WARNING, "Disabling safe uploads is no longer supported"); zend_value_error("%s(): Disabling safe uploads is no longer supported", get_active_function_name());
return FAILURE; return FAILURE;
} }
break; break;
@ -2557,7 +2557,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
ch->handlers->write->method = PHP_CURL_FILE; ch->handlers->write->method = PHP_CURL_FILE;
ZVAL_COPY(&ch->handlers->write->stream, zvalue); ZVAL_COPY(&ch->handlers->write->stream, zvalue);
} else { } else {
php_error_docref(NULL, E_WARNING, "The provided file handle is not writable"); zend_value_error("%s(): The provided file handle must be writable", get_active_function_name());
return FAILURE; return FAILURE;
} }
break; break;
@ -2575,7 +2575,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
ch->handlers->write_header->method = PHP_CURL_FILE; ch->handlers->write_header->method = PHP_CURL_FILE;
ZVAL_COPY(&ch->handlers->write_header->stream, zvalue); ZVAL_COPY(&ch->handlers->write_header->stream, zvalue);
} else { } else {
php_error_docref(NULL, E_WARNING, "The provided file handle is not writable"); zend_value_error("%s(): The provided file handle must be writable", get_active_function_name());
return FAILURE; return FAILURE;
} }
break; break;
@ -2604,7 +2604,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
zval_ptr_dtor(&ch->handlers->std_err); zval_ptr_dtor(&ch->handlers->std_err);
ZVAL_COPY(&ch->handlers->std_err, zvalue); ZVAL_COPY(&ch->handlers->std_err, zvalue);
} else { } else {
php_error_docref(NULL, E_WARNING, "The provided file handle is not writable"); zend_value_error("%s(): The provided file handle must be writable", get_active_function_name());
return FAILURE; return FAILURE;
} }
/* break omitted intentionally */ /* break omitted intentionally */
@ -2674,7 +2674,8 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
break; break;
#endif #endif
} }
php_error_docref(NULL, E_WARNING, "You must pass an array with the %s argument", name);
zend_type_error("%s(): The %s option must have an array value", get_active_function_name(), name);
return FAILURE; return FAILURE;
} }
@ -2850,6 +2851,14 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
ch->handlers->fnmatch->method = PHP_CURL_USER; ch->handlers->fnmatch->method = PHP_CURL_USER;
break; break;
default:
if (is_array_config) {
zend_argument_value_error(2, "must contain only valid cURL options");
} else {
zend_argument_value_error(2, "is not a valid cURL option");
}
error = CURLE_UNKNOWN_OPTION;
break;
} }
SAVE_CURL_ERROR(ch, error); SAVE_CURL_ERROR(ch, error);
@ -2876,12 +2885,7 @@ PHP_FUNCTION(curl_setopt)
ch = Z_CURL_P(zid); ch = Z_CURL_P(zid);
if (options <= 0 && options != CURLOPT_SAFE_UPLOAD) { if (_php_curl_setopt(ch, options, zvalue, 0) == SUCCESS) {
php_error_docref(NULL, E_WARNING, "Invalid curl configuration option");
RETURN_FALSE;
}
if (_php_curl_setopt(ch, options, zvalue) == SUCCESS) {
RETURN_TRUE; RETURN_TRUE;
} else { } else {
RETURN_FALSE; RETURN_FALSE;
@ -2906,12 +2910,12 @@ PHP_FUNCTION(curl_setopt_array)
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arr), option, string_key, entry) { ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arr), option, string_key, entry) {
if (string_key) { if (string_key) {
php_error_docref(NULL, E_WARNING, zend_argument_value_error(2, "contains an invalid cURL option");
"Array keys must be CURLOPT constants or equivalent integer values"); RETURN_THROWS();
RETURN_FALSE;
} }
ZVAL_DEREF(entry); ZVAL_DEREF(entry);
if (_php_curl_setopt(ch, (zend_long) option, entry) == FAILURE) { if (_php_curl_setopt(ch, (zend_long) option, entry, 1) == FAILURE) {
RETURN_FALSE; RETURN_FALSE;
} }
} ZEND_HASH_FOREACH_END(); } ZEND_HASH_FOREACH_END();
@ -3292,8 +3296,8 @@ PHP_FUNCTION(curl_close)
ch = Z_CURL_P(zid); ch = Z_CURL_P(zid);
if (ch->in_callback) { if (ch->in_callback) {
php_error_docref(NULL, E_WARNING, "Attempt to close cURL handle from a callback"); zend_throw_error(NULL, "%s(): Attempt to close cURL handle from a callback", get_active_function_name());
return; RETURN_THROWS();
} }
} }
/* }}} */ /* }}} */
@ -3449,8 +3453,8 @@ PHP_FUNCTION(curl_reset)
ch = Z_CURL_P(zid); ch = Z_CURL_P(zid);
if (ch->in_callback) { if (ch->in_callback) {
php_error_docref(NULL, E_WARNING, "Attempt to reset cURL handle from a callback"); zend_throw_error(NULL, "%s(): Attempt to reset cURL handle from a callback", get_active_function_name());
return; RETURN_THROWS();
} }
curl_easy_reset(ch->cp); curl_easy_reset(ch->cp);

View file

@ -477,7 +477,7 @@ static int _php_curl_multi_setopt(php_curlm *mh, zend_long option, zval *zvalue,
break; break;
#endif #endif
default: default:
php_error_docref(NULL, E_WARNING, "Invalid curl multi configuration option"); zend_argument_value_error(2, "is not a valid cURL multi option");
error = CURLM_UNKNOWN_OPTION; error = CURLM_UNKNOWN_OPTION;
break; break;
} }

View file

@ -67,7 +67,7 @@ static int _php_curl_share_setopt(php_curlsh *sh, zend_long option, zval *zvalue
break; break;
default: default:
php_error_docref(NULL, E_WARNING, "Invalid curl share configuration option"); zend_argument_value_error(2, "is not a valid cURL share option");
error = CURLSHE_BAD_OPTION; error = CURLSHE_BAD_OPTION;
break; break;
} }

View file

@ -33,16 +33,21 @@ if(!empty($host)) {
$tempfile = tempnam(sys_get_temp_dir(), 'CURL_FILE_HANDLE'); $tempfile = tempnam(sys_get_temp_dir(), 'CURL_FILE_HANDLE');
$fp = fopen($tempfile, "r"); // Opening 'fubar' with the incorrect readonly flag
$ch = curl_init($url); $ch = curl_init($url);
$fp = fopen($tempfile, "r"); // Opening 'fubar' with the incorrect readonly flag try {
curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FILE, $fp);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
curl_exec($ch); curl_exec($ch);
curl_close($ch); curl_close($ch);
is_file($tempfile) and @unlink($tempfile); is_file($tempfile) and @unlink($tempfile);
isset($tempname) and is_file($tempname) and @unlink($tempname); isset($tempname) and is_file($tempname) and @unlink($tempname);
?> ?>
--EXPECTF-- --EXPECT--
Warning: curl_setopt(): The provided file handle is not writable in %s on line %d curl_setopt(): The provided file handle must be writable
Hello World! Hello World!
Hello World! Hello World!

View file

@ -9,10 +9,15 @@ include 'skipif.inc';
<?php <?php
$url = "file:///etc/passwd\0http://google.com"; $url = "file:///etc/passwd\0http://google.com";
$ch = curl_init(); $ch = curl_init();
var_dump(curl_setopt($ch, CURLOPT_URL, $url));
try {
curl_setopt($ch, CURLOPT_URL, $url);
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}
?> ?>
Done Done
--EXPECTF-- --EXPECT--
Warning: curl_setopt(): Curl option contains invalid characters (\0) in %s%ebug68089.php on line 4 curl_setopt(): cURL option cannot contain any null-bytes
bool(false)
Done Done

View file

@ -42,7 +42,12 @@ var_dump($file->getPostFilename());
curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file)); curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
var_dump(curl_exec($ch)); var_dump(curl_exec($ch));
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 0); try {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 0);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
$params = array('file' => '@' . __DIR__ . '/curl_testdata1.txt'); $params = array('file' => '@' . __DIR__ . '/curl_testdata1.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
var_dump(curl_exec($ch)); var_dump(curl_exec($ch));
@ -69,8 +74,7 @@ string(%d) "%s/curl_testdata1.txt"
string(%d) "curl_testdata1.txt|text/plain|6" string(%d) "curl_testdata1.txt|text/plain|6"
string(%d) "foo.txt" string(%d) "foo.txt"
string(%d) "foo.txt|application/octet-stream|6" string(%d) "foo.txt|application/octet-stream|6"
curl_setopt(): Disabling safe uploads is no longer supported
Warning: curl_setopt(): Disabling safe uploads is no longer supported in %s on line %d
string(0) "" string(0) ""
string(0) "" string(0) ""
string(%d) "array(1) { string(%d) "array(1) {

View file

@ -14,7 +14,12 @@ $errno = curl_multi_errno($mh);
echo $errno . PHP_EOL; echo $errno . PHP_EOL;
echo curl_multi_strerror($errno) . PHP_EOL; echo curl_multi_strerror($errno) . PHP_EOL;
@curl_multi_setopt($mh, -1, -1); try {
curl_multi_setopt($mh, -1, -1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
$errno = curl_multi_errno($mh); $errno = curl_multi_errno($mh);
echo $errno . PHP_EOL; echo $errno . PHP_EOL;
echo curl_multi_strerror($errno) . PHP_EOL; echo curl_multi_strerror($errno) . PHP_EOL;
@ -22,5 +27,6 @@ echo curl_multi_strerror($errno) . PHP_EOL;
--EXPECT-- --EXPECT--
0 0
No error No error
curl_multi_setopt(): Argument #2 ($option) is not a valid cURL multi option
6 6
Unknown option Unknown option

View file

@ -11,11 +11,14 @@ if (!extension_loaded("curl")) {
$mh = curl_multi_init(); $mh = curl_multi_init();
var_dump(curl_multi_setopt($mh, CURLMOPT_PIPELINING, 0)); var_dump(curl_multi_setopt($mh, CURLMOPT_PIPELINING, 0));
var_dump(curl_multi_setopt($mh, -1, 0));
try {
curl_multi_setopt($mh, -1, 0);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
?> ?>
--EXPECTF-- --EXPECT--
bool(true) bool(true)
curl_multi_setopt(): Argument #2 ($option) is not a valid cURL multi option
Warning: curl_multi_setopt(): Invalid curl multi configuration option in %s on line %d
bool(false)

View file

@ -17,7 +17,11 @@ echo "*** curl_setopt() call with CURLOPT_HTTPHEADER\n";
$url = "{$host}/"; $url = "{$host}/";
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, 1); try {
curl_setopt($ch, CURLOPT_HTTPHEADER, 1);
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}
$curl_content = curl_exec($ch); $curl_content = curl_exec($ch);
curl_close($ch); curl_close($ch);
@ -36,9 +40,8 @@ curl_close($ch);
var_dump( $curl_content ); var_dump( $curl_content );
?> ?>
--EXPECTF-- --EXPECT--
*** curl_setopt() call with CURLOPT_HTTPHEADER *** curl_setopt() call with CURLOPT_HTTPHEADER
curl_setopt(): The CURLOPT_HTTPHEADER option must have an array value
Warning: curl_setopt(): You must pass an array with the CURLOPT_HTTPHEADER argument in %s on line %d
bool(false) bool(false)
bool(true) bool(true)

View file

@ -16,10 +16,21 @@ try {
echo $e->getMessage(), "\n"; echo $e->getMessage(), "\n";
} }
curl_setopt($ch, -10, 0); try {
curl_setopt($ch, -10, 0);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
curl_setopt($ch, 1000, 0);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
?> ?>
--EXPECTF-- --EXPECT--
*** curl_setopt() call with incorrect parameters *** curl_setopt() call with incorrect parameters
curl_setopt(): Argument #2 ($option) must be of type int, string given curl_setopt(): Argument #2 ($option) must be of type int, string given
curl_setopt(): Argument #2 ($option) is not a valid cURL option
Warning: curl_setopt(): Invalid curl configuration option in %scurl_setopt_error.php on line %d curl_setopt(): Argument #2 ($option) is not a valid cURL option

View file

@ -14,7 +14,12 @@ $errno = curl_share_errno($sh);
echo $errno . PHP_EOL; echo $errno . PHP_EOL;
echo curl_share_strerror($errno) . PHP_EOL; echo curl_share_strerror($errno) . PHP_EOL;
@curl_share_setopt($sh, -1, -1); try {
curl_share_setopt($sh, -1, -1);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
$errno = curl_share_errno($sh); $errno = curl_share_errno($sh);
echo $errno . PHP_EOL; echo $errno . PHP_EOL;
echo curl_share_strerror($errno) . PHP_EOL; echo curl_share_strerror($errno) . PHP_EOL;
@ -22,5 +27,6 @@ echo curl_share_strerror($errno) . PHP_EOL;
--EXPECT-- --EXPECT--
0 0
No error No error
curl_share_setopt(): Argument #2 ($option) is not a valid cURL share option
1 1
Unknown share option Unknown share option

View file

@ -12,12 +12,15 @@ if (!extension_loaded("curl")) {
$sh = curl_share_init(); $sh = curl_share_init();
var_dump(curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE)); var_dump(curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE));
var_dump(curl_share_setopt($sh, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_DNS)); var_dump(curl_share_setopt($sh, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_DNS));
var_dump(curl_share_setopt($sh, -1, 0));
try {
curl_share_setopt($sh, -1, 0);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
?> ?>
--EXPECTF-- --EXPECT--
bool(true) bool(true)
bool(true) bool(true)
curl_share_setopt(): Argument #2 ($option) is not a valid cURL share option
Warning: curl_share_setopt(): Invalid curl share configuration option in %s on line %d
bool(false)