Req #73810 [Opn]: hex2bin should return false or throw an exception instead of an E_WARNING

From: Date: Sun, 25 Dec 2016 23:45:37 +0000
Subject: Req #73810 [Opn]: 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-206205@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: yohgaki@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 Type: Feature/Change Request Package: Unknown/Other Function Operating System: All PHP Version: 7.1.0 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#206205) next »