Skip to content

Commit 01b266a

Browse files
committed
Improve error messages of various extensions
Closes GH-5278
1 parent d7b73de commit 01b266a

29 files changed

+100
-100
lines changed

ext/bz2/bz2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ static PHP_FUNCTION(bzopen)
379379
}
380380

381381
if (CHECK_ZVAL_NULL_PATH(file)) {
382-
zend_type_error("Filename must not contain null bytes");
382+
zend_argument_type_error(1, "must not contain null bytes");
383383
RETURN_THROWS();
384384
}
385385

@@ -430,7 +430,7 @@ static PHP_FUNCTION(bzopen)
430430

431431
stream = php_stream_bz2open_from_BZFILE(bz, mode, stream);
432432
} else {
433-
zend_type_error("First parameter has to be string or file-resource");
433+
zend_argument_type_error(1, "must be of type string or file-resource, %s given", zend_zval_type_name(file));
434434
RETURN_THROWS();
435435
}
436436

ext/bz2/tests/002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ resource(%d) of type (stream)
9191
resource(%d) of type (stream)
9292

9393
Warning: fopen(bz_open_002.txt): Failed to open stream: Bad file %s in %s on line %d
94-
First parameter has to be string or file-resource
94+
bzopen(): Argument #1 ($file) must be of type string or file-resource, bool given
9595

9696
Warning: fopen(bz_open_002.txt): Failed to open stream: Bad file %s in %s on line %d
97-
First parameter has to be string or file-resource
97+
bzopen(): Argument #1 ($file) must be of type string or file-resource, bool given
9898

9999
Warning: bzopen(): cannot write to a stream opened in read only mode in %s on line %d
100100
bool(false)

ext/bz2/tests/bzopen_string_filename_with_null_bytes.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ bzopen(): throw TypeError if filename contains null bytes
77

88
try {
99
bzopen("file\0", "w");
10-
} catch (\TypeError $e) {
10+
} catch (TypeError $e) {
1111
echo $e->getMessage() . \PHP_EOL;
1212
}
1313

1414
try {
1515
bzopen("file\0", "r");
16-
} catch (\TypeError $e) {
16+
} catch (TypeError $e) {
1717
echo $e->getMessage() . \PHP_EOL;
1818
}
1919

2020
?>
2121
--EXPECT--
22-
Filename must not contain null bytes
23-
Filename must not contain null bytes
22+
bzopen(): Argument #1 ($file) must not contain null bytes
23+
bzopen(): Argument #1 ($file) must not contain null bytes

ext/dom/parentnode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ xmlNode* dom_zvals_to_fragment(php_libxml_ref_obj *document, xmlNode *contextNod
197197
} else {
198198
xmlFree(fragment);
199199

200-
zend_type_error("Invalid argument type must be either DOMNode or string");
200+
zend_argument_type_error(i + 1, "must be of type DOMNode|string, %s given", zend_zval_type_name(&nodes[i]));
201201
return NULL;
202202
}
203203
} else if (Z_TYPE(nodes[i]) == IS_STRING) {
@@ -213,7 +213,7 @@ xmlNode* dom_zvals_to_fragment(php_libxml_ref_obj *document, xmlNode *contextNod
213213
} else {
214214
xmlFree(fragment);
215215

216-
zend_type_error("Invalid argument type must be either DOMNode or string");
216+
zend_argument_type_error(i + 1, "must be of type DOMNode|string, %s given", zend_zval_type_name(&nodes[i]));
217217

218218
return NULL;
219219
}

ext/dom/tests/DOM4_ParentNode_append_invalidtypes.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ try {
1515
echo "OK! {$e->getMessage()}";
1616
}
1717
--EXPECT--
18-
OK! Invalid argument type must be either DOMNode or string
18+
OK! DOMElement::append(): Argument #1 must be of type DOMNode|string, array given

ext/gd/gd.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3885,7 +3885,7 @@ PHP_FUNCTION(imageaffine)
38853885
src = php_gd_libgdimageptr_from_zval_p(IM);
38863886

38873887
if ((nelems = zend_hash_num_elements(Z_ARRVAL_P(z_affine))) != 6) {
3888-
zend_value_error("Affine array must have six elements");
3888+
zend_argument_value_error(2, "must have 6 elements");
38893889
RETURN_THROWS();
38903890
}
38913891

@@ -3902,7 +3902,7 @@ PHP_FUNCTION(imageaffine)
39023902
affine[i] = zval_get_double(zval_affine_elem);
39033903
break;
39043904
default:
3905-
zend_type_error("Invalid type for element %i", i);
3905+
zend_argument_type_error(3, "contains invalid type for element %i", i);
39063906
RETURN_THROWS();
39073907
}
39083908
}
@@ -3970,7 +3970,7 @@ PHP_FUNCTION(imageaffinematrixget)
39703970
case GD_AFFINE_SCALE: {
39713971
double x, y;
39723972
if (!options || Z_TYPE_P(options) != IS_ARRAY) {
3973-
zend_type_error("Array expected as options when using translate or scale");
3973+
zend_argument_type_error(1, "must be of type array when using translate or scale");
39743974
RETURN_THROWS();
39753975
}
39763976

@@ -4002,7 +4002,7 @@ PHP_FUNCTION(imageaffinematrixget)
40024002
double angle;
40034003

40044004
if (!options) {
4005-
zend_type_error("Number is expected as option when using rotate or shear");
4005+
zend_argument_type_error(2, "must be of type int|double when using rotate or shear");
40064006
RETURN_THROWS();
40074007
}
40084008

@@ -4019,7 +4019,7 @@ PHP_FUNCTION(imageaffinematrixget)
40194019
}
40204020

40214021
default:
4022-
zend_value_error("Invalid type for element " ZEND_LONG_FMT, type);
4022+
zend_argument_value_error(1, "must be a valid element type");
40234023
RETURN_THROWS();
40244024
}
40254025

@@ -4068,7 +4068,7 @@ PHP_FUNCTION(imageaffinematrixconcat)
40684068
m1[i] = zval_get_double(tmp);
40694069
break;
40704070
default:
4071-
zend_type_error("Matrix 1 contains invalid type for element %i", i);
4071+
zend_argument_type_error(1, "contains invalid type for element %i", i);
40724072
RETURN_THROWS();
40734073
}
40744074
}
@@ -4085,7 +4085,7 @@ PHP_FUNCTION(imageaffinematrixconcat)
40854085
m2[i] = zval_get_double(tmp);
40864086
break;
40874087
default:
4088-
zend_type_error("Matrix 2 contains invalid type for element %i", i);
4088+
zend_argument_type_error(2, "contains invalid type for element %i", i);
40894089
RETURN_THROWS();
40904090
}
40914091
}
@@ -4299,7 +4299,7 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
42994299
close_stream = 0;
43004300
} else if (Z_TYPE_P(to_zval) == IS_STRING) {
43014301
if (CHECK_ZVAL_NULL_PATH(to_zval)) {
4302-
zend_type_error("Invalid 2nd parameter, filename must not contain null bytes");
4302+
zend_argument_type_error(2, "must not contain null bytes");
43034303
RETURN_THROWS();
43044304
}
43054305

ext/gd/tests/bug67248.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ for($i=0;$i<7;$i++) {
1616
}
1717
?>
1818
--EXPECT--
19-
!! [TypeError] Array expected as options when using translate or scale
20-
!! [TypeError] Array expected as options when using translate or scale
21-
!! [TypeError] Number is expected as option when using rotate or shear
22-
!! [TypeError] Number is expected as option when using rotate or shear
23-
!! [TypeError] Number is expected as option when using rotate or shear
24-
!! [ValueError] Invalid type for element 5
25-
!! [ValueError] Invalid type for element 6
19+
!! [TypeError] imageaffinematrixget(): Argument #1 ($type) must be of type array when using translate or scale
20+
!! [TypeError] imageaffinematrixget(): Argument #1 ($type) must be of type array when using translate or scale
21+
!! [TypeError] imageaffinematrixget(): Argument #2 ($options) must be of type int|double when using rotate or shear
22+
!! [TypeError] imageaffinematrixget(): Argument #2 ($options) must be of type int|double when using rotate or shear
23+
!! [TypeError] imageaffinematrixget(): Argument #2 ($options) must be of type int|double when using rotate or shear
24+
!! [ValueError] imageaffinematrixget(): Argument #1 ($type) must be a valid element type
25+
!! [ValueError] imageaffinematrixget(): Argument #1 ($type) must be a valid element type

ext/gd/tests/imagebmp_nullbyte_injection.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ try {
1515
}
1616
?>
1717
--EXPECT--
18-
Invalid 2nd parameter, filename must not contain null bytes
18+
imagebmp(): Argument #2 ($to) must not contain null bytes

ext/gd/tests/imagegif_nullbyte_injection.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ try {
1414
}
1515
?>
1616
--EXPECT--
17-
Invalid 2nd parameter, filename must not contain null bytes
17+
imagegif(): Argument #2 ($to) must not contain null bytes

ext/gd/tests/imagejpeg_nullbyte_injection.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ try {
1818
}
1919
?>
2020
--EXPECT--
21-
Invalid 2nd parameter, filename must not contain null bytes
21+
imagejpeg(): Argument #2 ($to) must not contain null bytes

ext/gd/tests/imagepng_nullbyte_injection.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ try {
1818
}
1919
?>
2020
--EXPECTF--
21-
Invalid 2nd parameter, filename must not contain null bytes
21+
imagepng(): Argument #2 ($to) must not contain null bytes

ext/gd/tests/imagewbmp_nullbyte_injection.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ try {
1818
}
1919
?>
2020
--EXPECT--
21-
Invalid 2nd parameter, filename must not contain null bytes
21+
imagewbmp(): Argument #2 ($to) must not contain null bytes

ext/gd/tests/imagewebp_nullbyte_injection.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ try {
1818
}
1919
?>
2020
--EXPECT--
21-
Invalid 2nd parameter, filename must not contain null bytes
21+
imagewebp(): Argument #2 ($to) must not contain null bytes

ext/mysqli/mysqli.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ void php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAMETERS, int override_flags
12721272
* single value is an array. Also we'd have to make that one
12731273
* argument passed by reference.
12741274
*/
1275-
zend_throw_exception(zend_ce_exception, "Parameter ctor_params must be an array", 0);
1275+
zend_argument_error(zend_ce_exception, 3, "must be of type array, %s given", zend_zval_type_name(ctor_params));
12761276
RETURN_THROWS();
12771277
}
12781278
}

ext/openssl/openssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6640,7 +6640,7 @@ PHP_OPENSSL_API zend_string* php_openssl_random_pseudo_bytes(zend_long buffer_le
66406640
|| ZEND_LONG_INT_OVFL(buffer_length)
66416641
#endif
66426642
) {
6643-
zend_throw_exception(zend_ce_error, "Length must be greater than 0", 0);
6643+
zend_argument_error(NULL, 1, "must be greater than 0");
66446644
return NULL;
66456645
}
66466646
buffer = zend_string_alloc(buffer_length, 0);

ext/openssl/tests/openssl_random_pseudo_bytes_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ try {
1111
}
1212
?>
1313
--EXPECT--
14-
Length must be greater than 0
14+
openssl_random_pseudo_bytes(): Argument #1 ($length) must be greater than 0

ext/pcre/php_pcre.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,7 @@ static void preg_replace_common(INTERNAL_FUNCTION_PARAMETERS, int is_filter)
23032303
}
23042304
} else {
23052305
if (Z_TYPE_P(regex) != IS_ARRAY) {
2306-
zend_type_error("Parameter mismatch, pattern is a string while replacement is an array");
2306+
zend_argument_type_error(1, "must be of type array when argument #2 ($replace) is an array, %s given", zend_zval_type_name(regex));
23072307
RETURN_THROWS();
23082308
}
23092309
}

ext/pcre/tests/bug21732.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ try {
1717
var_dump(preg_replace_callback("/(ab)(cd)(e)/", array(new foo(), "cb"), 'abcde'));
1818
?>
1919
--EXPECT--
20-
Parameter mismatch, pattern is a string while replacement is an array
20+
preg_replace(): Argument #1 ($regex) must be of type array when argument #2 ($replace) is an array, string given
2121
array(4) {
2222
[0]=>
2323
string(5) "abcde"

ext/pcre/tests/preg_replace_error2.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ Arg value is: this is a string
3636
string(64) "this is a stringthis is a stringthis is a stringthis is a string"
3737

3838
Arg value is: Array
39-
Parameter mismatch, pattern is a string while replacement is an array
39+
preg_replace(): Argument #1 ($regex) must be of type array when argument #2 ($replace) is an array, string given
4040
Object of class stdClass could not be converted to string
4141
Done

ext/pdo/pdo_dbh.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static PHP_METHOD(PDO, dbh_constructor)
225225

226226
snprintf(alt_dsn, sizeof(alt_dsn), "pdo.dsn.%s", data_source);
227227
if (FAILURE == cfg_get_string(alt_dsn, &ini_dsn)) {
228-
zend_throw_exception_ex(php_pdo_get_exception(), 0, "invalid data source name");
228+
zend_argument_error(php_pdo_get_exception(), 1, "must be a valid data source name");
229229
RETURN_THROWS();
230230
}
231231

@@ -242,12 +242,12 @@ static PHP_METHOD(PDO, dbh_constructor)
242242
/* the specified URI holds connection details */
243243
data_source = dsn_from_uri(data_source + sizeof("uri:")-1, alt_dsn, sizeof(alt_dsn));
244244
if (!data_source) {
245-
zend_throw_exception_ex(php_pdo_get_exception(), 0, "invalid data source URI");
245+
zend_argument_error(php_pdo_get_exception(), 1, "must be a valid data source URI");
246246
RETURN_THROWS();
247247
}
248248
colon = strchr(data_source, ':');
249249
if (!colon) {
250-
zend_throw_exception_ex(php_pdo_get_exception(), 0, "invalid data source name (via URI)");
250+
zend_argument_error(php_pdo_get_exception(), 1, "must be a valid data source name (via URI)");
251251
RETURN_THROWS();
252252
}
253253
}

ext/pdo_mysql/tests/pdo_mysql___construct.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,11 @@ MySQLPDOTest::skip();
293293
print "done!";
294294
?>
295295
--EXPECTF--
296-
[002] invalid data source name, [n/a] n/a
297-
[003] invalid data source name, [n/a] n/a
298-
[004] invalid data source name, [n/a] n/a
299-
[005] invalid data source name, [n/a] n/a
300-
[006] invalid data source name, [n/a] n/a
296+
[002] PDO::__construct(): Argument #1 ($dsn) must be a valid data source name, [n/a] n/a
297+
[003] PDO::__construct(): Argument #1 ($dsn) must be a valid data source name, [n/a] n/a
298+
[004] PDO::__construct(): Argument #1 ($dsn) must be a valid data source name, [n/a] n/a
299+
[005] PDO::__construct(): Argument #1 ($dsn) must be a valid data source name, [n/a] n/a
300+
[006] PDO::__construct(): Argument #1 ($dsn) must be a valid data source name, [n/a] n/a
301301
[007] could not find driver, [n/a] n/a
302302
[009] SQLSTATE[%s] [1045] Access denied for user 'dont%s'@'%s' (using password: YES), [n/a] n/a
303303
[017] DSN=%s, SQLSTATE[%s] [%d] %s

ext/simplexml/simplexml.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,15 +2282,15 @@ SXE_METHOD(__construct)
22822282
}
22832283

22842284
if (ZEND_SIZE_T_INT_OVFL(data_len)) {
2285-
zend_throw_exception(zend_ce_exception, "Data is too long", 0);
2285+
zend_argument_error(zend_ce_exception, 1, "is too long");
22862286
RETURN_THROWS();
22872287
}
22882288
if (ZEND_SIZE_T_INT_OVFL(ns_len)) {
2289-
zend_throw_exception(zend_ce_exception, "Namespace is too long", 0);
2289+
zend_argument_error(zend_ce_exception, 4, "is too long");
22902290
RETURN_THROWS();
22912291
}
22922292
if (ZEND_LONG_EXCEEDS_INT(options)) {
2293-
zend_throw_exception(zend_ce_exception, "Invalid options", 0);
2293+
zend_argument_error(zend_ce_exception, 2, "is invalid");
22942294
RETURN_THROWS();
22952295
}
22962296

ext/simplexml/tests/bug74950.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platforms only");
1010
$xml=new SimpleXMLElement(0,9000000000);var_dump($xml->getDocNamespaces())?>
1111
?>
1212
--EXPECTF--
13-
Fatal error: Uncaught Exception: Invalid options in %sbug74950.php:%d
13+
Fatal error: Uncaught Exception: SimpleXMLElement::__construct(): Argument #2 ($options) is invalid in %sbug74950.php:%d
1414
Stack trace:
1515
#0 %sbug74950.php(%d): SimpleXMLElement->__construct('0', 9000000000)
1616
#1 {main}

ext/snmp/snmp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ PHP_METHOD(snmp, __construct)
15401540
case SNMP_VERSION_3:
15411541
break;
15421542
default:
1543-
zend_throw_exception(zend_ce_exception, "Unknown SNMP protocol version", 0);
1543+
zend_argument_value_error(zend_ce_exception, 1, "must be a valid SNMP protocol version");
15441544
RETURN_THROWS();
15451545
}
15461546

ext/snmp/tests/snmp-object-error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var_dump($session->max_oids);
7272
SNMP::__construct() expects at least 3 parameters, 2 given
7373
SNMP::__construct(): Argument #4 must be of type int, string given
7474
SNMP::__construct(): Argument #5 must be of type int, string given
75-
Unknown SNMP protocol version
75+
SNMP::__construct(): Argument #1 ($version) must be a valid SNMP protocol version
7676
Exception handling
7777

7878
Warning: SNMP::get(): Invalid object identifier: .1.3.6.1.2.1.1.1..0 in %s on line %d

ext/spl/php_spl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ PHP_FUNCTION(class_parents)
9393
}
9494

9595
if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
96-
zend_type_error("Object or string expected");
96+
zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(obj));
9797
RETURN_THROWS();
9898
}
9999

@@ -126,7 +126,7 @@ PHP_FUNCTION(class_implements)
126126
RETURN_THROWS();
127127
}
128128
if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
129-
zend_type_error("Object or string expected");
129+
zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(obj));
130130
RETURN_THROWS();
131131
}
132132

@@ -155,7 +155,7 @@ PHP_FUNCTION(class_uses)
155155
RETURN_THROWS();
156156
}
157157
if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
158-
zend_type_error("Object or string expected");
158+
zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(obj));
159159
RETURN_THROWS();
160160
}
161161

0 commit comments

Comments
 (0)