]> granicus.if.org Git - php/commitdiff
Convert warnings to ValueError in mb_strpos function family.
authorGeorge Peter Banyard <girgias@php.net>
Tue, 7 Jan 2020 23:11:37 +0000 (00:11 +0100)
committerGeorge Peter Banyard <girgias@php.net>
Fri, 24 Jan 2020 22:59:22 +0000 (23:59 +0100)
Closes GH-5109

16 files changed:
ext/mbstring/mbstring.c
ext/mbstring/tests/bug43840.phpt
ext/mbstring/tests/bug43841.phpt
ext/mbstring/tests/bug45923.phpt
ext/mbstring/tests/mb_stripos.phpt
ext/mbstring/tests/mb_stripos_empty_needle.phpt
ext/mbstring/tests/mb_stripos_invalid_offset.phpt [new file with mode: 0644]
ext/mbstring/tests/mb_stripos_variation5_Bug45923.phpt
ext/mbstring/tests/mb_strpos.phpt
ext/mbstring/tests/mb_strpos_empty_needle.phpt
ext/mbstring/tests/mb_strpos_invalid_offset.phpt [new file with mode: 0644]
ext/mbstring/tests/mb_strpos_offset_errors.phpt
ext/mbstring/tests/mb_strpos_variation5.phpt
ext/mbstring/tests/mb_strripos_empty_needle.phpt
ext/mbstring/tests/mb_strripos_variation5_Bug45923.phpt
ext/mbstring/tests/mb_strrpos_empty_needle.phpt

index 053e91e5bc89c57fa5aaefa62568774c75d0d53b..1970febc5492e266a607fc4ee7cda889ae9bddd2 100644 (file)
@@ -2084,13 +2084,13 @@ static void handle_strpos_error(size_t error) {
        case MBFL_ERROR_NOT_FOUND:
                break;
        case MBFL_ERROR_ENCODING:
-               php_error_docref(NULL, E_WARNING, "Unknown encoding or conversion error");
+               php_error_docref(NULL, E_WARNING, "Conversion error");
                break;
        case MBFL_ERROR_OFFSET:
-               php_error_docref(NULL, E_WARNING, "Offset not contained in string");
+               zend_value_error("Offset not contained in string");
                break;
        default:
-               php_error_docref(NULL, E_WARNING, "Unknown error in mb_strpos");
+               zend_value_error("Unknown error in mb_strpos");
                break;
        }
 }
index 9cf7cacdb9e359f379dfa66109e3400034923b81..457356823ecefe6f3d532bdf449370e85763ed8a 100644 (file)
@@ -26,12 +26,20 @@ $needle = base64_decode('44CC');
 foreach($offsets as $i) {
        echo "\n-- Offset is $i --\n";
        echo "--Multibyte String:--\n";
-       var_dump( mb_strpos($string_mb, $needle, $i, 'UTF-8') );
+       try {
+           var_dump( mb_strpos($string_mb, $needle, $i, 'UTF-8') );
+       } catch (\ValueError $e) {
+           echo $e->getMessage() . \PHP_EOL;
+       }
        echo"--ASCII String:--\n";
-       var_dump(mb_strpos('This is na English ta', 'a', $i));
+       try {
+           var_dump(mb_strpos('This is na English ta', 'a', $i));
+       } catch (\ValueError $e) {
+           echo $e->getMessage() . \PHP_EOL;
+       }
 }
 ?>
---EXPECTF--
+--EXPECT--
 -- Offset is 20 --
 --Multibyte String:--
 int(20)
@@ -46,30 +54,18 @@ bool(false)
 
 -- Offset is 22 --
 --Multibyte String:--
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --ASCII String:--
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- Offset is 53 --
 --Multibyte String:--
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --ASCII String:--
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- Offset is 54 --
 --Multibyte String:--
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --ASCII String:--
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
index b353efaf77c954c6e71ed3dfc0965e928f23bf4e..cb24ece04a1ca63c17ca2d0158dd862f0a1b3dc4 100644 (file)
@@ -17,50 +17,49 @@ function_exists('mb_strrpos') or die("skip mb_strrpos() is not available in this
  */
 
 $offsets = array(-25, -24, -13, -12);
-$string_mb =
-base64_decode('5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvv
-JfvvJjvvJnjgII=');
-$needle = base64_decode('44CC');
+// Japanese string in UTF-8
+$string_mb = "日本語テキストです。0123456789。";
+$needle = "。";
 
 foreach ($offsets as $i) {
        echo "\n-- Offset is $i --\n";
        echo "Multibyte String:\n";
-       var_dump( mb_strrpos($string_mb, $needle, $i, 'UTF-8') );
+       try {
+           var_dump( mb_strrpos($string_mb, $needle, $i, 'UTF-8') );
+       } catch (\ValueError $e) {
+           echo $e->getMessage() . \PHP_EOL;
+       }
        echo "ASCII String:\n";
        echo "mb_strrpos:\n";
-       var_dump(mb_strrpos('This is na English ta', 'a', $i));
+       try {
+           var_dump(mb_strrpos('This is na English ta', 'a', $i));
+       } catch (\ValueError $e) {
+           echo $e->getMessage() . \PHP_EOL;
+       }
        echo "strrpos:\n";
        try {
            var_dump(strrpos('This is na English ta', 'a', $i));
-       } catch (ValueError $exception) {
-           echo $exception->getMessage() . "\n";
+       } catch (\ValueError $e) {
+           echo $e->getMessage() . \PHP_EOL;
        }
 }
 ?>
---EXPECTF--
+--EXPECT--
 -- Offset is -25 --
 Multibyte String:
-
-Warning: mb_strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 ASCII String:
 mb_strrpos:
-
-Warning: mb_strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 strrpos:
 Offset not contained in string
 
 -- Offset is -24 --
 Multibyte String:
-
-Warning: mb_strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 ASCII String:
 mb_strrpos:
-
-Warning: mb_strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 strrpos:
 Offset not contained in string
 
index 98428d6d30d847c9dee91c07e3f88fafac3af3f9..522f4ddddd635313e5e6c6f657ae95623b2e1070 100644 (file)
@@ -8,13 +8,13 @@ Bug #45923 (mb_st[r]ripos() offset not handled correctly)
 function section($func, $haystack, $needle)
 {
        echo "\n------- $func -----------\n\n";
-       foreach(array(0, 3, 6, 9, 11, 12, -1, -3, -6, -20) as $offset) {
+       foreach([0, 3, 6, 9, 11, 12, -1, -3, -6, -20] as $offset) {
                echo "> Offset: $offset\n";
-               try {
-                   var_dump($func($haystack,$needle,$offset));
-               } catch (ValueError $exception) {
-                   echo $exception->getMessage() . "\n";
-               }
+        try {
+                   var_dump($func($haystack, $needle, $offset));
+        } catch (\ValueError $e) {
+            echo $e->getMessage() . \PHP_EOL;
+        }
        }
 }
 
@@ -30,7 +30,7 @@ section('mb_strrpos' , "●○◆ ●○◆ ●○◆", "●○◆");
 section('strripos'   , "abc abc abc"  , "abc");
 section('mb_strripos', "●○◆ ●○◆ ●○◆", "●○◆");
 ?>
---EXPECTF--
+--EXPECT--
 ------- strpos -----------
 
 > Offset: 0
@@ -67,9 +67,7 @@ bool(false)
 > Offset: 11
 bool(false)
 > Offset: 12
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 > Offset: -1
 bool(false)
 > Offset: -3
@@ -77,9 +75,7 @@ int(8)
 > Offset: -6
 int(8)
 > Offset: -20
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 ------- stripos -----------
 
@@ -117,9 +113,7 @@ bool(false)
 > Offset: 11
 bool(false)
 > Offset: 12
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 > Offset: -1
 bool(false)
 > Offset: -3
@@ -127,9 +121,7 @@ int(8)
 > Offset: -6
 int(8)
 > Offset: -20
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 ------- strrpos -----------
 
@@ -167,9 +159,7 @@ bool(false)
 > Offset: 11
 bool(false)
 > Offset: 12
-
-Warning: mb_strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 > Offset: -1
 int(8)
 > Offset: -3
@@ -177,9 +167,7 @@ int(8)
 > Offset: -6
 int(4)
 > Offset: -20
-
-Warning: mb_strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 ------- strripos -----------
 
@@ -217,9 +205,7 @@ bool(false)
 > Offset: 11
 bool(false)
 > Offset: 12
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 > Offset: -1
 int(8)
 > Offset: -3
@@ -227,6 +213,4 @@ int(8)
 > Offset: -6
 int(4)
 > Offset: -20
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
index d80169edd3d7797d6988278b5c216bde6ddc0457..900b222c931f956d38e855a4b2b01f4896cd055d 100644 (file)
@@ -41,30 +41,6 @@ print mb_stripos($euc_jp, 0, -15,       'EUC-JP') . "\n";
 print mb_stripos($euc_jp, 0, -43,       'EUC-JP') . "\n";
 
 
-// Invalid offset - should return false with warning
-print ("== INVALID OFFSET ==\n");
-
-$r =  mb_stripos($euc_jp, 'ÆüËܸì', 44, 'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r =  mb_stripos($euc_jp, 'ÆüËܸì', 50, 'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r =  mb_stripos($euc_jp, '0', 50,     'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r =  mb_stripos($euc_jp, 3, 50,       'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r =   mb_stripos($euc_jp, 0, 50,       'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r = mb_stripos($euc_jp, 'ÆüËܸì', -50, 'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r = mb_stripos($euc_jp, '0', -50,     'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r = mb_stripos($euc_jp, 3, -50,       'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r = mb_stripos($euc_jp, 0, -50,       'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r = mb_stripos($euc_jp, 0, -44,       'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-
 // Out of range - should return false
 print ("== OUT OF RANGE ==\n");
 
@@ -143,37 +119,6 @@ String len: 43
 33
 30
 0
-== INVALID OFFSET ==
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
 == OUT OF RANGE ==
 OK_OUT_RANGE
 OK_OUT_RANGE
index 31e21f1cd5c91dfa9866d10c66e2e3966f1353cb..d991e8bdb5be0f87908096523efc9cfe61ef1701 100644 (file)
@@ -24,10 +24,18 @@ echo "\n-- ASCII string with in range negative offset --\n";
 var_dump(mb_stripos($string_ascii, '', -2));
 
 echo "\n-- ASCII string with out of bound positive offset --\n";
-var_dump(mb_stripos($string_ascii, '', 150));
+try {
+    var_dump(mb_stripos($string_ascii, '', 150));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 echo "\n-- ASCII string with out of bound negative offset --\n";
-var_dump(mb_stripos($string_ascii, '', -150));
+try {
+    var_dump(mb_stripos($string_ascii, '', -150));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 
 echo "\n-- Multi-byte string without offset --\n";
@@ -40,13 +48,21 @@ echo "\n-- Multi-byte string with in range negative offset --\n";
 var_dump(mb_stripos($string_mb, '', -2));
 
 echo "\n-- Multi-byte string with out of bound positive offset --\n";
-var_dump(mb_stripos($string_mb, '', 150));
+try {
+    var_dump(mb_stripos($string_mb, '', 150));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 echo "\n-- Multi-byte string with out of bound negative offset --\n";
-var_dump(mb_stripos($string_mb, '', -150));
+try {
+    var_dump(mb_stripos($string_mb, '', -150));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 ?>
---EXPECTF--
+--EXPECT--
 -- ASCII string without offset --
 int(0)
 
@@ -57,14 +73,10 @@ int(2)
 int(5)
 
 -- ASCII string with out of bound positive offset --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- ASCII string with out of bound negative offset --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- Multi-byte string without offset --
 int(0)
@@ -76,11 +88,7 @@ int(2)
 int(19)
 
 -- Multi-byte string with out of bound positive offset --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- Multi-byte string with out of bound negative offset --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
diff --git a/ext/mbstring/tests/mb_stripos_invalid_offset.phpt b/ext/mbstring/tests/mb_stripos_invalid_offset.phpt
new file mode 100644 (file)
index 0000000..f4ce16d
--- /dev/null
@@ -0,0 +1,93 @@
+--TEST--
+mb_stripos() with invalid offsets
+--SKIPIF--
+<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
+--FILE--
+<?php
+
+ini_set('include_path','.');
+include_once('common.inc');
+mb_internal_encoding('UTF-8') or print("mb_internal_encoding() failed\n");
+
+// Test string
+$string = '0123この文字列は日本語です。UTF-8を使っています。0123日本語は面倒臭い。';
+
+$slen = mb_strlen($string);
+echo "String len: $slen\n";
+
+print ("== INVALID OFFSET ==\n");
+
+try {
+    var_dump( mb_stripos($string, '日本語', 44));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump( mb_stripos($string, '日本語', 50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump( mb_stripos($string, '0', 50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_stripos($string, 3, 50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_stripos($string, 0, 50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_stripos($string, '日本語', -50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_stripos($string, '0', -50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_stripos($string, 3, -50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_stripos($string, 0, -50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_stripos($string, 0, -44));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+?>
+--EXPECT--
+String len: 42
+== INVALID OFFSET ==
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
index cc05393a5539dd38be81866584603009dcfd83da..ba2875e7674ac19f62c41b6e4baecc3b27285583 100644 (file)
@@ -36,25 +36,28 @@ $needle_mb = base64_decode('44CC');
 for ($i = -30; $i <= 60; $i += 10) {
        echo "\n**-- Offset is: $i --**\n";
        echo "-- ASCII String --\n";
-       var_dump(mb_stripos($string_ascii, $needle_ascii, $i));
+       try {
+           var_dump(mb_stripos($string_ascii, $needle_ascii, $i));
+    } catch (\ValueError $e) {
+        echo $e->getMessage() . \PHP_EOL;
+    }
        echo "--Multibyte String --\n";
-       var_dump(mb_stripos($string_mb, $needle_mb, $i, 'UTF-8'));
+       try {
+           var_dump(mb_stripos($string_mb, $needle_mb, $i, 'UTF-8'));
+    } catch (\ValueError $e) {
+        echo $e->getMessage() . \PHP_EOL;
+    }
 }
 
-echo "Done";
 ?>
---EXPECTF--
+--EXPECT--
 *** Testing mb_stripos() : usage variations ***
 
 **-- Offset is: -30 --**
 -- ASCII String --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 **-- Offset is: -20 --**
 -- ASCII String --
@@ -88,41 +91,24 @@ int(20)
 
 **-- Offset is: 30 --**
 -- ASCII String --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 **-- Offset is: 40 --**
 -- ASCII String --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 **-- Offset is: 50 --**
 -- ASCII String --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 **-- Offset is: 60 --**
 -- ASCII String --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
-Done
+Offset not contained in string
index 3b46a9b08f58567514aa677601a2336d51e3fcb7..1b689ab95de9ed6d06f9d4d57ba60386ea10d20a 100644 (file)
@@ -41,51 +41,6 @@ print mb_strpos($euc_jp, 0, -15,       'EUC-JP') . "\n";
 print mb_strpos($euc_jp, 0, -43,       'EUC-JP') . "\n";
 
 
-// Invalid offset - should return false with warning
-print ("== INVALID OFFSET ==\n");
-
-$r =  mb_strpos($euc_jp, 'ÆüËܸì', 44, 'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r =  mb_strpos($euc_jp, 'ÆüËܸì', 50, 'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r =  mb_strpos($euc_jp, '0', 50,     'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r =  mb_strpos($euc_jp, 3, 50,       'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r =   mb_strpos($euc_jp, 0, 50,       'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r = mb_strpos($euc_jp, 'ÆüËܸì', -50, 'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r = mb_strpos($euc_jp, '0', -50,     'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r = mb_strpos($euc_jp, 3, -50,       'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r = mb_strpos($euc_jp, 0, -50,       'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-$r = mb_strpos($euc_jp, 0, -44,       'EUC-JP');
-($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
-
-// Out of range - should return false
-print ("== OUT OF RANGE ==\n");
-
-$r =  mb_strpos($euc_jp, 'ÆüËܸì', 40, 'EUC-JP');
-($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
-$r =  mb_strpos($euc_jp, '0', 40,     'EUC-JP');
-($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
-$r =  mb_strpos($euc_jp, 3, 40,       'EUC-JP');
-($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
-$r =   mb_strpos($euc_jp, 0, 40,       'EUC-JP');
-($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
-$r =  mb_strpos($euc_jp, 'ÆüËܸì', -3, 'EUC-JP');
-($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
-$r =  mb_strpos($euc_jp, '0', -3,     'EUC-JP');
-($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
-$r =  mb_strpos($euc_jp, 3, -3,       'EUC-JP');
-($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
-$r =   mb_strpos($euc_jp, 0, -3,       'EUC-JP');
-($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
-
-
 // Non-existent
 echo "== NON-EXISTENT ==\n";
 
@@ -126,7 +81,7 @@ $r = mb_strpos($euc_jp, "\n");
 ($r === FALSE) ? print "OK_NEWLINE\n" : print "NG_NEWLINE\n";
 
 ?>
---EXPECTF--
+--EXPECT--
 String len: 43
 == POSITIVE OFFSET ==
 10
@@ -143,46 +98,6 @@ String len: 43
 33
 30
 0
-== INVALID OFFSET ==
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-OK_INVALID_OFFSET
-== OUT OF RANGE ==
-OK_OUT_RANGE
-OK_OUT_RANGE
-OK_OUT_RANGE
-OK_OUT_RANGE
-OK_OUT_RANGE
-OK_OUT_RANGE
-OK_OUT_RANGE
-OK_OUT_RANGE
 == NON-EXISTENT ==
 OK_STR
 OK_NEWLINE
index 31612647d641ff41c492dfbdc123ce3d68572d11..da3e984b3ad735f336ffee29db5f9a8fd645fe38 100644 (file)
@@ -24,11 +24,18 @@ echo "\n-- ASCII string with in range negative offset --\n";
 var_dump(mb_strpos($string_ascii, '', -2));
 
 echo "\n-- ASCII string with out of bound positive offset --\n";
-var_dump(mb_strpos($string_ascii, '', 15));
+try {
+    var_dump(mb_strpos($string_ascii, '', 15));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 echo "\n-- ASCII string with out of bound negative offset --\n";
-var_dump(mb_strpos($string_ascii, '', -15));
-
+try {
+    var_dump(mb_strpos($string_ascii, '', -15));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 echo "\n-- Multi-byte string without offset --\n";
 var_dump(mb_strpos($string_mb, ''));
@@ -40,13 +47,21 @@ echo "\n-- Multi-byte string with in range negative offset --\n";
 var_dump(mb_strpos($string_mb, '', -2));
 
 echo "\n-- Multi-byte string with out of bound positive offset --\n";
-var_dump(mb_strpos($string_mb, '', 150));
+try {
+    var_dump(mb_strpos($string_mb, '', 150));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 echo "\n-- Multi-byte string with out of bound negative offset --\n";
-var_dump(mb_strpos($string_mb, '', -150));
+try {
+    var_dump(mb_strpos($string_mb, '', -150));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 ?>
---EXPECTF--
+--EXPECT--
 -- ASCII string without offset --
 int(0)
 
@@ -57,14 +72,10 @@ int(2)
 int(5)
 
 -- ASCII string with out of bound positive offset --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- ASCII string with out of bound negative offset --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- Multi-byte string without offset --
 int(0)
@@ -76,11 +87,7 @@ int(2)
 int(19)
 
 -- Multi-byte string with out of bound positive offset --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- Multi-byte string with out of bound negative offset --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
diff --git a/ext/mbstring/tests/mb_strpos_invalid_offset.phpt b/ext/mbstring/tests/mb_strpos_invalid_offset.phpt
new file mode 100644 (file)
index 0000000..3e2ea65
--- /dev/null
@@ -0,0 +1,93 @@
+--TEST--
+mb_strpos() with invalid offsets
+--SKIPIF--
+<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
+--FILE--
+<?php
+
+ini_set('include_path','.');
+include_once('common.inc');
+mb_internal_encoding('UTF-8') or print("mb_internal_encoding() failed\n");
+
+// Test string
+$string = '0123この文字列は日本語です。UTF-8を使っています。0123日本語は面倒臭い。';
+
+$slen = mb_strlen($string);
+echo "String len: $slen\n";
+
+print ("== INVALID OFFSET ==\n");
+
+try {
+    var_dump( mb_strpos($string, '日本語', 44));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump( mb_strpos($string, '日本語', 50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump( mb_strpos($string, '0', 50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_strpos($string, 3, 50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_strpos($string, 0, 50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_strpos($string, '日本語', -50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_strpos($string, '0', -50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_strpos($string, 3, -50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_strpos($string, 0, -50));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+try {
+    var_dump(mb_strpos($string, 0, -44));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+?>
+--EXPECT--
+String len: 42
+== INVALID OFFSET ==
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
index 76f22431d4ff7e0caafa88962aed8b8c4abb449c..41d7e257d4409a4d9a294a359d99117a4fef37ed 100644 (file)
@@ -3,37 +3,54 @@ Offset errors for various strpos functions
 --FILE--
 <?php
 
-var_dump(mb_strpos("f", "bar", 3));
-var_dump(mb_strpos("f", "bar", -3));
-var_dump(mb_strrpos("f", "bar", 3));
-var_dump(mb_strrpos("f", "bar", -3));
-var_dump(mb_stripos("f", "bar", 3));
-var_dump(mb_stripos("f", "bar", -3));
-var_dump(mb_strripos("f", "bar", 3));
-var_dump(mb_strripos("f", "bar", -3));
+try {
+    var_dump(mb_strpos("f", "bar", 3));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump(mb_strpos("f", "bar", -3));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump(mb_strrpos("f", "bar", 3));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump(mb_strrpos("f", "bar", -3));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump(mb_stripos("f", "bar", 3));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump(mb_stripos("f", "bar", -3));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump(mb_strripos("f", "bar", 3));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump(mb_strripos("f", "bar", -3));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 ?>
---EXPECTF--
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
-
-Warning: mb_strrpos(): Offset not contained in string in %s on line %d
-bool(false)
-
-Warning: mb_strrpos(): Offset not contained in string in %s on line %d
-bool(false)
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
-
-Warning: mb_stripos(): Offset not contained in string in %s on line %d
-bool(false)
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+--EXPECT--
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
+Offset not contained in string
index 097c8dccb42258db97c83b9952ac73561c55fa5d..7c2d4cd4a545a6055fea5479da322a1954cf65c3 100644 (file)
@@ -30,31 +30,34 @@ $needle_mb = base64_decode('44CC');
 
 /*
  * Loop through integers as multiples of ten for $offset argument
- * mb_strpos should not be able to accept negative values as $offset.
  * 60 is larger than *BYTE* count for $string_mb
  */
 for ($i = -30; $i <= 60; $i += 10) {
        echo "\n**-- Offset is: $i --**\n";
        echo "-- ASCII String --\n";
-       var_dump(mb_strpos($string_ascii, $needle_ascii, $i));
+    try {
+           var_dump(mb_strpos($string_ascii, $needle_ascii, $i));
+    } catch (\ValueError $e) {
+        echo $e->getMessage() . \PHP_EOL;
+    }
+    
        echo "--Multibyte String --\n";
-       var_dump(mb_strpos($string_mb, $needle_mb, $i, 'UTF-8'));
+    try {
+           var_dump(mb_strpos($string_mb, $needle_mb, $i, 'UTF-8'));
+    } catch (\ValueError $e) {
+        echo $e->getMessage() . \PHP_EOL;
+    }
 }
 
-echo "Done";
 ?>
---EXPECTF--
+--EXPECT--
 *** Testing mb_strpos() : usage variations ***
 
 **-- Offset is: -30 --**
 -- ASCII String --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 **-- Offset is: -20 --**
 -- ASCII String --
@@ -88,41 +91,24 @@ int(20)
 
 **-- Offset is: 30 --**
 -- ASCII String --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 **-- Offset is: 40 --**
 -- ASCII String --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 **-- Offset is: 50 --**
 -- ASCII String --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 **-- Offset is: 60 --**
 -- ASCII String --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_strpos(): Offset not contained in string in %s on line %d
-bool(false)
-Done
+Offset not contained in string
index fdd873d1fb9cec23a1da037dc70d9e08df496b1d..7d41b05a61e90d52b454613356e1a50a8433c1d1 100644 (file)
@@ -24,10 +24,18 @@ echo "\n-- ASCII string with in range negative offset --\n";
 var_dump(mb_strripos($string_ascii, '', -2));
 
 echo "\n-- ASCII string with out of bound positive offset --\n";
-var_dump(mb_strripos($string_ascii, '', 15));
+try {
+    var_dump(mb_strripos($string_ascii, '', 15));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 echo "\n-- ASCII string with out of bound negative offset --\n";
-var_dump(mb_strripos($string_ascii, '', -15));
+try {
+    var_dump(mb_strripos($string_ascii, '', -15));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 
 echo "\n-- Multi-byte string without offset --\n";
@@ -40,13 +48,21 @@ echo "\n-- Multi-byte string with in range negative offset --\n";
 var_dump(mb_strripos($string_mb, '', -2));
 
 echo "\n-- Multi-byte string with out of bound positive offset --\n";
-var_dump(mb_strripos($string_mb, '', 150));
+try {
+    var_dump(mb_strripos($string_mb, '', 150));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 echo "\n-- Multi-byte string with out of bound negative offset --\n";
-var_dump(mb_strripos($string_mb, '', -150));
+try {
+    var_dump(mb_strripos($string_mb, '', -150));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 ?>
---EXPECTF--
+--EXPECT--
 -- ASCII string without offset --
 int(7)
 
@@ -57,14 +73,10 @@ int(7)
 int(5)
 
 -- ASCII string with out of bound positive offset --
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- ASCII string with out of bound negative offset --
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- Multi-byte string without offset --
 int(21)
@@ -76,11 +88,7 @@ int(21)
 int(19)
 
 -- Multi-byte string with out of bound positive offset --
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- Multi-byte string with out of bound negative offset --
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
index 402969d6d96c9c4f26d0b8273d1e2ecca023bcba..90235d6064746864da817853cc94535fe7cb7cd2 100644 (file)
@@ -37,14 +37,22 @@ $needle_mb = base64_decode('44CC');
 for ($i = -10; $i <= 60; $i += 10) {
        echo "\n**-- Offset is: $i --**\n";
        echo "-- ASCII String --\n";
-       var_dump(mb_strripos($string_ascii, $needle_ascii, $i));
+    try {
+           var_dump(mb_strripos($string_ascii, $needle_ascii, $i));
+    } catch (\ValueError $e) {
+        echo $e->getMessage() . \PHP_EOL;
+    }
+
        echo "--Multibyte String --\n";
-       var_dump(mb_strripos($string_mb, $needle_mb, $i, 'UTF-8'));
+    try {
+           var_dump(mb_strripos($string_mb, $needle_mb, $i, 'UTF-8'));
+    } catch (\ValueError $e) {
+        echo $e->getMessage() . \PHP_EOL;
+    }
 }
 
-echo "Done";
 ?>
---EXPECTF--
+--EXPECT--
 *** Testing mb_strripos() : usage variations ***
 
 **-- Offset is: -10 --**
@@ -73,41 +81,24 @@ int(20)
 
 **-- Offset is: 30 --**
 -- ASCII String --
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 **-- Offset is: 40 --**
 -- ASCII String --
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 **-- Offset is: 50 --**
 -- ASCII String --
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 **-- Offset is: 60 --**
 -- ASCII String --
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 --Multibyte String --
-
-Warning: mb_strripos(): Offset not contained in string in %s on line %d
-bool(false)
-Done
+Offset not contained in string
index 41e4bf5239e5baa07a5bffb413a7ef9474a3993c..974c44dac00c120387752baa538c97612d25baa9 100644 (file)
@@ -24,10 +24,18 @@ echo "\n-- ASCII string with in range negative offset --\n";
 var_dump(mb_strrpos($string_ascii, '', -2));
 
 echo "\n-- ASCII string with out of bound positive offset --\n";
-var_dump(mb_strrpos($string_ascii, '', 15));
+try {
+    var_dump(mb_strrpos($string_ascii, '', 15));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 echo "\n-- ASCII string with out of bound negative offset --\n";
-var_dump(mb_strrpos($string_ascii, '', -15));
+try {
+    var_dump(mb_strrpos($string_ascii, '', -15));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 
 echo "\n-- Multi-byte string without offset --\n";
@@ -40,13 +48,21 @@ echo "\n-- Multi-byte string with in range negative offset --\n";
 var_dump(mb_strrpos($string_mb, '', -2));
 
 echo "\n-- Multi-byte string with out of bound positive offset --\n";
-var_dump(mb_strrpos($string_mb, '', 150));
+try {
+    var_dump(mb_strrpos($string_mb, '', 150));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 echo "\n-- Multi-byte string with out of bound negative offset --\n";
-var_dump(mb_strrpos($string_mb, '', -150));
+try {
+    var_dump(mb_strrpos($string_mb, '', -150));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 ?>
---EXPECTF--
+--EXPECT--
 -- ASCII string without offset --
 int(7)
 
@@ -57,14 +73,10 @@ int(7)
 int(5)
 
 -- ASCII string with out of bound positive offset --
-
-Warning: mb_strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- ASCII string with out of bound negative offset --
-
-Warning: mb_strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- Multi-byte string without offset --
 int(21)
@@ -76,11 +88,7 @@ int(21)
 int(19)
 
 -- Multi-byte string with out of bound positive offset --
-
-Warning: mb_strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string
 
 -- Multi-byte string with out of bound negative offset --
-
-Warning: mb_strrpos(): Offset not contained in string in %s on line %d
-bool(false)
+Offset not contained in string