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;
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;
}
@ -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;
zend_long lval;
@ -2381,7 +2381,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
break;
case CURLOPT_SAFE_UPLOAD:
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;
}
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;
ZVAL_COPY(&ch->handlers->write->stream, zvalue);
} 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;
}
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;
ZVAL_COPY(&ch->handlers->write_header->stream, zvalue);
} 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;
}
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_COPY(&ch->handlers->std_err, zvalue);
} 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;
}
/* break omitted intentionally */
@ -2674,7 +2674,8 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
break;
#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;
}
@ -2850,6 +2851,14 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
ch->handlers->fnmatch->method = PHP_CURL_USER;
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);
@ -2876,12 +2885,7 @@ PHP_FUNCTION(curl_setopt)
ch = Z_CURL_P(zid);
if (options <= 0 && options != CURLOPT_SAFE_UPLOAD) {
php_error_docref(NULL, E_WARNING, "Invalid curl configuration option");
RETURN_FALSE;
}
if (_php_curl_setopt(ch, options, zvalue) == SUCCESS) {
if (_php_curl_setopt(ch, options, zvalue, 0) == SUCCESS) {
RETURN_TRUE;
} else {
RETURN_FALSE;
@ -2906,12 +2910,12 @@ PHP_FUNCTION(curl_setopt_array)
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arr), option, string_key, entry) {
if (string_key) {
php_error_docref(NULL, E_WARNING,
"Array keys must be CURLOPT constants or equivalent integer values");
RETURN_FALSE;
zend_argument_value_error(2, "contains an invalid cURL option");
RETURN_THROWS();
}
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;
}
} ZEND_HASH_FOREACH_END();
@ -3292,8 +3296,8 @@ PHP_FUNCTION(curl_close)
ch = Z_CURL_P(zid);
if (ch->in_callback) {
php_error_docref(NULL, E_WARNING, "Attempt to close cURL handle from a callback");
return;
zend_throw_error(NULL, "%s(): Attempt to close cURL handle from a callback", get_active_function_name());
RETURN_THROWS();
}
}
/* }}} */
@ -3449,8 +3453,8 @@ PHP_FUNCTION(curl_reset)
ch = Z_CURL_P(zid);
if (ch->in_callback) {
php_error_docref(NULL, E_WARNING, "Attempt to reset cURL handle from a callback");
return;
zend_throw_error(NULL, "%s(): Attempt to reset cURL handle from a callback", get_active_function_name());
RETURN_THROWS();
}
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;
#endif
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;
break;
}

View file

@ -67,7 +67,7 @@ static int _php_curl_share_setopt(php_curlsh *sh, zend_long option, zval *zvalue
break;
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;
break;
}

View file

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

View file

@ -9,10 +9,15 @@ include 'skipif.inc';
<?php
$url = "file:///etc/passwd\0http://google.com";
$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
--EXPECTF--
Warning: curl_setopt(): Curl option contains invalid characters (\0) in %s%ebug68089.php on line 4
bool(false)
--EXPECT--
curl_setopt(): cURL option cannot contain any null-bytes
Done

View file

@ -42,7 +42,12 @@ var_dump($file->getPostFilename());
curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
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');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
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) "foo.txt"
string(%d) "foo.txt|application/octet-stream|6"
Warning: curl_setopt(): Disabling safe uploads is no longer supported in %s on line %d
curl_setopt(): Disabling safe uploads is no longer supported
string(0) ""
string(0) ""
string(%d) "array(1) {

View file

@ -14,7 +14,12 @@ $errno = curl_multi_errno($mh);
echo $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);
echo $errno . PHP_EOL;
echo curl_multi_strerror($errno) . PHP_EOL;
@ -22,5 +27,6 @@ echo curl_multi_strerror($errno) . PHP_EOL;
--EXPECT--
0
No error
curl_multi_setopt(): Argument #2 ($option) is not a valid cURL multi option
6
Unknown option

View file

@ -11,11 +11,14 @@ if (!extension_loaded("curl")) {
$mh = curl_multi_init();
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)
Warning: curl_multi_setopt(): Invalid curl multi configuration option in %s on line %d
bool(false)
curl_multi_setopt(): Argument #2 ($option) is not a valid cURL multi option

View file

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

View file

@ -16,10 +16,21 @@ try {
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(): Argument #2 ($option) must be of type int, string given
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
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 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);
echo $errno . PHP_EOL;
echo curl_share_strerror($errno) . PHP_EOL;
@ -22,5 +27,6 @@ echo curl_share_strerror($errno) . PHP_EOL;
--EXPECT--
0
No error
curl_share_setopt(): Argument #2 ($option) is not a valid cURL share option
1
Unknown share option

View file

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