🚀 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 3/4] utf8: respect string boundaries after skipping ANSI escape sequences
#3643
Author: Junio C Hamano
Date: Mon, 23 Nov 2026 17:04
65 lines
14624 bytes
Dirk Gouders <dirk@gouders.net> writes:

> utf8_strnwidth() can skip ANSI escape sequences and it is possible
> that such sequences end the string within the given boundary.
>
> Check for such situations before proceeding and calling utf8_width().
>
> Add tests that test such corner cases.

It would become clearer if the above described externally observable
symptom and the condition to trigger it.

"such sequences end the string within the given boundary" is slightly
imprecise, isn't it?  IIUC, the problem is that an escape sequence
reaching orig + len would cause utf8_width() to read past len, which
leads it to count extra columns or reading out-of-bounds memory.

The code change looks correct, though ;-)

> diff --git a/t/unit-tests/u-utf8-width.c b/t/unit-tests/u-utf8-width.c
> index 86e09c3574..5e2b636db9 100644
> --- a/t/unit-tests/u-utf8-width.c
> +++ b/t/unit-tests/u-utf8-width.c
> @@ -2,6 +2,17 @@
>  #include "utf8.h"
>  #include "strbuf.h"
>  
> +/*
> + * Test utf8_strnwidth for strings with SGR sequences and corner case length
> + * arguments.
> + */
> +void test_utf8_width__strnwidth_ansi(void)
> +{
> +	cl_assert_equal_i(3, utf8_strnwidth("\033[31mhello", 4, 1));
> +	cl_assert_equal_i(0, utf8_strnwidth("\033[31mhello", 5, 1));
> +	cl_assert_equal_i(1, utf8_strnwidth("\033[31mhello", 6, 1));
> +}

These are OK to be equal_i() for now, but I notice a TODO at the bottom
of utf8_strnwidth().  It might be helpful to leave a comment to warn
that this test needs to be updated when it happens?

> diff --git a/utf8.c b/utf8.c
> index 009e8fee39..dfc5261619 100644
> --- a/utf8.c
> +++ b/utf8.c
> @@ -230,10 +230,14 @@ 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_counted(string,
> -							orig + len - string)) != 0)
> -			string += skip;
> +		if (skip_ansi) {
> +			while ((skip = display_mode_esc_sequence_len_counted(string,
> +					                orig + len - string)))
> +				string += skip;
> +
> +			if (string >= orig + len)
> +				break;
> +		}

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