php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42866 str_split() returns extra char when given string size is not mulitple of lenth
Submitted: 2007-10-05 11:20 UTC Modified: 2007-11-21 02:27 UTC
From: kraghuba at in dot ibm dot com Assigned:
Status: Closed Package: Strings related
PHP Version: 6CVS-2007-10-05 (snap) OS: linux,win xp
Private report: No CVE-ID: None
 [2007-10-05 11:20 UTC] kraghuba at in dot ibm dot com
Description:
------------
str_split() returns extra charecter when given string is not mulitple of given length. for example if the given string size is 22 and split length is 5, then the last element of the returned array contains 5 or more than 5 chars, which is wrong. The last element of the return array should only contain 2 chars.

This behavior is only noticed on  PHP6 with UNICODE ON

Reproduce code:
---------------
<?php
$str = 'Testing str_split()';
$split_length = 5;

var_dump( str_split($str, $split_length) );

?>

Expected result:
----------------
array(4) {
  [0]=>
  unicode(5) "Testi"
  [1]=>
  unicode(5) "ng st"
  [2]=>
  unicode(5) "r_spl"
  [3]=>
  unicode(4) "it()"
}

Actual result:
--------------
array(4) {
  [0]=>
  unicode(5) "Testi"
  [1]=>
  unicode(5) "ng st"
  [2]=>
  unicode(5) "r_spl"
  [3]=>
  unicode(8) "it()^@E^@1"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-11-15 17:43 UTC] claudiocherubino at gmail dot com
I tried fixing the bug, in my opinion a single line should be changed.
Sorry, I don't know if this is the right place to post the patch.
Here it is:


Index: ext/standard/string.c
===================================================================
RCS file: /repository/php-src/ext/standard/string.c,v
retrieving revision 1.655
diff -u -r1.655 string.c
--- ext/standard/string.c	7 Oct 2007 05:15:06 -0000	1.655
+++ ext/standard/string.c	15 Nov 2007 17:29:00 -0000
@@ -7802,7 +7802,7 @@
 	}
 
 	if (p != (str.s + str_len * charsize)) {
-		add_next_index_zstrl(return_value, str_type, ZSTR(p), (str.s + str_len * charsize - p), 1);
+		add_next_index_zstrl(return_value, str_type, ZSTR(p), (str.s + str_len * charsize - p)/charsize, 1);
 	}
 }
 /* }}} */
 [2007-11-21 02:27 UTC] stas@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

thanks
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun May 26 02:01:26 2019 UTC