tony2001                Wed Oct 11 13:14:08 2006 UTC

  Added files:                 
    /php-src/ext/standard/tests/strings str_word_count1.phpt 

  Modified files:              
    /php-src/ext/standard       string.c 
    /php-src/ext/standard/tests/strings str_word_count.phpt 
  Log:
  avoid reading str[-1], add warning when invalid format specified
  add test
  
  
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/string.c?r1=1.607&r2=1.608&diff_format=u
Index: php-src/ext/standard/string.c
diff -u php-src/ext/standard/string.c:1.607 php-src/ext/standard/string.c:1.608
--- php-src/ext/standard/string.c:1.607 Sun Oct  8 13:34:24 2006
+++ php-src/ext/standard/string.c       Wed Oct 11 13:14:07 2006
@@ -18,7 +18,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: string.c,v 1.607 2006/10/08 13:34:24 bjori Exp $ */
+/* $Id: string.c,v 1.608 2006/10/11 13:14:07 tony2001 Exp $ */
 
 /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
 
@@ -7358,7 +7358,7 @@
                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 @@
                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 @@
                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) {
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/str_word_count.phpt?r1=1.8&r2=1.9&diff_format=u
Index: php-src/ext/standard/tests/strings/str_word_count.phpt
diff -u php-src/ext/standard/tests/strings/str_word_count.phpt:1.8 
php-src/ext/standard/tests/strings/str_word_count.phpt:1.9
--- php-src/ext/standard/tests/strings/str_word_count.phpt:1.8  Mon Aug 28 
19:52:01 2006
+++ php-src/ext/standard/tests/strings/str_word_count.phpt      Wed Oct 11 
13:14:08 2006
@@ -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, "-"));
+
+echo "Done\n";
 ?>
 --EXPECTF--
 array(6) {
@@ -72,10 +74,18 @@
   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 @@
   [0]=>
   string(5) "-foo-"
 }
+Done
 --UEXPECTF--
 array(6) {
   [0]=>
@@ -266,10 +277,18 @@
   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 @@
   [0]=>
   unicode(5) "-foo-"
 }
+Done

http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/str_word_count1.phpt?view=markup&rev=1.1
Index: php-src/ext/standard/tests/strings/str_word_count1.phpt
+++ php-src/ext/standard/tests/strings/str_word_count1.phpt
--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

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to