🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.git
1 messages
1 total messages Started by Junio C Hamano Mon, 23 Nov 2026 17:04
Re: [PATCH v5 4/4] utf8: respect boundaries in utf8_strnwidth()
#3645
Author: Junio C Hamano
Date: Mon, 23 Nov 2026 17:04
76 lines
15196 bytes
Dirk Gouders <dirk@gouders.net> writes:

> Subject: Re: [PATCH v5 4/4] utf8: respect boundaries in utf8_strnwidth()

Micronit.  I recall 3/4 was also making utf8_strnwidth() respect
boundaries.  Can we tighten the title to be a bit more specific to what
this patch does, e.g., utf8: bound utf8_width() calls in utf8_strnwidth()
or somtheing?

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

If you follow the convention to mark functions with () suffix,
consistently do so also for utf8_width().  It is a very good change to
make sure that the called utf8_width() will not read beyond the known
end-of-region the caller has.



> 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(-)
>
> 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 dfc5261619..f7fb419545 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;
>  	}
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads