Re: [PATCH 1/2] utf8: respect bounds in utf8_strnwidth()
- From
Dirk Gouders <dirk@gouders.net>
- Date
- Nov 17, 2026, 15:15 UTC
- Message-ID
- <ghcxs3v6a3.fsf@gouders.net>
- In-Reply-To
- <xmqqld6siau9.fsf@gitster.g>
On Mon, Nov 16 2026, Junio C Hamano wrote:
Show 41 quoted lines
> Dirk Gouders <dirk@gouders.net> writes:
>
>> utf8_strnwidth() takes a len argument, but because
>> display_mode_esc_sequence_len() works without an explicit boundary,
>> there are cases when utf8_strnwidth() operates beyond the given len
>> boundary.
>
> The problem is well spotted, but isn't the blame slightly misplaced?
> The preimage with missing context looks like this:
>
> int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
> {
> const char *orig = string;
> size_t width = 0;
>
> while (string && string < orig + len) {
> int glyph_width;
> size_t skip;
>
> while (skip_ansi &&
> (skip = display_mode_esc_sequence_len(string)) != 0)
> string += skip;
>
> glyph_width = utf8_width(&string, NULL);
> if (glyph_width > 0)
> width += glyph_width;
> }
>
> Even if display_mode_esc_sequence_len() were updated to take the
> remaining number of bytes in string (which I think *is* a good
> change, by the way), the loop it is in may cause the entirety of the
> string up to the "len" bytes to be skipped. Because the original
> lacks a guard before the call to utf8_width(), however, we end up
> calling it even if string points beyond the given region of bytes
> orig[0,len).
>
> If you blame the lack of such a guard before utf8_width() (or after
> the loop that has display_mode_esc_sequence_len()), it would match
> the proposed fix. But I doubt it is entirely fair to blame the lack
> of an explicit boundary in display_mode_esc_sequence_len() for the
> breakage you are addressing.Yes, I'm not sure if that term is correct but the main problem seems to be a "contract mismatch": a function that accepts a boundary argument calls one function display_mode_esc_sequence_len() that doesn't have an explicit boundary and for a second one utf8_width() it doesn't use the optional boundary argument.
Show 10 quoted lines
>> Fix this by adding a boundary-check after looping with >> display_mode_esc_sequence_len() to ensure we operate strictly within >> the given len. > > If the incoming "string" is at the end of an mmapped region (beyond > the end of which nothing is mapped), reading string[len] would give > you SIGBUS, so display_mode_esc_sequence_len() would already have > taken you into trouble. In other words, even though such a check > before utf8_width() is better than nothing, in a sense it is too > late, no?
Correct.
I am currently preparing a v2 that adresses all the above. I will try to give that v2 sufficient time to hopefully reduce errors in my workflow and not steal other people's time.
Regards,
Dirk
Show 21 quoted lines
>> diff --git a/utf8.c b/utf8.c
>> index 96460cc414..7e050bb2c4 100644
>> --- a/utf8.c
>> +++ b/utf8.c
>> @@ -217,9 +217,13 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
>> int glyph_width;
>> size_t skip;
>>
>> - while (skip_ansi &&
>> - (skip = display_mode_esc_sequence_len(string)) != 0)
>> - string += skip;
>> + if (skip_ansi) {
>> + while ((skip = display_mode_esc_sequence_len(string)) != 0)
>> + string += skip;
>> +
>> + if (string >= orig + len)
>> + break;
>> + }
>>
>> glyph_width = utf8_width(&string, NULL);
>> if (glyph_width > 0)