]> granicus.if.org Git - php/commitdiff
fix #34757 (iconv_substr() gives "Unknown error" when offset > string length)
authorAntony Dovgal <tony2001@php.net>
Tue, 11 Oct 2005 13:50:52 +0000 (13:50 +0000)
committerAntony Dovgal <tony2001@php.net>
Tue, 11 Oct 2005 13:50:52 +0000 (13:50 +0000)
NEWS
ext/iconv/iconv.c

diff --git a/NEWS b/NEWS
index c1ad8bcd294b4645c2afccf055fa32e393d6a463..eb31b3fba0a691f823672a86428988f8221961f7 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -62,6 +62,8 @@ PHP                                                                        NEWS
 - Fixed bug #34785 (subclassing of mysqli_stmt does not work). (Georg)
 - Fixed bug #34777 (Crash in dblib when fetching non-existent error info). (Ilia)
 - Fixed bug #34771 (strtotime() fails with 1-12am/pm). (Derick)
+- Fixed bug #34757 (iconv_substr() gives "Unknown error" when offset > string 
+  length). (Tony)
 - Fixed bug #34723 (array_count_values() strips leading zeroes). (Tony)
 - Fixed bug #34704 (Infinite recursion due to corrupt JPEG). (Marcus)
 - Fixed bug #34678 (__call(), is_callable() and static methods). (Dmitry)
index 92e6a2e0dfb4ec100c676d6ca2e8a54d6db401a9..5b8b60d1756dfeb66b1b5cf7ab3495fdf3535189 100644 (file)
@@ -583,26 +583,38 @@ static php_iconv_err_t _php_iconv_substr(smart_str *pretval,
        size_t out_left;
 
        unsigned int cnt;
-
+       unsigned int total_len;
+       
+       err = _php_iconv_strlen(&total_len, str, nbytes, enc);
+       if (err != PHP_ICONV_ERR_SUCCESS) {
+               return err;
+       }
+       
        /* normalize the offset and the length */
-       if (offset < 0 || len < 0) {
-               unsigned int total_len;
-               err = _php_iconv_strlen(&total_len, str, nbytes, enc);
-               if (err != PHP_ICONV_ERR_SUCCESS) {
-                       return err;
-               }
-               if (offset < 0) {
-                       if ((offset += total_len) < 0) {
-                               offset = 0;
-                       }
+       if (offset < 0) {
+               if ((offset += total_len) < 0) {
+                       offset = 0;
                }
-               if (len < 0) {
-                       if ((len += (total_len - offset)) < 0) {
-                               len = 0;
-                       }
+       }
+       if (len < 0) {
+               if ((len += (total_len - offset)) < 0) {
+                       len = 0;
                }
        }
 
+       if (offset >= total_len) {
+               return PHP_ICONV_ERR_SUCCESS;
+       }
+       
+       if ((offset + len) > total_len) {
+               /* trying to compute the length */
+               len = total_len - offset;
+       }
+
+       if (len == 0) {
+               return PHP_ICONV_ERR_SUCCESS;
+       }
+       
        cd1 = iconv_open(GENERIC_SUPERSET_NAME, enc);
 
        if (cd1 == (iconv_t)(-1)) {