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

Re: [PATCH v2 4/4] utf8: respect boundaries in utf8_strnwidth()

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 18, 2026, 21:50 UTC
Message-ID
<xmqqo6bl3j32.fsf@gitster.g>
In-Reply-To
<20261118-master-v2-4-3f2aaec15d31@gouders.net>
Dirk Gouders <dirk@gouders.net> writes:
Show 15 quoted lines
> utf8_strnwidth() can be called with boundaries that effectively cause
> incomplete multibyte sequences and we should return "len" in such
> cases.
>
> But, utf8_width is used without its optional boundary argument and
> will thus work beyond the given boundary.
>
> Fix this by using the boundary argument for utf8_width.
> Add some tests that cover such situations.
>
> Signed-off-by: Dirk Gouders <dirk@gouders.net>
> ---
>  t/unit-tests/u-utf8-width.c | 9 +++++++++
>  utf8.c                      | 5 ++++-
>  2 files changed, 13 insertions(+), 1 deletion(-)
Well spotted, and nicely done.
Show 44 quoted lines
>
> diff --git a/t/unit-tests/u-utf8-width.c b/t/unit-tests/u-utf8-width.c
> index 5e2b636db9..c4df4dd6a6 100644
> --- a/t/unit-tests/u-utf8-width.c
> +++ b/t/unit-tests/u-utf8-width.c
> @@ -2,6 +2,15 @@
>  #include "utf8.h"
>  #include "strbuf.h"
>  
> +/*
> + * Test utf8_strnwidth for cjk characters invalidated by len argument.
> + */
> +void test_utf8_width__strnwidth_cjk_chopped(void)
> +{
> +	cl_assert_equal_i(1, utf8_strnwidth("三bytes long, 二columns wide", 1, 1));
> +	cl_assert_equal_i(2, utf8_strnwidth("三bytes long, 二columns wide", 3, 1));
> +}
> +
>  /*
>   * Test utf8_strnwidth for strings with SGR sequences and corner case length
>   * arguments.
> diff --git a/utf8.c b/utf8.c
> index 1b88324ea3..8d4b159e1d 100644
> --- a/utf8.c
> +++ b/utf8.c
> @@ -229,6 +229,7 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
>  	while (string && string < orig + len) {
>  		int glyph_width;
>  		size_t skip;
> +		size_t remainder;
>  
>  		if (skip_ansi ) {
>  			while ((skip = display_mode_esc_sequence_len_counted(string,
> @@ -239,7 +240,9 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
>  				break;
>  		}
>  
> -		glyph_width = utf8_width(&string, NULL);
> +		remainder = orig + len - string;
> +
> +		glyph_width = utf8_width(&string, &remainder);
>  		if (glyph_width > 0)
>  			width += glyph_width;
>  	}
Previous: Dirk Gouders
Message 26 of 26 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
  9. 0/4 utf8: bounds-related fixes and a doc fixDirk Gouders, Nov 18, 2026
  10. 1/4 utf8: add display_mode_esc_sequence_len_counted()Dirk Gouders, Nov 18, 2026
  11. Andy KoppeNov 19, 2026
  12. Dirk GoudersNov 19, 2026
  13. Andy KoppeNov 19, 2026
  14. Andy KoppeNov 19, 2026
  15. Dirk GoudersNov 19, 2026
  16. Dirk GoudersNov 19, 2026
  17. Dirk GoudersNov 19, 2026
  18. Junio C HamanoNov 19, 2026
  19. Dirk GoudersNov 19, 2026
  20. 2/4 utf8: fix comment for utf8_strnwidth()Dirk Gouders, Nov 18, 2026
  21. Andy KoppeNov 18, 2026
  22. Dirk GoudersNov 19, 2026
  23. 3/4 utf8: respect string boundaries after skipping ANSI escape sequencesDirk Gouders, Nov 18, 2026
  24. Junio C HamanoNov 18, 2026
  25. 4/4 utf8: respect boundaries in utf8_strnwidth()Dirk Gouders, Nov 18, 2026
  26. Junio C HamanoNov 18, 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.