]> granicus.if.org Git - php/commitdiff
avoid reading str[-1], add warning when invalid format specified
authorAntony Dovgal <tony2001@php.net>
Wed, 11 Oct 2006 13:14:08 +0000 (13:14 +0000)
committerAntony Dovgal <tony2001@php.net>
Wed, 11 Oct 2006 13:14:08 +0000 (13:14 +0000)
add test

ext/standard/string.c
ext/standard/tests/strings/str_word_count.phpt
ext/standard/tests/strings/str_word_count1.phpt [new file with mode: 0644]

index 2162031eb4f73ed0327740ff8363ea351ab432d6..5a67d861c10b6644b84eb4565009462149d3c7c7 100644 (file)
@@ -7358,7 +7358,7 @@ static int php_u_str_word_count(UChar *str, int str_len, long type, UChar *char_
                idx++;
        }
        /* last character cannot be -, unless explicitly allowed by the user */
-       if (str[str_len-1] == (UChar)0x2d /*'-'*/ &&
+       if (str_len && str[str_len-1] == (UChar)0x2d /*'-'*/ &&
                (!char_list || !u_memchr(char_list, 0x2d /*'-'*/, char_list_len))) {
                str_len--;
        }
@@ -7422,7 +7422,7 @@ static int php_str_word_count(char *str, int str_len, long type, char *char_list
                p++;
        }
        /* last character cannot be -, unless explicitly allowed by the user */
-       if (*(e - 1) == '-' && (!char_list || !ch['-'])) {
+       if (str_len && *(e - 1) == '-' && (!char_list || !ch['-'])) {
                e--;
        }
 
@@ -7477,8 +7477,17 @@ PHP_FUNCTION(str_word_count)
                return;
        }
 
-       if (type == 1 || type == 2) {
-               array_init(return_value);
+       switch (type) {
+               case 1:
+               case 2:
+                       array_init(return_value);
+                       break;
+               case 0:
+                       /* nothing to be done */
+                       break;
+               default:
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid format value %ld", type);
+                       RETURN_FALSE;
        }
 
        if (str_type == IS_UNICODE) {
index 20ec1e7a38ad2073bc55aa67c48b74d1a4cd45be..44480fc2c81e1ba3283d6832789a84c225f4c6df 100644 (file)
@@ -41,6 +41,8 @@ var_dump(str_word_count("'foo'", 2));
 var_dump(str_word_count("'foo'", 2, "'"));
 var_dump(str_word_count("-foo-", 2));
 var_dump(str_word_count("-foo-", 2, "-"));
+
+echo "Done\n";
 ?>
 --EXPECTF--
 array(6) {
@@ -72,10 +74,18 @@ array(6) {
   string(5) "today"
 }
 int(6)
-NULL
-NULL
-NULL
-NULL
+
+Warning: str_word_count(): Invalid format value 3 in %s on line %d
+bool(false)
+
+Warning: str_word_count(): Invalid format value 123 in %s on line %d
+bool(false)
+
+Warning: str_word_count(): Invalid format value -1 in %s on line %d
+bool(false)
+
+Warning: str_word_count(): Invalid format value 1569325056 in %s on line %d
+bool(false)
 
 Warning: str_word_count() expects parameter 2 to be long, array given in %s on line %d
 NULL
@@ -236,6 +246,7 @@ array(1) {
   [0]=>
   string(5) "-foo-"
 }
+Done
 --UEXPECTF--
 array(6) {
   [0]=>
@@ -266,10 +277,18 @@ array(6) {
   unicode(5) "today"
 }
 int(6)
-NULL
-NULL
-NULL
-NULL
+
+Warning: str_word_count(): Invalid format value 3 in %s on line %d
+bool(false)
+
+Warning: str_word_count(): Invalid format value 123 in %s on line %d
+bool(false)
+
+Warning: str_word_count(): Invalid format value -1 in %s on line %d
+bool(false)
+
+Warning: str_word_count(): Invalid format value 1569325056 in %s on line %d
+bool(false)
 
 Warning: str_word_count() expects parameter 2 to be long, array given in %s on line %d
 NULL
@@ -430,3 +449,4 @@ array(1) {
   [0]=>
   unicode(5) "-foo-"
 }
+Done
diff --git a/ext/standard/tests/strings/str_word_count1.phpt b/ext/standard/tests/strings/str_word_count1.phpt
new file mode 100644 (file)
index 0000000..5f49fcf
--- /dev/null
@@ -0,0 +1,26 @@
+--TEST--
+str_word_count() and invalid arguments
+--FILE--
+<?php
+
+var_dump(str_word_count(""));
+var_dump(str_word_count("", -1));
+var_dump(str_word_count("", -1, $a));
+var_dump($a);
+
+echo "Done\n";
+?>
+--EXPECTF--    
+int(0)
+
+Warning: str_word_count(): Invalid format value -1 in %s on line %d
+bool(false)
+
+Notice: Undefined variable: a in %s on line %d
+
+Warning: str_word_count(): Invalid format value -1 in %s on line %d
+bool(false)
+
+Notice: Undefined variable: a in %s on line %d
+NULL
+Done