Bug #72320 [NEW]: iconv_substr returns false for empty strings
| From: | chris at ocproducts dot com | Date: | Thu, 02 Jun 2016 21:00:42 +0000 |
| Subject: | Bug #72320 [NEW]: iconv_substr returns false for empty strings | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-201410@lists.php.net to get a copy of this message | ||
From: chris at ocproducts dot com
Operating system:
PHP version: 7.0.7
Package: ICONV related
Bug Type: Bug
Bug description:iconv_substr returns false for empty strings
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 bug report at https://bugs.php.net/bug.php?id=72320&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72320&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72320&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72320&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=72320&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=72320&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=72320&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=72320&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=72320&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=72320&r=support
Expected behavior: https://bugs.php.net/fix.php?id=72320&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=72320&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=72320&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=72320&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72320&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=72320&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=72320&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=72320&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72320&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=72320&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=72320&r=mysqlcfg