@@ -2518,14 +2518,14 @@ PHP_FUNCTION(mb_strwidth)
25182518 RETVAL_LONG (mb_get_strwidth (string , enc ));
25192519}
25202520
2521- static zend_string * mb_trim_string (zend_string * input , zend_string * marker , const mbfl_encoding * enc , unsigned int from , int width )
2521+ static zend_string * mb_trim_string (zend_string * input , zend_string * marker , const mbfl_encoding * enc , size_t from , size_t width )
25222522{
25232523 uint32_t wchar_buf [128 ];
25242524 unsigned char * in = (unsigned char * )ZSTR_VAL (input );
25252525 size_t in_len = ZSTR_LEN (input );
25262526 unsigned int state = 0 ;
2527- int remaining_width = width ;
2528- unsigned int to_skip = from ;
2527+ size_t remaining_width = width ;
2528+ size_t to_skip = from ;
25292529 size_t out_len = 0 ;
25302530 bool first_call = true, input_err = false;
25312531 mb_convert_buf buf ;
@@ -2537,17 +2537,23 @@ static zend_string* mb_trim_string(zend_string *input, zend_string *marker, cons
25372537 if (out_len <= to_skip ) {
25382538 to_skip -= out_len ;
25392539 } else {
2540- for (unsigned int i = to_skip ; i < out_len ; i ++ ) {
2540+ for (size_t i = to_skip ; i < out_len ; i ++ ) {
25412541 uint32_t w = wchar_buf [i ];
2542+ size_t current_w_width = character_width (w );
2543+
25422544 input_err |= (w == MBFL_BAD_INPUT );
2543- remaining_width -= character_width ( w );
2544- if (remaining_width < 0 ) {
2545- /* We need to truncate string and append trim marker */
2546- width -= mb_get_strwidth ( marker , enc );
2547- /* 'width' is now the amount we want to take from 'input' */
2548- if (width <= 0 ) {
2545+
2546+ if (remaining_width < current_w_width ) {
2547+ size_t marker_width = mb_get_strwidth ( marker , enc );
2548+
2549+ /* The trim marker is larger than the desired string width */
2550+ if (width <= marker_width ) {
25492551 return zend_string_copy (marker );
25502552 }
2553+
2554+ /* We need to truncate string and append trim marker */
2555+ width -= marker_width ;
2556+ /* 'width' is now the amount we want to take from 'input' */
25512557 mb_convert_buf_init (& buf , width , MBSTRG (current_filter_illegal_substchar ), MBSTRG (current_filter_illegal_mode ));
25522558
25532559 if (first_call ) {
@@ -2558,6 +2564,7 @@ static zend_string* mb_trim_string(zend_string *input, zend_string *marker, cons
25582564 goto restart_conversion ;
25592565 }
25602566 }
2567+ remaining_width -= current_w_width ;
25612568 }
25622569 to_skip = 0 ;
25632570 }
@@ -2597,12 +2604,13 @@ static zend_string* mb_trim_string(zend_string *input, zend_string *marker, cons
25972604 if (out_len <= from ) {
25982605 from -= out_len ;
25992606 } else {
2600- for (unsigned int i = from ; i < out_len ; i ++ ) {
2601- width - = character_width (wchar_buf [i ]);
2602- if (width < 0 ) {
2607+ for (size_t i = from ; i < out_len ; i ++ ) {
2608+ size_t current_wchar_char_width = character_width (wchar_buf [i ]);
2609+ if (width < current_wchar_char_width ) {
26032610 enc -> from_wchar (wchar_buf + from , i - from , & buf , true);
26042611 goto append_trim_marker ;
26052612 }
2613+ width -= current_wchar_char_width ;
26062614 }
26072615 ZEND_ASSERT (in_len > 0 );
26082616 enc -> from_wchar (wchar_buf + from , out_len - from , & buf , false);
0 commit comments