tony2001 Wed Oct 11 13:15:30 2006 UTC
Added files: (Branch: PHP_5_2)
/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:
MFH: 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.445.2.14.2.25&r2=1.445.2.14.2.26&diff_format=u
Index: php-src/ext/standard/string.c
diff -u php-src/ext/standard/string.c:1.445.2.14.2.25
php-src/ext/standard/string.c:1.445.2.14.2.26
--- php-src/ext/standard/string.c:1.445.2.14.2.25 Thu Oct 5 18:23:19 2006
+++ php-src/ext/standard/string.c Wed Oct 11 13:15:29 2006
@@ -18,7 +18,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: string.c,v 1.445.2.14.2.25 2006/10/05 18:23:19 andrei Exp $ */
+/* $Id: string.c,v 1.445.2.14.2.26 2006/10/11 13:15:29 tony2001 Exp $ */
/* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
@@ -4848,7 +4848,7 @@
long type = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls", &str,
&str_len, &type, &char_list, &char_list_len) == FAILURE) {
- WRONG_PARAM_COUNT;
+ return;
}
if (char_list) {
@@ -4857,9 +4857,18 @@
p = str;
e = str + str_len;
-
- 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;
}
/* first character cannot be ' or -, unless explicitly allowed by the
user */
@@ -4867,7 +4876,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--;
}
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/str_word_count.phpt?r1=1.4.2.1&r2=1.4.2.1.2.1&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.4.2.1
php-src/ext/standard/tests/strings/str_word_count.phpt:1.4.2.1.2.1
--- php-src/ext/standard/tests/strings/str_word_count.phpt:1.4.2.1 Tue Nov
29 16:14:18 2005
+++ php-src/ext/standard/tests/strings/str_word_count.phpt Wed Oct 11
13:15:30 2006
@@ -12,7 +12,7 @@
var_dump(str_word_count($str, 3));
var_dump(str_word_count($str, 123));
var_dump(str_word_count($str, -1));
-var_dump(str_word_count($str, 99999999999999999));
+var_dump(str_word_count($str, 999999999));
var_dump(str_word_count($str, array()));
var_dump(str_word_count($str, $b));
var_dump($str);
@@ -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,19 +74,23 @@
string(5) "today"
}
int(6)
-NULL
-NULL
-NULL
-NULL
-Warning: str_word_count() expects parameter 2 to be long, array given in %s on
line 13
+Warning: str_word_count(): Invalid format value 3 in %s on line %d
+bool(false)
-Warning: Wrong parameter count for str_word_count() in %s on line 13
-NULL
+Warning: str_word_count(): Invalid format value 123 in %s on line %d
+bool(false)
-Warning: str_word_count() expects parameter 2 to be long, string given in %s
on line 14
+Warning: str_word_count(): Invalid format value -1 in %s on line %d
+bool(false)
-Warning: Wrong parameter count for str_word_count() in %s on line 14
+Warning: str_word_count(): Invalid format value 999999999 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
+
+Warning: str_word_count() expects parameter 2 to be long, string given in %s
on line %d
NULL
string(55) "Hello friend, you're
looking good today!"
@@ -92,14 +98,10 @@
int(6)
int(5)
-Warning: str_word_count() expects parameter 3 to be string, array given in %s
on line 21
-
-Warning: Wrong parameter count for str_word_count() in %s on line 21
+Warning: str_word_count() expects parameter 3 to be string, array given in %s
on line %d
NULL
-Warning: str_word_count() expects parameter 3 to be string, object given in %s
on line 22
-
-Warning: Wrong parameter count for str_word_count() in %s on line 22
+Warning: str_word_count() expects parameter 3 to be string, object given in %s
on line %d
NULL
int(7)
array(5) {
@@ -141,14 +143,10 @@
string(3) "foo"
}
-Warning: str_word_count() expects parameter 3 to be string, array given in %s
on line 27
-
-Warning: Wrong parameter count for str_word_count() in %s on line 27
+Warning: str_word_count() expects parameter 3 to be string, array given in %s
on line %d
NULL
-Warning: str_word_count() expects parameter 3 to be string, object given in %s
on line 28
-
-Warning: Wrong parameter count for str_word_count() in %s on line 28
+Warning: str_word_count() expects parameter 3 to be string, object given in %s
on line %d
NULL
array(7) {
[0]=>
@@ -205,14 +203,10 @@
string(3) "foo"
}
-Warning: str_word_count() expects parameter 3 to be string, array given in %s
on line 33
-
-Warning: Wrong parameter count for str_word_count() in %s on line 33
+Warning: str_word_count() expects parameter 3 to be string, array given in %s
on line %d
NULL
-Warning: str_word_count() expects parameter 3 to be string, object given in %s
on line 34
-
-Warning: Wrong parameter count for str_word_count() in %s on line 34
+Warning: str_word_count() expects parameter 3 to be string, object given in %s
on line %d
NULL
array(7) {
[0]=>
@@ -252,3 +246,4 @@
[0]=>
string(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