Bug #72320 [Ver->Csd]: iconv_substr returns false for empty strings

From: Date: Mon, 29 Aug 2016 23:25:21 +0000
Subject: Bug #72320 [Ver->Csd]: iconv_substr returns false for empty strings
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-203664@lists.php.net to get a copy of this message
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: Verified +Status: Closed Type: Bug Package: ICONV related PHP Version: 7.0.7 Block user comment: N Private report: N New Comment: Automatic comment on behalf of cmb Revision: http://git.php.net/?p=php-src.git;a=commit;h=a837b2f6a35ab7cdfb49b9407ac0401caa672b23 Log: Fix #72320: iconv_substr returns false for empty strings Previous Comments: ------------------------------------------------------------------------ [2016-06-03 12:34:33] cmb@php.net 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> ------------------------------------------------------------------------ [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

« previous php.bugs (#203664) next »