Skip to content

Commit 517c993

Browse files
committed
Promote warnings to exceptions in ext/curl
Closes GH-5963
1 parent 124260c commit 517c993

12 files changed

+104
-54
lines changed

ext/curl/interface.c

+25-21
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static int php_curl_option_str(php_curl *ch, zend_long option, const char *str,
104104
CURLcode error = CURLE_OK;
105105

106106
if (strlen(str) != len) {
107-
php_error_docref(NULL, E_WARNING, "Curl option contains invalid characters (\\0)");
107+
zend_type_error("%s(): cURL option cannot contain any null-bytes", get_active_function_name());
108108
return FAILURE;
109109
}
110110

@@ -2204,7 +2204,7 @@ PHP_FUNCTION(curl_copy_handle)
22042204
}
22052205
/* }}} */
22062206

2207-
static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{ */
2207+
static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue, bool is_array_config) /* {{{ */
22082208
{
22092209
CURLcode error = CURLE_OK;
22102210
zend_long lval;
@@ -2381,7 +2381,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
23812381
break;
23822382
case CURLOPT_SAFE_UPLOAD:
23832383
if (!zend_is_true(zvalue)) {
2384-
php_error_docref(NULL, E_WARNING, "Disabling safe uploads is no longer supported");
2384+
zend_value_error("%s(): Disabling safe uploads is no longer supported", get_active_function_name());
23852385
return FAILURE;
23862386
}
23872387
break;
@@ -2557,7 +2557,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
25572557
ch->handlers->write->method = PHP_CURL_FILE;
25582558
ZVAL_COPY(&ch->handlers->write->stream, zvalue);
25592559
} else {
2560-
php_error_docref(NULL, E_WARNING, "The provided file handle is not writable");
2560+
zend_value_error("%s(): The provided file handle must be writable", get_active_function_name());
25612561
return FAILURE;
25622562
}
25632563
break;
@@ -2575,7 +2575,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
25752575
ch->handlers->write_header->method = PHP_CURL_FILE;
25762576
ZVAL_COPY(&ch->handlers->write_header->stream, zvalue);
25772577
} else {
2578-
php_error_docref(NULL, E_WARNING, "The provided file handle is not writable");
2578+
zend_value_error("%s(): The provided file handle must be writable", get_active_function_name());
25792579
return FAILURE;
25802580
}
25812581
break;
@@ -2604,7 +2604,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
26042604
zval_ptr_dtor(&ch->handlers->std_err);
26052605
ZVAL_COPY(&ch->handlers->std_err, zvalue);
26062606
} else {
2607-
php_error_docref(NULL, E_WARNING, "The provided file handle is not writable");
2607+
zend_value_error("%s(): The provided file handle must be writable", get_active_function_name());
26082608
return FAILURE;
26092609
}
26102610
/* break omitted intentionally */
@@ -2674,7 +2674,8 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
26742674
break;
26752675
#endif
26762676
}
2677-
php_error_docref(NULL, E_WARNING, "You must pass an array with the %s argument", name);
2677+
2678+
zend_type_error("%s(): The %s option must have an array value", get_active_function_name(), name);
26782679
return FAILURE;
26792680
}
26802681

@@ -2850,6 +2851,14 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
28502851
ch->handlers->fnmatch->method = PHP_CURL_USER;
28512852
break;
28522853

2854+
default:
2855+
if (is_array_config) {
2856+
zend_argument_value_error(2, "must contain only valid cURL options");
2857+
} else {
2858+
zend_argument_value_error(2, "is not a valid cURL option");
2859+
}
2860+
error = CURLE_UNKNOWN_OPTION;
2861+
break;
28532862
}
28542863

28552864
SAVE_CURL_ERROR(ch, error);
@@ -2876,12 +2885,7 @@ PHP_FUNCTION(curl_setopt)
28762885

28772886
ch = Z_CURL_P(zid);
28782887

2879-
if (options <= 0 && options != CURLOPT_SAFE_UPLOAD) {
2880-
php_error_docref(NULL, E_WARNING, "Invalid curl configuration option");
2881-
RETURN_FALSE;
2882-
}
2883-
2884-
if (_php_curl_setopt(ch, options, zvalue) == SUCCESS) {
2888+
if (_php_curl_setopt(ch, options, zvalue, 0) == SUCCESS) {
28852889
RETURN_TRUE;
28862890
} else {
28872891
RETURN_FALSE;
@@ -2906,12 +2910,12 @@ PHP_FUNCTION(curl_setopt_array)
29062910

29072911
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arr), option, string_key, entry) {
29082912
if (string_key) {
2909-
php_error_docref(NULL, E_WARNING,
2910-
"Array keys must be CURLOPT constants or equivalent integer values");
2911-
RETURN_FALSE;
2913+
zend_argument_value_error(2, "contains an invalid cURL option");
2914+
RETURN_THROWS();
29122915
}
2916+
29132917
ZVAL_DEREF(entry);
2914-
if (_php_curl_setopt(ch, (zend_long) option, entry) == FAILURE) {
2918+
if (_php_curl_setopt(ch, (zend_long) option, entry, 1) == FAILURE) {
29152919
RETURN_FALSE;
29162920
}
29172921
} ZEND_HASH_FOREACH_END();
@@ -3292,8 +3296,8 @@ PHP_FUNCTION(curl_close)
32923296
ch = Z_CURL_P(zid);
32933297

32943298
if (ch->in_callback) {
3295-
php_error_docref(NULL, E_WARNING, "Attempt to close cURL handle from a callback");
3296-
return;
3299+
zend_throw_error(NULL, "%s(): Attempt to close cURL handle from a callback", get_active_function_name());
3300+
RETURN_THROWS();
32973301
}
32983302
}
32993303
/* }}} */
@@ -3449,8 +3453,8 @@ PHP_FUNCTION(curl_reset)
34493453
ch = Z_CURL_P(zid);
34503454

34513455
if (ch->in_callback) {
3452-
php_error_docref(NULL, E_WARNING, "Attempt to reset cURL handle from a callback");
3453-
return;
3456+
zend_throw_error(NULL, "%s(): Attempt to reset cURL handle from a callback", get_active_function_name());
3457+
RETURN_THROWS();
34543458
}
34553459

34563460
curl_easy_reset(ch->cp);

ext/curl/multi.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ static int _php_curl_multi_setopt(php_curlm *mh, zend_long option, zval *zvalue,
477477
break;
478478
#endif
479479
default:
480-
php_error_docref(NULL, E_WARNING, "Invalid curl multi configuration option");
480+
zend_argument_value_error(2, "is not a valid cURL multi option");
481481
error = CURLM_UNKNOWN_OPTION;
482482
break;
483483
}

ext/curl/share.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static int _php_curl_share_setopt(php_curlsh *sh, zend_long option, zval *zvalue
6767
break;
6868

6969
default:
70-
php_error_docref(NULL, E_WARNING, "Invalid curl share configuration option");
70+
zend_argument_value_error(2, "is not a valid cURL share option");
7171
error = CURLSHE_BAD_OPTION;
7272
break;
7373
}

ext/curl/tests/bug48207.phpt

+9-4
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,21 @@ if(!empty($host)) {
3333

3434

3535
$tempfile = tempnam(sys_get_temp_dir(), 'CURL_FILE_HANDLE');
36+
$fp = fopen($tempfile, "r"); // Opening 'fubar' with the incorrect readonly flag
3637

3738
$ch = curl_init($url);
38-
$fp = fopen($tempfile, "r"); // Opening 'fubar' with the incorrect readonly flag
39-
curl_setopt($ch, CURLOPT_FILE, $fp);
39+
try {
40+
curl_setopt($ch, CURLOPT_FILE, $fp);
41+
} catch (ValueError $exception) {
42+
echo $exception->getMessage() . "\n";
43+
}
44+
4045
curl_exec($ch);
4146
curl_close($ch);
4247
is_file($tempfile) and @unlink($tempfile);
4348
isset($tempname) and is_file($tempname) and @unlink($tempname);
4449
?>
45-
--EXPECTF--
46-
Warning: curl_setopt(): The provided file handle is not writable in %s on line %d
50+
--EXPECT--
51+
curl_setopt(): The provided file handle must be writable
4752
Hello World!
4853
Hello World!

ext/curl/tests/bug68089.phpt

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

ext/curl/tests/curl_file_upload.phpt

+7-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ var_dump($file->getPostFilename());
4242
curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
4343
var_dump(curl_exec($ch));
4444

45-
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 0);
45+
try {
46+
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 0);
47+
} catch (ValueError $exception) {
48+
echo $exception->getMessage() . "\n";
49+
}
50+
4651
$params = array('file' => '@' . __DIR__ . '/curl_testdata1.txt');
4752
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
4853
var_dump(curl_exec($ch));
@@ -69,8 +74,7 @@ string(%d) "%s/curl_testdata1.txt"
6974
string(%d) "curl_testdata1.txt|text/plain|6"
7075
string(%d) "foo.txt"
7176
string(%d) "foo.txt|application/octet-stream|6"
72-
73-
Warning: curl_setopt(): Disabling safe uploads is no longer supported in %s on line %d
77+
curl_setopt(): Disabling safe uploads is no longer supported
7478
string(0) ""
7579
string(0) ""
7680
string(%d) "array(1) {

ext/curl/tests/curl_multi_errno_strerror_001.phpt

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ $errno = curl_multi_errno($mh);
1414
echo $errno . PHP_EOL;
1515
echo curl_multi_strerror($errno) . PHP_EOL;
1616

17-
@curl_multi_setopt($mh, -1, -1);
17+
try {
18+
curl_multi_setopt($mh, -1, -1);
19+
} catch (ValueError $exception) {
20+
echo $exception->getMessage() . "\n";
21+
}
22+
1823
$errno = curl_multi_errno($mh);
1924
echo $errno . PHP_EOL;
2025
echo curl_multi_strerror($errno) . PHP_EOL;
2126
?>
2227
--EXPECT--
2328
0
2429
No error
30+
curl_multi_setopt(): Argument #2 ($option) is not a valid cURL multi option
2531
6
2632
Unknown option

ext/curl/tests/curl_multi_setopt_basic001.phpt

+8-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ if (!extension_loaded("curl")) {
1111

1212
$mh = curl_multi_init();
1313
var_dump(curl_multi_setopt($mh, CURLMOPT_PIPELINING, 0));
14-
var_dump(curl_multi_setopt($mh, -1, 0));
14+
15+
try {
16+
curl_multi_setopt($mh, -1, 0);
17+
} catch (ValueError $exception) {
18+
echo $exception->getMessage() . "\n";
19+
}
1520

1621
?>
17-
--EXPECTF--
22+
--EXPECT--
1823
bool(true)
19-
20-
Warning: curl_multi_setopt(): Invalid curl multi configuration option in %s on line %d
21-
bool(false)
24+
curl_multi_setopt(): Argument #2 ($option) is not a valid cURL multi option

ext/curl/tests/curl_setopt_basic003.phpt

+7-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ echo "*** curl_setopt() call with CURLOPT_HTTPHEADER\n";
1717
$url = "{$host}/";
1818
$ch = curl_init();
1919

20-
curl_setopt($ch, CURLOPT_HTTPHEADER, 1);
20+
try {
21+
curl_setopt($ch, CURLOPT_HTTPHEADER, 1);
22+
} catch (TypeError $exception) {
23+
echo $exception->getMessage() . "\n";
24+
}
2125

2226
$curl_content = curl_exec($ch);
2327
curl_close($ch);
@@ -36,9 +40,8 @@ curl_close($ch);
3640

3741
var_dump( $curl_content );
3842
?>
39-
--EXPECTF--
43+
--EXPECT--
4044
*** curl_setopt() call with CURLOPT_HTTPHEADER
41-
42-
Warning: curl_setopt(): You must pass an array with the CURLOPT_HTTPHEADER argument in %s on line %d
45+
curl_setopt(): The CURLOPT_HTTPHEADER option must have an array value
4346
bool(false)
4447
bool(true)

ext/curl/tests/curl_setopt_error.phpt

+15-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,21 @@ try {
1616
echo $e->getMessage(), "\n";
1717
}
1818

19-
curl_setopt($ch, -10, 0);
19+
try {
20+
curl_setopt($ch, -10, 0);
21+
} catch (ValueError $e) {
22+
echo $e->getMessage(), "\n";
23+
}
24+
25+
try {
26+
curl_setopt($ch, 1000, 0);
27+
} catch (ValueError $e) {
28+
echo $e->getMessage(), "\n";
29+
}
30+
2031
?>
21-
--EXPECTF--
32+
--EXPECT--
2233
*** curl_setopt() call with incorrect parameters
2334
curl_setopt(): Argument #2 ($option) must be of type int, string given
24-
25-
Warning: curl_setopt(): Invalid curl configuration option in %scurl_setopt_error.php on line %d
35+
curl_setopt(): Argument #2 ($option) is not a valid cURL option
36+
curl_setopt(): Argument #2 ($option) is not a valid cURL option

ext/curl/tests/curl_share_errno_strerror_001.phpt

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ $errno = curl_share_errno($sh);
1414
echo $errno . PHP_EOL;
1515
echo curl_share_strerror($errno) . PHP_EOL;
1616

17-
@curl_share_setopt($sh, -1, -1);
17+
try {
18+
curl_share_setopt($sh, -1, -1);
19+
} catch (ValueError $e) {
20+
echo $e->getMessage(), "\n";
21+
}
22+
1823
$errno = curl_share_errno($sh);
1924
echo $errno . PHP_EOL;
2025
echo curl_share_strerror($errno) . PHP_EOL;
2126
?>
2227
--EXPECT--
2328
0
2429
No error
30+
curl_share_setopt(): Argument #2 ($option) is not a valid cURL share option
2531
1
2632
Unknown share option

ext/curl/tests/curl_share_setopt_basic001.phpt

+8-5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ if (!extension_loaded("curl")) {
1212
$sh = curl_share_init();
1313
var_dump(curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE));
1414
var_dump(curl_share_setopt($sh, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_DNS));
15-
var_dump(curl_share_setopt($sh, -1, 0));
15+
16+
try {
17+
curl_share_setopt($sh, -1, 0);
18+
} catch (ValueError $e) {
19+
echo $e->getMessage(), "\n";
20+
}
1621

1722
?>
18-
--EXPECTF--
23+
--EXPECT--
1924
bool(true)
2025
bool(true)
21-
22-
Warning: curl_share_setopt(): Invalid curl share configuration option in %s on line %d
23-
bool(false)
26+
curl_share_setopt(): Argument #2 ($option) is not a valid cURL share option

0 commit comments

Comments
 (0)