Skip to content

Commit 6dcf882

Browse files
committed
ext/mbstring: Refactor mb_trim_width() to take size_t arguments
1 parent 8fb651d commit 6dcf882

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

ext/mbstring/mbstring.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,14 +2547,14 @@ PHP_FUNCTION(mb_strwidth)
25472547
RETVAL_LONG(mb_get_strwidth(string, enc));
25482548
}
25492549

2550-
static zend_string* mb_trim_string(zend_string *input, zend_string *marker, const mbfl_encoding *enc, unsigned int from, int width)
2550+
static zend_string* mb_trim_string(zend_string *input, zend_string *marker, const mbfl_encoding *enc, size_t from, size_t width)
25512551
{
25522552
uint32_t wchar_buf[128];
25532553
unsigned char *in = (unsigned char*)ZSTR_VAL(input);
25542554
size_t in_len = ZSTR_LEN(input);
25552555
unsigned int state = 0;
2556-
int remaining_width = width;
2557-
unsigned int to_skip = from;
2556+
size_t remaining_width = width;
2557+
size_t to_skip = from;
25582558
size_t out_len = 0;
25592559
bool first_call = true, input_err = false;
25602560
mb_convert_buf buf;
@@ -2566,17 +2566,23 @@ static zend_string* mb_trim_string(zend_string *input, zend_string *marker, cons
25662566
if (out_len <= to_skip) {
25672567
to_skip -= out_len;
25682568
} else {
2569-
for (unsigned int i = to_skip; i < out_len; i++) {
2569+
for (size_t i = to_skip; i < out_len; i++) {
25702570
uint32_t w = wchar_buf[i];
2571+
size_t current_w_width = character_width(w);
2572+
25712573
input_err |= (w == MBFL_BAD_INPUT);
2572-
remaining_width -= character_width(w);
2573-
if (remaining_width < 0) {
2574-
/* We need to truncate string and append trim marker */
2575-
width -= mb_get_strwidth(marker, enc);
2576-
/* 'width' is now the amount we want to take from 'input' */
2577-
if (width <= 0) {
2574+
2575+
if (remaining_width < current_w_width) {
2576+
size_t marker_width = mb_get_strwidth(marker, enc);
2577+
2578+
/* The trim marker is larger than the desired string width */
2579+
if (width <= marker_width) {
25782580
return zend_string_copy(marker);
25792581
}
2582+
2583+
/* We need to truncate string and append trim marker */
2584+
width -= marker_width;
2585+
/* 'width' is now the amount we want to take from 'input' */
25802586
mb_convert_buf_init(&buf, width, MBSTRG(current_filter_illegal_substchar), MBSTRG(current_filter_illegal_mode));
25812587

25822588
if (first_call) {
@@ -2587,6 +2593,7 @@ static zend_string* mb_trim_string(zend_string *input, zend_string *marker, cons
25872593
goto restart_conversion;
25882594
}
25892595
}
2596+
remaining_width -= current_w_width;
25902597
}
25912598
to_skip = 0;
25922599
}
@@ -2626,12 +2633,13 @@ static zend_string* mb_trim_string(zend_string *input, zend_string *marker, cons
26262633
if (out_len <= from) {
26272634
from -= out_len;
26282635
} else {
2629-
for (unsigned int i = from; i < out_len; i++) {
2630-
width -= character_width(wchar_buf[i]);
2631-
if (width < 0) {
2636+
for (size_t i = from; i < out_len; i++) {
2637+
size_t current_wchar_char_width = character_width(wchar_buf[i]);
2638+
if (width < current_wchar_char_width) {
26322639
enc->from_wchar(wchar_buf + from, i - from, &buf, true);
26332640
goto append_trim_marker;
26342641
}
2642+
width -= character_width(wchar_buf[i]);
26352643
}
26362644
ZEND_ASSERT(in_len > 0);
26372645
enc->from_wchar(wchar_buf + from, out_len - from, &buf, false);

0 commit comments

Comments
 (0)