Doc #65910 [Opn->Ana]: hex2bin returns null rather than false when parameter 1 is not scalar
| From: | yohgaki@php.net | Date: | Wed, 23 Oct 2013 01:49:33 +0000 |
| Subject: | Doc #65910 [Opn->Ana]: hex2bin returns null rather than false when parameter 1 is not scalar | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-10524@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: yohgaki@php.net
Reported by: lucas at threeamdesign dot com
Summary: hex2bin returns null rather than false when
parameter 1 is not scalar
-Status: Open
+Status: Analyzed
Type: Documentation Problem
Package: Scripting Engine problem
PHP Version: 5.5Git-2013-10-16 (Git)
Block user comment: N
Private report: N
New Comment:
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;
}
Previous Comments:
------------------------------------------------------------------------
[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