Skip to content

Commit 9b7ba01

Browse files
committed
MFH: avoid reading str[-1], add warning when invalid format specified
add test
1 parent 1cc0339 commit 9b7ba01

File tree

3 files changed

+63
-33
lines changed

3 files changed

+63
-33
lines changed

ext/standard/string.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4848,7 +4848,7 @@ PHP_FUNCTION(str_word_count)
48484848
long type = 0;
48494849

48504850
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls", &str, &str_len, &type, &char_list, &char_list_len) == FAILURE) {
4851-
WRONG_PARAM_COUNT;
4851+
return;
48524852
}
48534853

48544854
if (char_list) {
@@ -4857,17 +4857,26 @@ PHP_FUNCTION(str_word_count)
48574857

48584858
p = str;
48594859
e = str + str_len;
4860-
4861-
if (type == 1 || type == 2) {
4862-
array_init(return_value);
4860+
4861+
switch(type) {
4862+
case 1:
4863+
case 2:
4864+
array_init(return_value);
4865+
break;
4866+
case 0:
4867+
/* nothing to be done */
4868+
break;
4869+
default:
4870+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid format value %ld", type);
4871+
RETURN_FALSE;
48634872
}
48644873

48654874
/* first character cannot be ' or -, unless explicitly allowed by the user */
48664875
if ((*p == '\'' && (!char_list || !ch['\''])) || (*p == '-' && (!char_list || !ch['-']))) {
48674876
p++;
48684877
}
48694878
/* last character cannot be -, unless explicitly allowed by the user */
4870-
if (*(e - 1) == '-' && (!char_list || !ch['-'])) {
4879+
if (str_len && *(e - 1) == '-' && (!char_list || !ch['-'])) {
48714880
e--;
48724881
}
48734882

ext/standard/tests/strings/str_word_count.phpt

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var_dump(str_word_count($str));
1212
var_dump(str_word_count($str, 3));
1313
var_dump(str_word_count($str, 123));
1414
var_dump(str_word_count($str, -1));
15-
var_dump(str_word_count($str, 99999999999999999));
15+
var_dump(str_word_count($str, 999999999));
1616
var_dump(str_word_count($str, array()));
1717
var_dump(str_word_count($str, $b));
1818
var_dump($str);
@@ -41,6 +41,8 @@ var_dump(str_word_count("'foo'", 2));
4141
var_dump(str_word_count("'foo'", 2, "'"));
4242
var_dump(str_word_count("-foo-", 2));
4343
var_dump(str_word_count("-foo-", 2, "-"));
44+
45+
echo "Done\n";
4446
?>
4547
--EXPECTF--
4648
array(6) {
@@ -72,34 +74,34 @@ array(6) {
7274
string(5) "today"
7375
}
7476
int(6)
75-
NULL
76-
NULL
77-
NULL
78-
NULL
7977

80-
Warning: str_word_count() expects parameter 2 to be long, array given in %s on line 13
78+
Warning: str_word_count(): Invalid format value 3 in %s on line %d
79+
bool(false)
8180

82-
Warning: Wrong parameter count for str_word_count() in %s on line 13
83-
NULL
81+
Warning: str_word_count(): Invalid format value 123 in %s on line %d
82+
bool(false)
8483

85-
Warning: str_word_count() expects parameter 2 to be long, string given in %s on line 14
84+
Warning: str_word_count(): Invalid format value -1 in %s on line %d
85+
bool(false)
8686

87-
Warning: Wrong parameter count for str_word_count() in %s on line 14
87+
Warning: str_word_count(): Invalid format value 999999999 in %s on line %d
88+
bool(false)
89+
90+
Warning: str_word_count() expects parameter 2 to be long, array given in %s on line %d
91+
NULL
92+
93+
Warning: str_word_count() expects parameter 2 to be long, string given in %s on line %d
8894
NULL
8995
string(55) "Hello friend, you're
9096
looking good today!"
9197
int(5)
9298
int(6)
9399
int(5)
94100

95-
Warning: str_word_count() expects parameter 3 to be string, array given in %s on line 21
96-
97-
Warning: Wrong parameter count for str_word_count() in %s on line 21
101+
Warning: str_word_count() expects parameter 3 to be string, array given in %s on line %d
98102
NULL
99103

100-
Warning: str_word_count() expects parameter 3 to be string, object given in %s on line 22
101-
102-
Warning: Wrong parameter count for str_word_count() in %s on line 22
104+
Warning: str_word_count() expects parameter 3 to be string, object given in %s on line %d
103105
NULL
104106
int(7)
105107
array(5) {
@@ -141,14 +143,10 @@ array(5) {
141143
string(3) "foo"
142144
}
143145

144-
Warning: str_word_count() expects parameter 3 to be string, array given in %s on line 27
145-
146-
Warning: Wrong parameter count for str_word_count() in %s on line 27
146+
Warning: str_word_count() expects parameter 3 to be string, array given in %s on line %d
147147
NULL
148148

149-
Warning: str_word_count() expects parameter 3 to be string, object given in %s on line 28
150-
151-
Warning: Wrong parameter count for str_word_count() in %s on line 28
149+
Warning: str_word_count() expects parameter 3 to be string, object given in %s on line %d
152150
NULL
153151
array(7) {
154152
[0]=>
@@ -205,14 +203,10 @@ array(5) {
205203
string(3) "foo"
206204
}
207205

208-
Warning: str_word_count() expects parameter 3 to be string, array given in %s on line 33
209-
210-
Warning: Wrong parameter count for str_word_count() in %s on line 33
206+
Warning: str_word_count() expects parameter 3 to be string, array given in %s on line %d
211207
NULL
212208

213-
Warning: str_word_count() expects parameter 3 to be string, object given in %s on line 34
214-
215-
Warning: Wrong parameter count for str_word_count() in %s on line 34
209+
Warning: str_word_count() expects parameter 3 to be string, object given in %s on line %d
216210
NULL
217211
array(7) {
218212
[0]=>
@@ -252,3 +246,4 @@ array(1) {
252246
[0]=>
253247
string(5) "-foo-"
254248
}
249+
Done
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
str_word_count() and invalid arguments
3+
--FILE--
4+
<?php
5+
6+
var_dump(str_word_count(""));
7+
var_dump(str_word_count("", -1));
8+
var_dump(str_word_count("", -1, $a));
9+
var_dump($a);
10+
11+
echo "Done\n";
12+
?>
13+
--EXPECTF--
14+
int(0)
15+
16+
Warning: str_word_count(): Invalid format value -1 in %s on line %d
17+
bool(false)
18+
19+
Notice: Undefined variable: a in %s on line %d
20+
21+
Warning: str_word_count(): Invalid format value -1 in %s on line %d
22+
bool(false)
23+
24+
Notice: Undefined variable: a in %s on line %d
25+
NULL
26+
Done

0 commit comments

Comments
 (0)