Skip to content

Improve error messages of various extensions #5278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/bz2/bz2.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ static PHP_FUNCTION(bzopen)
}

if (CHECK_ZVAL_NULL_PATH(file)) {
zend_type_error("Filename must not contain null bytes");
zend_argument_type_error(1, "must not contain null bytes");
RETURN_THROWS();
}

Expand Down Expand Up @@ -430,7 +430,7 @@ static PHP_FUNCTION(bzopen)

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

Expand Down
4 changes: 2 additions & 2 deletions ext/bz2/tests/002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ resource(%d) of type (stream)
resource(%d) of type (stream)

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

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

Warning: bzopen(): cannot write to a stream opened in read only mode in %s on line %d
bool(false)
Expand Down
8 changes: 4 additions & 4 deletions ext/bz2/tests/bzopen_string_filename_with_null_bytes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ bzopen(): throw TypeError if filename contains null bytes

try {
bzopen("file\0", "w");
} catch (\TypeError $e) {
} catch (TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}

try {
bzopen("file\0", "r");
} catch (\TypeError $e) {
} catch (TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}

?>
--EXPECT--
Filename must not contain null bytes
Filename must not contain null bytes
bzopen(): Argument #1 ($file) must not contain null bytes
bzopen(): Argument #1 ($file) must not contain null bytes
4 changes: 2 additions & 2 deletions ext/dom/parentnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ xmlNode* dom_zvals_to_fragment(php_libxml_ref_obj *document, xmlNode *contextNod
} else {
xmlFree(fragment);

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

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

return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/tests/DOM4_ParentNode_append_invalidtypes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ try {
echo "OK! {$e->getMessage()}";
}
--EXPECT--
OK! Invalid argument type must be either DOMNode or string
OK! DOMElement::append(): Argument #1 must be of type DOMNode|string, array given
16 changes: 8 additions & 8 deletions ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3885,7 +3885,7 @@ PHP_FUNCTION(imageaffine)
src = php_gd_libgdimageptr_from_zval_p(IM);

if ((nelems = zend_hash_num_elements(Z_ARRVAL_P(z_affine))) != 6) {
zend_value_error("Affine array must have six elements");
zend_argument_value_error(2, "must have 6 elements");
RETURN_THROWS();
}

Expand All @@ -3902,7 +3902,7 @@ PHP_FUNCTION(imageaffine)
affine[i] = zval_get_double(zval_affine_elem);
break;
default:
zend_type_error("Invalid type for element %i", i);
zend_argument_type_error(3, "contains invalid type for element %i", i);
RETURN_THROWS();
}
}
Expand Down Expand Up @@ -3970,7 +3970,7 @@ PHP_FUNCTION(imageaffinematrixget)
case GD_AFFINE_SCALE: {
double x, y;
if (!options || Z_TYPE_P(options) != IS_ARRAY) {
zend_type_error("Array expected as options when using translate or scale");
zend_argument_type_error(1, "must be of type array when using translate or scale");
RETURN_THROWS();
}

Expand Down Expand Up @@ -4002,7 +4002,7 @@ PHP_FUNCTION(imageaffinematrixget)
double angle;

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

Expand All @@ -4019,7 +4019,7 @@ PHP_FUNCTION(imageaffinematrixget)
}

default:
zend_value_error("Invalid type for element " ZEND_LONG_FMT, type);
zend_argument_value_error(1, "must be a valid element type");
RETURN_THROWS();
}

Expand Down Expand Up @@ -4068,7 +4068,7 @@ PHP_FUNCTION(imageaffinematrixconcat)
m1[i] = zval_get_double(tmp);
break;
default:
zend_type_error("Matrix 1 contains invalid type for element %i", i);
zend_argument_type_error(1, "contains invalid type for element %i", i);
RETURN_THROWS();
}
}
Expand All @@ -4085,7 +4085,7 @@ PHP_FUNCTION(imageaffinematrixconcat)
m2[i] = zval_get_double(tmp);
break;
default:
zend_type_error("Matrix 2 contains invalid type for element %i", i);
zend_argument_type_error(2, "contains invalid type for element %i", i);
RETURN_THROWS();
}
}
Expand Down Expand Up @@ -4299,7 +4299,7 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
close_stream = 0;
} else if (Z_TYPE_P(to_zval) == IS_STRING) {
if (CHECK_ZVAL_NULL_PATH(to_zval)) {
zend_type_error("Invalid 2nd parameter, filename must not contain null bytes");
zend_argument_type_error(2, "must not contain null bytes");
RETURN_THROWS();
}

Expand Down
14 changes: 7 additions & 7 deletions ext/gd/tests/bug67248.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ for($i=0;$i<7;$i++) {
}
?>
--EXPECT--
!! [TypeError] Array expected as options when using translate or scale
!! [TypeError] Array expected as options when using translate or scale
!! [TypeError] Number is expected as option when using rotate or shear
!! [TypeError] Number is expected as option when using rotate or shear
!! [TypeError] Number is expected as option when using rotate or shear
!! [ValueError] Invalid type for element 5
!! [ValueError] Invalid type for element 6
!! [TypeError] imageaffinematrixget(): Argument #1 ($type) must be of type array when using translate or scale
!! [TypeError] imageaffinematrixget(): Argument #1 ($type) must be of type array when using translate or scale
!! [TypeError] imageaffinematrixget(): Argument #2 ($options) must be of type int|double when using rotate or shear
!! [TypeError] imageaffinematrixget(): Argument #2 ($options) must be of type int|double when using rotate or shear
!! [TypeError] imageaffinematrixget(): Argument #2 ($options) must be of type int|double when using rotate or shear
!! [ValueError] imageaffinematrixget(): Argument #1 ($type) must be a valid element type
!! [ValueError] imageaffinematrixget(): Argument #1 ($type) must be a valid element type
2 changes: 1 addition & 1 deletion ext/gd/tests/imagebmp_nullbyte_injection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ try {
}
?>
--EXPECT--
Invalid 2nd parameter, filename must not contain null bytes
imagebmp(): Argument #2 ($to) must not contain null bytes
2 changes: 1 addition & 1 deletion ext/gd/tests/imagegif_nullbyte_injection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ try {
}
?>
--EXPECT--
Invalid 2nd parameter, filename must not contain null bytes
imagegif(): Argument #2 ($to) must not contain null bytes
2 changes: 1 addition & 1 deletion ext/gd/tests/imagejpeg_nullbyte_injection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ try {
}
?>
--EXPECT--
Invalid 2nd parameter, filename must not contain null bytes
imagejpeg(): Argument #2 ($to) must not contain null bytes
2 changes: 1 addition & 1 deletion ext/gd/tests/imagepng_nullbyte_injection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ try {
}
?>
--EXPECTF--
Invalid 2nd parameter, filename must not contain null bytes
imagepng(): Argument #2 ($to) must not contain null bytes
2 changes: 1 addition & 1 deletion ext/gd/tests/imagewbmp_nullbyte_injection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ try {
}
?>
--EXPECT--
Invalid 2nd parameter, filename must not contain null bytes
imagewbmp(): Argument #2 ($to) must not contain null bytes
2 changes: 1 addition & 1 deletion ext/gd/tests/imagewebp_nullbyte_injection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ try {
}
?>
--EXPECT--
Invalid 2nd parameter, filename must not contain null bytes
imagewebp(): Argument #2 ($to) must not contain null bytes
2 changes: 1 addition & 1 deletion ext/mysqli/mysqli.c
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ void php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAMETERS, int override_flags
* single value is an array. Also we'd have to make that one
* argument passed by reference.
*/
zend_throw_exception(zend_ce_exception, "Parameter ctor_params must be an array", 0);
zend_argument_error(zend_ce_exception, 3, "must be of type array, %s given", zend_zval_type_name(ctor_params));
RETURN_THROWS();
}
}
Expand Down
2 changes: 1 addition & 1 deletion ext/openssl/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -6640,7 +6640,7 @@ PHP_OPENSSL_API zend_string* php_openssl_random_pseudo_bytes(zend_long buffer_le
|| ZEND_LONG_INT_OVFL(buffer_length)
#endif
) {
zend_throw_exception(zend_ce_error, "Length must be greater than 0", 0);
zend_argument_error(NULL, 1, "must be greater than 0");
return NULL;
}
buffer = zend_string_alloc(buffer_length, 0);
Expand Down
2 changes: 1 addition & 1 deletion ext/openssl/tests/openssl_random_pseudo_bytes_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ try {
}
?>
--EXPECT--
Length must be greater than 0
openssl_random_pseudo_bytes(): Argument #1 ($length) must be greater than 0
2 changes: 1 addition & 1 deletion ext/pcre/php_pcre.c
Original file line number Diff line number Diff line change
Expand Up @@ -2303,7 +2303,7 @@ static void preg_replace_common(INTERNAL_FUNCTION_PARAMETERS, int is_filter)
}
} else {
if (Z_TYPE_P(regex) != IS_ARRAY) {
zend_type_error("Parameter mismatch, pattern is a string while replacement is an array");
zend_argument_type_error(1, "must be of type array when argument #2 ($replace) is an array, %s given", zend_zval_type_name(regex));
RETURN_THROWS();
}
}
Expand Down
2 changes: 1 addition & 1 deletion ext/pcre/tests/bug21732.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ try {
var_dump(preg_replace_callback("/(ab)(cd)(e)/", array(new foo(), "cb"), 'abcde'));
?>
--EXPECT--
Parameter mismatch, pattern is a string while replacement is an array
preg_replace(): Argument #1 ($regex) must be of type array when argument #2 ($replace) is an array, string given
array(4) {
[0]=>
string(5) "abcde"
Expand Down
2 changes: 1 addition & 1 deletion ext/pcre/tests/preg_replace_error2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ Arg value is: this is a string
string(64) "this is a stringthis is a stringthis is a stringthis is a string"

Arg value is: Array
Parameter mismatch, pattern is a string while replacement is an array
preg_replace(): Argument #1 ($regex) must be of type array when argument #2 ($replace) is an array, string given
Object of class stdClass could not be converted to string
Done
6 changes: 3 additions & 3 deletions ext/pdo/pdo_dbh.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ static PHP_METHOD(PDO, dbh_constructor)

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

Expand All @@ -242,12 +242,12 @@ static PHP_METHOD(PDO, dbh_constructor)
/* the specified URI holds connection details */
data_source = dsn_from_uri(data_source + sizeof("uri:")-1, alt_dsn, sizeof(alt_dsn));
if (!data_source) {
zend_throw_exception_ex(php_pdo_get_exception(), 0, "invalid data source URI");
zend_argument_error(php_pdo_get_exception(), 1, "must be a valid data source URI");
RETURN_THROWS();
}
colon = strchr(data_source, ':');
if (!colon) {
zend_throw_exception_ex(php_pdo_get_exception(), 0, "invalid data source name (via URI)");
zend_argument_error(php_pdo_get_exception(), 1, "must be a valid data source name (via URI)");
RETURN_THROWS();
}
}
Expand Down
10 changes: 5 additions & 5 deletions ext/pdo_mysql/tests/pdo_mysql___construct.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,11 @@ MySQLPDOTest::skip();
print "done!";
?>
--EXPECTF--
[002] invalid data source name, [n/a] n/a
[003] invalid data source name, [n/a] n/a
[004] invalid data source name, [n/a] n/a
[005] invalid data source name, [n/a] n/a
[006] invalid data source name, [n/a] n/a
[002] PDO::__construct(): Argument #1 ($dsn) must be a valid data source name, [n/a] n/a
[003] PDO::__construct(): Argument #1 ($dsn) must be a valid data source name, [n/a] n/a
[004] PDO::__construct(): Argument #1 ($dsn) must be a valid data source name, [n/a] n/a
[005] PDO::__construct(): Argument #1 ($dsn) must be a valid data source name, [n/a] n/a
[006] PDO::__construct(): Argument #1 ($dsn) must be a valid data source name, [n/a] n/a
[007] could not find driver, [n/a] n/a
[009] SQLSTATE[%s] [1045] Access denied for user 'dont%s'@'%s' (using password: YES), [n/a] n/a
[017] DSN=%s, SQLSTATE[%s] [%d] %s
Expand Down
6 changes: 3 additions & 3 deletions ext/simplexml/simplexml.c
Original file line number Diff line number Diff line change
Expand Up @@ -2282,15 +2282,15 @@ SXE_METHOD(__construct)
}

if (ZEND_SIZE_T_INT_OVFL(data_len)) {
zend_throw_exception(zend_ce_exception, "Data is too long", 0);
zend_argument_error(zend_ce_exception, 1, "is too long");
RETURN_THROWS();
}
if (ZEND_SIZE_T_INT_OVFL(ns_len)) {
zend_throw_exception(zend_ce_exception, "Namespace is too long", 0);
zend_argument_error(zend_ce_exception, 4, "is too long");
RETURN_THROWS();
}
if (ZEND_LONG_EXCEEDS_INT(options)) {
zend_throw_exception(zend_ce_exception, "Invalid options", 0);
zend_argument_error(zend_ce_exception, 2, "is invalid");
RETURN_THROWS();
}

Expand Down
2 changes: 1 addition & 1 deletion ext/simplexml/tests/bug74950.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platforms only");
$xml=new SimpleXMLElement(0,9000000000);var_dump($xml->getDocNamespaces())?>
?>
--EXPECTF--
Fatal error: Uncaught Exception: Invalid options in %sbug74950.php:%d
Fatal error: Uncaught Exception: SimpleXMLElement::__construct(): Argument #2 ($options) is invalid in %sbug74950.php:%d
Stack trace:
#0 %sbug74950.php(%d): SimpleXMLElement->__construct('0', 9000000000)
#1 {main}
Expand Down
2 changes: 1 addition & 1 deletion ext/snmp/snmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@ PHP_METHOD(snmp, __construct)
case SNMP_VERSION_3:
break;
default:
zend_throw_exception(zend_ce_exception, "Unknown SNMP protocol version", 0);
zend_argument_value_error(zend_ce_exception, 1, "must be a valid SNMP protocol version");
RETURN_THROWS();
}

Expand Down
2 changes: 1 addition & 1 deletion ext/snmp/tests/snmp-object-error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var_dump($session->max_oids);
SNMP::__construct() expects at least 3 parameters, 2 given
SNMP::__construct(): Argument #4 must be of type int, string given
SNMP::__construct(): Argument #5 must be of type int, string given
Unknown SNMP protocol version
SNMP::__construct(): Argument #1 ($version) must be a valid SNMP protocol version
Exception handling

Warning: SNMP::get(): Invalid object identifier: .1.3.6.1.2.1.1.1..0 in %s on line %d
Expand Down
6 changes: 3 additions & 3 deletions ext/spl/php_spl.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ PHP_FUNCTION(class_parents)
}

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

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

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

Expand Down
Loading