Bug #65362 [Com]: strcmp null return missing from docs.
| From: | atli dot jonsson at ymail dot com | Date: | Sun, 18 Aug 2013 22:34:49 +0000 |
| Subject: | Bug #65362 [Com]: strcmp null return missing from docs. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-180975@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=65362&edit=1
ID: 65362
Comment by: atli dot jonsson at ymail dot com
Reported by: atli dot jonsson at ymail dot com
Summary: strcmp null return missing from docs.
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
PHP Version: 5.5.1
Block user comment: N
Private report: N
New Comment:
Yes, I realize this is common behaviour, and not as such a bug in PHP.
That is why I originally categorized this as a documentation bug.
The problem isn't PHP's behaviour, it's that the documentation on the
functions I mentioned is misleading about the return type. There is no
mention of the possibility of a null return, and while you may get away
with not mentioning this fact for most functions, in the case of these
particular functions, it can easily lead to unpredictable and hard to
find bugs.
Previous Comments:
------------------------------------------------------------------------
[2013-08-18 20:32:25] johannes@php.net
This is common PHP behavior and documented in a note here: http://php.net/functions.internal
------------------------------------------------------------------------
[2013-08-18 07:54:39] yohgaki@php.net
Other functions such as strlen()/strncmp()/etc return null.
I'm not sure the best behavior, but E_NOTICE/E_WARNING for invalid parameters are
preferred.
------------------------------------------------------------------------
[2013-08-18 07:50:51] yohgaki@php.net
I changed bug type since this is in Zend/zend_builtin_functions.c.
Shouldn't it raise error for arrays? Currently, it simply returned.
/* {{{ proto int strcmp(string str1, string str2)
Binary safe string comparison */
ZEND_FUNCTION(strcmp)
{
char *s1, *s2;
int s1_len, s2_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &s1, &s1_len,
&s2, &s2_len) == FAILURE) {
return;
}
RETURN_LONG(zend_binary_strcmp(s1, s1_len, s2, s2_len));
}
/* }}} */
------------------------------------------------------------------------
[2013-07-30 23:36:30] atli dot jonsson at ymail dot com
Description:
------------
strcmp, strncmp, strcasecmp and strncasecmp will all return NULL when either
string parameter is of a type that is invalid for string conversions, like Arrays,
Objects and Resources.
However, the docs make no mention of this fact. (Aside from a comment.) As the 0
value returned for equal strings, and NULL returned for invalid comparisons, are
equal when compared in a non-strict manner, this can lead to unexpected behaviour.
There is a warning issued, but without clarification the above is still in no way
obvious.
Test script:
---------------
<?php
$arr = [];
$str = "PHP is awesome!";
if (strcmp($arr, $str) == 0) {
echo "Equal!"; // Ends up here.
}
else {
echo "Not equal!";
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65362&edit=1