Skip to content

Commit f82414e

Browse files
committed
Fix out of bounds offset handling with empty needle
For strrpos with positive out of bounds offsets was not detected.
1 parent 7af24ea commit f82414e

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

ext/intl/grapheme/grapheme_util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle,
127127
STRPOS_CHECK_STATUS(status, "Failed to set up iterator");
128128

129129
if (uneedle_len == 0) {
130-
offset_pos = grapheme_get_haystack_offset(bi, last && offset >= 0 ? uhaystack_len : offset);
130+
offset_pos = grapheme_get_haystack_offset(bi, offset);
131131
if (offset_pos == -1) {
132132
zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
133133
ret_pos = -1;
134134
goto finish;
135135
}
136-
ret_pos = offset_pos;
136+
ret_pos = last && offset >= 0 ? uhaystack_len : offset_pos;
137137
goto finish;
138138
}
139139

ext/intl/tests/grapheme_out_of_bounds.phpt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,49 @@ try {
118118
}
119119
echo "\n";
120120

121+
// Empty needle + out of bounds
122+
try {
123+
var_dump(grapheme_strpos("äöü", "", 4));
124+
} catch (ValueError $e) {
125+
echo $e->getMessage(), "\n";
126+
}
127+
try {
128+
var_dump(grapheme_stripos("äöü", "", 4));
129+
} catch (ValueError $e) {
130+
echo $e->getMessage(), "\n";
131+
}
132+
try {
133+
var_dump(grapheme_strrpos("äöü", "", 4));
134+
} catch (ValueError $e) {
135+
echo $e->getMessage(), "\n";
136+
}
137+
try {
138+
var_dump(grapheme_strripos("äöü", "", 4));
139+
} catch (ValueError $e) {
140+
echo $e->getMessage(), "\n";
141+
}
142+
try {
143+
var_dump(grapheme_strpos("äöü", "", -4));
144+
} catch (ValueError $e) {
145+
echo $e->getMessage(), "\n";
146+
}
147+
try {
148+
var_dump(grapheme_stripos("äöü", "", -4));
149+
} catch (ValueError $e) {
150+
echo $e->getMessage(), "\n";
151+
}
152+
try {
153+
var_dump(grapheme_strrpos("äöü", "", -4));
154+
} catch (ValueError $e) {
155+
echo $e->getMessage(), "\n";
156+
}
157+
try {
158+
var_dump(grapheme_strripos("äöü", "", -4));
159+
} catch (ValueError $e) {
160+
echo $e->getMessage(), "\n";
161+
}
162+
echo "\n";
163+
121164
var_dump(grapheme_substr("foo", 3));
122165
var_dump(grapheme_substr("foo", -3));
123166
var_dump(grapheme_substr("foo", 4));
@@ -181,6 +224,15 @@ grapheme_stripos(): Argument #3 ($offset) must be contained in argument #1 ($hay
181224
grapheme_strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
182225
grapheme_strripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
183226

227+
grapheme_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
228+
grapheme_stripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
229+
grapheme_strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
230+
grapheme_strripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
231+
grapheme_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
232+
grapheme_stripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
233+
grapheme_strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
234+
grapheme_strripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
235+
184236
string(0) ""
185237
string(3) "foo"
186238
string(0) ""

0 commit comments

Comments
 (0)