Skip to content

Commit 1d13808

Browse files
committed
Fix for the issue where strlen could potentially become negative
1 parent 178fc2d commit 1d13808

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

ext/intl/grapheme/grapheme_string.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,8 +992,21 @@ PHP_FUNCTION(grapheme_levenshtein)
992992
UBreakIterator *bi1, *bi2;
993993

994994
int32_t strlen_1, strlen_2;
995-
strlen_1 = grapheme_split_string(ustring1, ustring1_len, NULL, 0);
996-
strlen_2 = grapheme_split_string(ustring2, ustring2_len, NULL, 0);
995+
strlen_1 = grapheme_split_string(ustring1, ustring1_len, NULL, 0, &ustatus);
996+
if (UNEXPECTED(strlen_1 < 0)) {
997+
intl_error_set_code(NULL, ustatus);
998+
intl_error_set_custom_msg(NULL, "Error on grapheme_get_break_iterator for argument #1 ($string1)", 0);
999+
RETVAL_FALSE;
1000+
goto out_ustring2;
1001+
}
1002+
1003+
strlen_2 = grapheme_split_string(ustring2, ustring2_len, NULL, 0, &ustatus);
1004+
if (UNEXPECTED(strlen_2 < 0)) {
1005+
intl_error_set_code(NULL, ustatus);
1006+
intl_error_set_custom_msg(NULL, "Error on grapheme_get_break_iterator for argument #2 ($string2)", 0);
1007+
RETVAL_FALSE;
1008+
goto out_ustring2;
1009+
}
9971010

9981011
if (strlen_1 == 0) {
9991012
RETVAL_LONG(strlen_2 * cost_ins);

ext/intl/grapheme/grapheme_util.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,20 @@ zend_long grapheme_ascii_check(const unsigned char *day, size_t len)
226226
/* }}} */
227227

228228
/* {{{ grapheme_split_string: find and optionally return grapheme boundaries */
229-
int32_t grapheme_split_string(const UChar *text, int32_t text_length, int boundary_array[], int boundary_array_len )
229+
int32_t grapheme_split_string(const UChar *text, int32_t text_length, int boundary_array[], int boundary_array_len, UErrorCode *status)
230230
{
231231
unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE];
232-
UErrorCode status = U_ZERO_ERROR;
232+
*status = U_ZERO_ERROR;
233233
int ret_len, pos;
234234
UBreakIterator* bi;
235235

236-
bi = grapheme_get_break_iterator((void*)u_break_iterator_buffer, &status );
236+
bi = grapheme_get_break_iterator((void*)u_break_iterator_buffer, status);
237237

238-
if( U_FAILURE(status) ) {
238+
if( U_FAILURE(*status) ) {
239239
return -1;
240240
}
241241

242-
ubrk_setText(bi, text, text_length, &status);
242+
ubrk_setText(bi, text, text_length, status);
243243

244244
pos = 0;
245245

ext/intl/grapheme/grapheme_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ zend_long grapheme_strrpos_ascii(char *haystack, size_t haystack_len, char *need
2828
int32_t grapheme_strrpos_utf16(char *haystack, size_t haystack_len, char *needle, size_t needle_len, int32_t offset, int f_ignore_case);
2929
int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle, size_t needle_len, int32_t offset, int *puchar_pos, int f_ignore_case, int last);
3030

31-
int32_t grapheme_split_string(const UChar *text, int32_t text_length, int boundary_array[], int boundary_array_len );
31+
int32_t grapheme_split_string(const UChar *text, int32_t text_length, int boundary_array[], int boundary_array_len, UErrorCode *status);
3232

3333
int32_t grapheme_count_graphemes(UBreakIterator *bi, UChar *string, int32_t string_len);
3434

0 commit comments

Comments
 (0)