Req #65910 [Ana->Nab]: hex2bin returns null rather than false when parameter 1 is not scalar

From: Date: Tue, 30 Dec 2014 18:46:08 +0000
Subject: Req #65910 [Ana->Nab]: hex2bin returns null rather than false when parameter 1 is not scalar
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-189542@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=65910&edit=1 ID: 65910 Updated by: nikic@php.net Reported by: lucas at threeamdesign dot com Summary: hex2bin returns null rather than false when parameter 1 is not scalar -Status: Analyzed +Status: Not a bug Type: Feature/Change Request Package: *General Issues PHP Version: 5.5Git-2013-10-16 (Git) Block user comment: N Private report: N New Comment: The null return value used for zpp failures is NULL by convention (all functions where there are no special BC concerns use it). The docs however leave the return value for this case explicitly undefined, see the note on http://php.net/manual/en/functions.internal.php. This behavior applies to all internal functions and as such is not mentioned on every single function page. Previous Comments: ------------------------------------------------------------------------ [2014-12-30 18:29:01] levim@php.net I think returning null for errors generally makes more sense than false. Read the definition of null in our manual and it hopefully explains why: > The special NULL value represents a variable with no value. If there is an error then there probably is no value to return. It makes more sense than false in any case. ------------------------------------------------------------------------ [2013-10-23 01:49:33] yohgaki@php.net This is because "return;" is called when there is parameter mismatch. hex2bin() is not the only one, but there are MANY of them. What should we do with this? It would be cleaner if we return FALSE for all parameter mismatch. Any comments? /* {{{ proto string hex2bin(string data) Converts the hex representation of data to binary */ PHP_FUNCTION(hex2bin) { char *result, *data; size_t newlen; int datalen; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &datalen) == FAILURE) { return; } ------------------------------------------------------------------------ [2013-10-16 05:12:05] lucas at threeamdesign dot com Description: ------------ The docs say it returns false on failure but it returns null when the first argument is not a string (except objects with __toString() ). The function should be made consistent. People who are aware of this quirk will likely be using a ($val === false || $val === null) test, and people who aren't will likely have only ($val === false). Making it return false wouldn't be perfectly backwards-compatible, but would fix more problems than it causes because failure goes undetected if only the latter test is used. If the quirk must remain for compatibility, at least update the docs. Test script: --------------- <?php var_dump(hex2bin(array())); Expected result: ---------------- Warning: hex2bin() expects parameter 1 to be string, array given in foo.php on line 1 bool(false) Actual result: -------------- Warning: hex2bin() expects parameter 1 to be string, array given in foo.php on line 1 NULL ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=65910&edit=1

« previous php.bugs (#189542) next »