Bug #72320 [Opn->Ver]: iconv_substr returns false for empty strings
Edit report at https://bugs.php.net/bug.php?id=72320&edit=1
ID: 72320
Updated by: cmb@php.net
Reported by: chris at ocproducts dot com
Summary: iconv_substr returns false for empty strings
-Status: Open
+Status: Verified
Type: Bug
Package: ICONV related
PHP Version: 7.0.7
Block user comment: N
Private report: N
New Comment:
The behavior of substr() has been deliberately changed as of PHP
7.0.0 (see <https://3v4l.org/Q52RB> and the PHP
manual[1]). Hence,
the behavior of iconv_substr() should also have been changed, and
not having already done so is an oversight, in my opinion.
(mb_substr() already worked this way.)
[1] <http://php.net/manual/en/migration70.changed-functions.php#migration70.changed-functions.core>
Previous Comments:
------------------------------------------------------------------------
[2016-06-02 21:00:35] chris at ocproducts dot com
Description:
------------
From iconv.c...
_php_iconv_substr returns success if the offset is more than or equal to the character length of the
input string...
if ((size_t)offset >= total_len) {
return PHP_ICONV_ERR_SUCCESS;
}
...
Then in the calling function (iconv_substr) we check the result...
if (err == PHP_ICONV_ERR_SUCCESS && ZSTR_LEN(str) > 0 && retval.s != NULL) {
RETURN_NEW_STR(retval.s);
}
smart_str_free(&retval);
RETURN_FALSE;
Note how this will always return false for an empty input string. Additionally it will also return
false if offset==total_len, because retval.s will not have been set at the point of
_php_iconv_substr returning (as indicated).
The documentation says:
"If str is shorter than offset characters long, FALSE will be returned."
I believe the documentation describes the correct behaviour. 0 characters is not shorter than a 0
offset.
mb_substr and substr are confirmed to work correctly.
Likely nobody has noticed this bug until now because false and '' are similar, but it
matters for us strict type users, and when interacting with persistency layers.
Test script:
---------------
var_dump(iconv_substr('',0,10,'utf-8'));
Expected result:
----------------
string(0) ""
Actual result:
--------------
bool(false)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72320&edit=1
Thread (3 messages)