git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/2] utf8: respect bounds in utf8_strnwidth()

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 17, 2026, 00:02 UTC
Message-ID
<xmqqld6siau9.fsf@gitster.g>
In-Reply-To
<20261116-master-v1-1-dc2a50ce0aa0@gouders.net>
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.

> 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?

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)
Previous: Dirk GoudersNext: Dirk Gouders
Message 3 of 8 in “utf8: two minor fixes”
  1. 0/2 utf8: two minor fixesDirk Gouders, Nov 16, 2026
  2. 1/2 utf8: respect bounds in utf8_strnwidth()Dirk Gouders, Nov 16, 2026
  3. Junio C HamanoNov 17, 2026
  4. Dirk GoudersNov 17, 2026
  5. Junio C HamanoNov 17, 2026
  6. Dirk GoudersNov 17, 2026
  7. Junio C HamanoNov 17, 2026
  8. 2/2 utf8: fix comment for utf8_strnwidth()Dirk Gouders, Nov 16, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.