xref: /PHP-8.0/ext/mbstring/tests/bug43840.phpt (revision b5c7a83d)
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/*
11 * mb_strpos bounds check is byte count rather than a character count:
12 * The multibyte string should be returning the same results as the ASCII string.
13 * Multibyte string was not returning error message until offset was passed the
14 * byte count of the string. Should return error message when passed character count.
15 */
16
17$offsets = array(20, 21, 22, 53, 54);
18$string_mb = base64_decode('5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=');
19$needle = base64_decode('44CC');
20
21foreach($offsets as $i) {
22    echo "\n-- Offset is $i --\n";
23    echo "--Multibyte String:--\n";
24    try {
25        var_dump( mb_strpos($string_mb, $needle, $i, 'UTF-8') );
26    } catch (\ValueError $e) {
27        echo $e->getMessage() . \PHP_EOL;
28    }
29    echo"--ASCII String:--\n";
30    try {
31        var_dump(mb_strpos('This is na English ta', 'a', $i));
32    } catch (\ValueError $e) {
33        echo $e->getMessage() . \PHP_EOL;
34    }
35}
36?>
37--EXPECT--
38-- Offset is 20 --
39--Multibyte String:--
40int(20)
41--ASCII String:--
42int(20)
43
44-- Offset is 21 --
45--Multibyte String:--
46bool(false)
47--ASCII String:--
48bool(false)
49
50-- Offset is 22 --
51--Multibyte String:--
52mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
53--ASCII String:--
54mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
55
56-- Offset is 53 --
57--Multibyte String:--
58mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
59--ASCII String:--
60mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
61
62-- Offset is 54 --
63--Multibyte String:--
64mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
65--ASCII String:--
66mb_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
67