xref: /PHP-7.0/ext/mbstring/tests/bug43840.phpt (revision b2b61661)
1--TEST--
2Test mb_strpos() function : mb_strpos bounds check is byte count rather than a character count
3--SKIPIF--
4<?php
5extension_loaded('mbstring') or die('skip');
6function_exists('mb_strpos') or die("skip mb_strpos() is not available in this build");
7?>
8--FILE--
9<?php
10/* Prototype  : int mb_strpos(string $haystack, string $needle [, int $offset [, string $encoding]])
11 * Description: Find position of first occurrence of a string within another
12 * Source code: ext/mbstring/mbstring.c
13 */
14
15/*
16 * mb_strpos bounds check is byte count rather than a character count:
17 * The multibyte string should be returning the same results as the ASCII string.
18 * Multibyte string was not returning error message until offset was passed the
19 * byte count of the string. Should return error message when passed character count.
20 */
21
22$offsets = array(20, 21, 22, 53, 54);
23$string_mb = base64_decode('5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=');
24$needle = base64_decode('44CC');
25
26foreach($offsets as $i) {
27	echo "\n-- Offset is $i --\n";
28	echo "--Multibyte String:--\n";
29	var_dump( mb_strpos($string_mb, $needle, $i, 'UTF-8') );
30	echo"--ASCII String:--\n";
31	var_dump(mb_strpos(b'This is na English ta', b'a', $i));
32}
33?>
34--EXPECTF--
35-- Offset is 20 --
36--Multibyte String:--
37int(20)
38--ASCII String:--
39int(20)
40
41-- Offset is 21 --
42--Multibyte String:--
43bool(false)
44--ASCII String:--
45bool(false)
46
47-- Offset is 22 --
48--Multibyte String:--
49
50Warning: mb_strpos(): Offset not contained in string in %s on line %d
51bool(false)
52--ASCII String:--
53
54Warning: mb_strpos(): Offset not contained in string in %s on line %d
55bool(false)
56
57-- Offset is 53 --
58--Multibyte String:--
59
60Warning: mb_strpos(): Offset not contained in string in %s on line %d
61bool(false)
62--ASCII String:--
63
64Warning: mb_strpos(): Offset not contained in string in %s on line %d
65bool(false)
66
67-- Offset is 54 --
68--Multibyte String:--
69
70Warning: mb_strpos(): Offset not contained in string in %s on line %d
71bool(false)
72--ASCII String:--
73
74Warning: mb_strpos(): Offset not contained in string in %s on line %d
75bool(false)
76