Req #73810 [Opn->Wfx]: hex2bin should return false or throw an exception instead of an E_WARNING
| From: | cmb@php.net | Date: | Mon, 16 Aug 2021 15:02:05 +0000 |
| Subject: | Req #73810 [Opn->Wfx]: hex2bin should return false or throw an exception instead of an E_WARNING | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-235863@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=73810&edit=1
ID: 73810
Updated by: cmb@php.net
Reported by: francesco dot montanari at outlook dot com
Summary: hex2bin should return false or throw an exception
instead of an E_WARNING
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Strings related
Operating System: All
PHP Version: 7.1.0
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
There is no way to change a function to suddenly throw instead of
of raising a warning; except maybe in a major version, but that
still requires an RFC. And, like Yasuo already said, this
shouldn't be done for a single function. Thus, I'm closing this
ticket as WONTFIX. If you are still interested in this, please
pursue the RFC process[1].
Also note that this particular case might better be addressed by
using the PECL/uuid package[2].
[1] <https://wiki.php.net/rfc/howto>
[2] <https://pecl.php.net/package/uuid>
Previous Comments:
------------------------------------------------------------------------
[2016-12-25 23:45:35] yohgaki@php.net
Exceptions are preferred, but exception adoption should be done with consistency. i.e. All standard
module features should adopt exception at least.
For the time being, use ErrorException with your error handler.
http://php.net/manual/en/errorexception.construct.php
i.e. Throw ErrorException from your error handler to convert errors to exceptions.
------------------------------------------------------------------------
[2016-12-24 15:05:41] francesco dot montanari at outlook dot com
Description:
------------
I agree with this thread: http://grokbase.com/t/php/php-internals/136tjmm93r/hex2bin-e-warning-is-too-much-for-odd-string
"a function that is supposed to deal with external inputs is better if it returns FALSE without
error"
Now we have to do something like this:
$binaryUuid = @hex2bin(str_replace('-', '', $_GET['uuid']));
if($binaryUuid === false) {
// Show an "Invalid UUID" error
}
but using the "@" operator is considered bad practice, and validate the input BEFORE
passing it to hex2bin is out of discussion because it will slow down the app for nothing (for
example I receive 10 million valid uuid per day, and only about a hundred invalid ones).
Returning false, or throwing a catchable exception would be a better behaviour
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=73810&edit=1