php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46763 mb_stristr() wrong output when needle does not exist
Submitted: 2008-12-05 14:00 UTC Modified: 2008-12-30 02:17 UTC
From: ant@php.net Assigned:
Status: Closed Package: mbstring related
PHP Version: 5.3CVS-2008-12-05 (snap) OS: *
Private report: No CVE-ID:
 [2008-12-05 14:00 UTC] ant@php.net
Description:
------------
Take a look at the mb_stristr_basic.phpt test just checked into 5.3/6.0.  The test passes fine on 5.2 and fails on 5.3 and 6.0.

Reproduce code:
---------------
ext/mbstring/tests/mb_stristr_basic.phpt

Expected result:
----------------
*** Testing mb_stristr() : basic functionality ***

-- ASCII string: needle exists --
string(10) "6263646566"
string(10) "6263646566"
string(2) "61"

-- ASCII string: needle doesn't exist --
bool(false)

-- Multibyte string: needle exists --
string(52) "cebccebdcebecebfcf80cf81cf83cf84cf85cf86cf87cf88cf89"
string(52) "cebccebdcebecebfcf80cf81cf83cf84cf85cf86cf87cf88cf89"
string(44) "ceb1ceb2ceb3ceb4ceb5ceb6ceb7ceb8ceb9cebacebb"

-- Multibyte string: needle doesn't exist --
bool(false)
===DONE===


Actual result:
--------------
*** Testing mb_stristr() : basic functionality ***

-- ASCII string: needle exists --
string(10) "6263646566"
string(10) "6263646566"
string(2) "61"

-- ASCII string: needle doesn't exist --
string(6) "abcdef"

-- Multibyte string: needle exists --
string(52) "cebccebdcebecebfcf80cf81cf83cf84cf85cf86cf87cf88cf89"
string(52) "cebccebdcebecebfcf80cf81cf83cf84cf85cf86cf87cf88cf89"
string(44) "ceb1ceb2ceb3ceb4ceb5ceb6ceb7ceb8ceb9cebacebb"

-- Multibyte string: needle doesn't exist --
string(48) "αβγδεζηθικλμνξοπρστυφχψω"
===DONE===


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-12-30 01:04 UTC] typoon at gmail dot com
In the file mbstring.c on function mb_stristr the variable 'n' used to determine the position of needle on haystack was changed from signed int to unsignedint. When the php_mb_stripos function does not find needle in haystack it returns -1 to n. As n is unsigned, that value will be huge, causing the problem.

Here is a simple patch that might fix the issue.

Index: ext/mbstring/mbstring.c
===================================================================
RCS file: /repository/php-src/ext/mbstring/mbstring.c,v
retrieving revision 1.224.2.22.2.25.2.41
diff -u -u -w -p -r1.224.2.22.2.25.2.41 mbstring.c
--- ext/mbstring/mbstring.c	27 Dec 2008 13:32:24 -0000	1.224.2.22.2.25.2.41
+++ ext/mbstring/mbstring.c	30 Dec 2008 00:58:31 -0000
@@ -2555,7 +2555,8 @@ PHP_FUNCTION(mb_strrchr)
 PHP_FUNCTION(mb_stristr)
 {
 	zend_bool part = 0;
-	unsigned int n, from_encoding_len, len, mblen;
+	unsigned from_encoding_len, len, mblen;
+       int n;
 	mbfl_string haystack, needle, result, *ret = NULL;
 	const char *from_encoding = mbfl_no2preferred_mime_name(MBSTRG(current_internal_encoding));
 	mbfl_string_init(&haystack);
 [2008-12-30 02:17 UTC] felipe@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.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 09:02:23 2014 UTC