Skip to content

Commit 87f4698

Browse files
committed
Revert support for empty grapheme needles
1 parent 21d4f76 commit 87f4698

File tree

3 files changed

+123
-16
lines changed

3 files changed

+123
-16
lines changed

ext/intl/grapheme/grapheme_string.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ PHP_FUNCTION(grapheme_strpos)
124124

125125
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
126126

127+
if (needle_len == 0) {
128+
zend_argument_value_error(2, "cannot be empty");
129+
RETURN_THROWS();
130+
}
131+
127132
if (offset >= 0) {
128133
/* quick check to see if the string might be there
129134
* I realize that 'offset' is 'grapheme count offset' but will work in spite of that
@@ -177,6 +182,11 @@ PHP_FUNCTION(grapheme_stripos)
177182

178183
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
179184

185+
if (needle_len == 0) {
186+
zend_argument_value_error(2, "cannot be empty");
187+
RETURN_THROWS();
188+
}
189+
180190
is_ascii = ( grapheme_ascii_check((unsigned char*)haystack, haystack_len) >= 0 );
181191

182192
if ( is_ascii ) {
@@ -238,6 +248,11 @@ PHP_FUNCTION(grapheme_strrpos)
238248

239249
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
240250

251+
if (needle_len == 0) {
252+
zend_argument_value_error(2, "cannot be empty");
253+
RETURN_THROWS();
254+
}
255+
241256
is_ascii = grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0;
242257

243258
if ( is_ascii ) {
@@ -293,6 +308,11 @@ PHP_FUNCTION(grapheme_strripos)
293308

294309
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
295310

311+
if (needle_len == 0) {
312+
zend_argument_value_error(2, "cannot be empty");
313+
RETURN_THROWS();
314+
}
315+
296316
is_ascii = grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0;
297317

298318
if ( is_ascii ) {
@@ -559,6 +579,11 @@ static void strstr_common_handler(INTERNAL_FUNCTION_PARAMETERS, int f_ignore_cas
559579
RETURN_THROWS();
560580
}
561581

582+
if (needle_len == 0) {
583+
zend_argument_value_error(2, "cannot be empty");
584+
RETURN_THROWS();
585+
}
586+
562587
if ( !f_ignore_case ) {
563588

564589
/* ASCII optimization: quick check to see if the string might be there

ext/intl/tests/grapheme.phpt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,19 @@ function ut_main()
400400
$arg0 = urlencode($test[0]);
401401
$res_str .= "substring of \"$arg0\" from \"$test[1]\" - grapheme_substr";
402402
if ( 3 == count( $test ) ) {
403-
$result = grapheme_substr($test[0], $test[1]);
403+
try {
404+
$result = grapheme_substr($test[0], $test[1]);
405+
} catch (ValueError $exception) {
406+
echo $exception->getMessage() . "\n";
407+
}
404408
}
405409
else {
406410
$res_str .= " with length $test[2]";
407-
$result = grapheme_substr($test[0], $test[1], $test[2]);
411+
try {
412+
$result = grapheme_substr($test[0], $test[1], $test[2]);
413+
} catch (ValueError $exception) {
414+
echo $exception->getMessage() . "\n";
415+
}
408416
}
409417
$res_str .= " = ";
410418
if ( $result === false ) {
@@ -465,11 +473,19 @@ function ut_main()
465473
$arg0 = urlencode($test[0]);
466474
$res_str .= "find \"$arg1\" in \"$arg0\" - grapheme_strstr";
467475
if ( 3 == count( $test ) ) {
468-
$result = grapheme_strstr($test[0], $test[1]);
476+
try {
477+
$result = grapheme_strstr($test[0], $test[1]);
478+
} catch (ValueError $exception) {
479+
echo $exception->getMessage() . "\n";
480+
}
469481
}
470482
else {
471483
$res_str .= " before flag is " . ( $test[2] ? "TRUE" : "FALSE" );
472-
$result = grapheme_strstr($test[0], $test[1], $test[2]);
484+
try {
485+
$result = grapheme_strstr($test[0], $test[1], $test[2]);
486+
} catch (ValueError $exception) {
487+
echo $exception->getMessage() . "\n";
488+
}
473489
}
474490
$res_str .= " = ";
475491
if ( $result === false ) {

ext/intl/tests/grapheme_empty.phpt

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,84 @@ Test grapheme_strpos-alike functions with empty needle
55
--FILE--
66
<?php
77

8-
var_dump(grapheme_strpos("abc", ""));
9-
var_dump(grapheme_stripos("abc", ""));
10-
var_dump(grapheme_strrpos("abc", ""));
11-
var_dump(grapheme_strripos("abc", ""));
12-
var_dump(grapheme_strstr("abc", ""));
13-
var_dump(grapheme_stristr("abc", ""));
8+
ini_set("intl.error_level", E_WARNING);
9+
10+
try {
11+
var_dump(grapheme_strpos("abc", "", -1));
12+
} catch (ValueError $exception) {
13+
echo $exception->getMessage() . "\n";
14+
}
15+
16+
try {
17+
var_dump(grapheme_strpos("abc", ""));
18+
} catch (ValueError $exception) {
19+
echo $exception->getMessage() . "\n";
20+
}
21+
22+
try {
23+
var_dump(grapheme_strpos("abc", "", -1));
24+
} catch (ValueError $exception) {
25+
echo $exception->getMessage() . "\n";
26+
}
27+
28+
try {
29+
var_dump(grapheme_stripos("abc", ""));
30+
} catch (ValueError $exception) {
31+
echo $exception->getMessage() . "\n";
32+
}
33+
34+
try {
35+
var_dump(grapheme_stripos("abc", "", -1));
36+
} catch (ValueError $exception) {
37+
echo $exception->getMessage() . "\n";
38+
}
39+
40+
try {
41+
var_dump(grapheme_strrpos("abc", ""));
42+
} catch (ValueError $exception) {
43+
echo $exception->getMessage() . "\n";
44+
}
45+
46+
try {
47+
var_dump(grapheme_strrpos("abc", "", -1));
48+
} catch (ValueError $exception) {
49+
echo $exception->getMessage() . "\n";
50+
}
51+
52+
try {
53+
var_dump(grapheme_strripos("abc", ""));
54+
} catch (ValueError $exception) {
55+
echo $exception->getMessage() . "\n";
56+
}
57+
58+
try {
59+
var_dump(grapheme_strripos("abc", "", 1));
60+
} catch (ValueError $exception) {
61+
echo $exception->getMessage() . "\n";
62+
}
63+
64+
try {
65+
var_dump(grapheme_strstr("abc", ""));
66+
} catch (ValueError $exception) {
67+
echo $exception->getMessage() . "\n";
68+
}
69+
70+
try {
71+
var_dump(grapheme_stristr("abc", ""));
72+
} catch (ValueError $exception) {
73+
echo $exception->getMessage() . "\n";
74+
}
1475

1576
?>
1677
--EXPECT--
17-
int(0)
18-
int(0)
19-
int(3)
20-
int(3)
21-
string(3) "abc"
22-
bool(false)
78+
grapheme_strpos(): Argument #2 ($needle) cannot be empty
79+
grapheme_strpos(): Argument #2 ($needle) cannot be empty
80+
grapheme_strpos(): Argument #2 ($needle) cannot be empty
81+
grapheme_stripos(): Argument #2 ($needle) cannot be empty
82+
grapheme_stripos(): Argument #2 ($needle) cannot be empty
83+
grapheme_strrpos(): Argument #2 ($needle) cannot be empty
84+
grapheme_strrpos(): Argument #2 ($needle) cannot be empty
85+
grapheme_strripos(): Argument #2 ($needle) cannot be empty
86+
grapheme_strripos(): Argument #2 ($needle) cannot be empty
87+
grapheme_strstr(): Argument #2 ($needle) cannot be empty
88+
grapheme_stristr(): Argument #2 ($needle) cannot be empty

0 commit comments

Comments
 (0)