Git development
 help / color / mirror / Atom feed
From: Dirk Gouders <dirk@gouders.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, "Junio C Hamano <gitster@pobox.com>,
	Andy Koppe" <andy.koppe@gmail.com>
Subject: Re: [PATCH v5 3/4] utf8: respect string boundaries after skipping ANSI escape sequences
Date: Tue, 24 Nov 2026 10:53:00 +0100	[thread overview]
Message-ID: <ghmrqyimir.fsf@gouders.net> (raw)
In-Reply-To: <xmqq1p8ba395.fsf@gitster.g>

On Mon, Nov 23 2026, Junio C Hamano wrote:

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

Yes, even for the one who observed this it can be difficult to
reconstruct the needed imaginary after a few days.

I will try to provide an example.

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

Ok, I will leave a similar comment.

I also noticed the comment and started an .org file to note what the
callers (and theirs ...) do with the return values and what changes are
needed.  In quite some case those int's end as widths specifiers in
format strings and I concluded I'd better stay away from this task.

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

  reply	other threads:[~2026-11-24  9:53 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 18:56 [PATCH 0/2] utf8: two minor fixes Dirk Gouders
2026-11-16 18:57 ` [PATCH 1/2] utf8: respect bounds in utf8_strnwidth() Dirk Gouders
2026-11-17  0:02   ` Junio C Hamano
2026-11-17 15:15     ` Dirk Gouders
2026-11-17 16:18     ` Junio C Hamano
2026-11-17 17:57       ` Dirk Gouders
2026-11-17 18:11         ` Junio C Hamano
2026-11-16 18:57 ` [PATCH 2/2] utf8: fix comment for utf8_strnwidth() Dirk Gouders
2026-11-18 21:08 ` [PATCH v2 0/4] utf8: bounds-related fixes and a doc fix Dirk Gouders
2026-11-18 21:08   ` [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted() Dirk Gouders
2026-11-19  5:54     ` Andy Koppe
2026-11-19  7:43       ` Dirk Gouders
2026-11-19 20:58         ` Andy Koppe
2026-11-19 21:04           ` Andy Koppe
2026-11-19 21:33             ` Dirk Gouders
2026-11-19 22:32       ` Dirk Gouders
2026-11-19 22:59         ` Dirk Gouders
2026-11-20 10:16         ` Andy Koppe
2026-11-20 16:36           ` Junio C Hamano
2026-11-20 18:03             ` Andy Koppe
2026-11-19 19:59     ` Junio C Hamano
2026-11-19 20:28       ` Dirk Gouders
2026-11-18 21:08   ` [PATCH v2 2/4] utf8: fix comment for utf8_strnwidth() Dirk Gouders
2026-11-18 22:40     ` Andy Koppe
2026-11-19  5:58       ` Dirk Gouders
2026-11-18 21:08   ` [PATCH v2 3/4] utf8: respect string boundaries after skipping ANSI escape sequences Dirk Gouders
2026-11-18 21:49     ` Junio C Hamano
2026-11-18 21:08   ` [PATCH v2 4/4] utf8: respect boundaries in utf8_strnwidth() Dirk Gouders
2026-11-18 21:50     ` Junio C Hamano
2026-11-20 14:44 ` [PATCH v3 0/4] utf8: bounds-related fixes and a doc fix Dirk Gouders
2026-11-20 14:44   ` [PATCH v3 1/4] utf8: introduce bounds for display_mode_esc_sequence_len() Dirk Gouders
2026-11-20 14:44   ` [PATCH v3 2/4] utf8: fix comment for utf8_strnwidth() Dirk Gouders
2026-11-20 14:44   ` [PATCH v3 3/4] utf8: respect string boundaries after skipping ANSI escape sequences Dirk Gouders
2026-11-20 14:44   ` [PATCH v3 4/4] utf8: respect boundaries in utf8_strnwidth() Dirk Gouders
2026-11-20 15:09   ` [PATCH v3 0/4] utf8: bounds-related fixes and a doc fix Dirk Gouders
2026-11-20 15:11 ` [PATCH v4 0/5] " Dirk Gouders
2026-11-20 15:11   ` [PATCH v4 1/5] utf8: introduce bounds for display_mode_esc_sequence_len() Dirk Gouders
2026-11-22  7:06     ` Andy Koppe
2026-11-22  9:23       ` Dirk Gouders
2026-11-22 13:25       ` Dirk Gouders
2026-11-22  7:25     ` Andy Koppe
2026-11-22  9:27       ` Dirk Gouders
2026-11-20 15:11   ` [PATCH v4 2/5] utf8: fix comment for utf8_strnwidth() Dirk Gouders
2026-11-20 15:11   ` [PATCH v4 3/5] utf8: respect string boundaries after skipping ANSI escape sequences Dirk Gouders
2026-11-20 15:11   ` [PATCH v4 4/5] utf8: respect boundaries in utf8_strnwidth() Dirk Gouders
2026-11-20 15:11   ` [PATCH v4 5/5] utf8: introduce display_mode_esc_sequence_len_all() Dirk Gouders
2026-11-22  6:49   ` [PATCH v4 0/5] utf8: bounds-related fixes and a doc fix Andy Koppe
2026-11-22  9:19     ` Dirk Gouders
2026-11-22 14:51 ` [PATCH v5 0/4] " Dirk Gouders
2026-11-22 14:51   ` [PATCH v5 1/4] utf8: add display_mode_esc_sequence_len_counted() Dirk Gouders
2026-11-23 17:04     ` Junio C Hamano
2026-11-23 19:02       ` Dirk Gouders
2026-11-22 14:51   ` [PATCH v5 2/4] utf8: fix comment for utf8_strnwidth() Dirk Gouders
2026-11-23 17:04     ` Junio C Hamano
2026-11-22 14:51   ` [PATCH v5 3/4] utf8: respect string boundaries after skipping ANSI escape sequences Dirk Gouders
2026-11-23 17:04     ` Junio C Hamano
2026-11-24  9:53       ` Dirk Gouders [this message]
2026-11-22 14:51   ` [PATCH v5 4/4] utf8: respect boundaries in utf8_strnwidth() Dirk Gouders
2026-11-23 17:04     ` Junio C Hamano
2026-11-23 17:24   ` [PATCH v5 0/4] utf8: bounds-related fixes and a doc fix Junio C Hamano
2026-11-25  9:56     ` Dirk Gouders
2026-11-25 18:19   ` [PATCH v6 " Dirk Gouders
2026-11-25 18:19     ` [PATCH v6 1/4] utf8: add display_mode_esc_sequence_len_counted() Dirk Gouders
2026-11-25 18:19     ` [PATCH v6 2/4] utf8: fix comment for utf8_strnwidth() Dirk Gouders
2026-11-25 18:19     ` [PATCH v6 3/4] utf8: respect string boundaries after skipping ANSI escape sequences Dirk Gouders
2026-11-25 18:19     ` [PATCH v6 4/4] utf8: use bound for utf8_width() calls in utf8_strnwidth() Dirk Gouders
2026-11-25 19:40     ` [PATCH v6 0/4] utf8: bounds-related fixes and a doc fix Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ghmrqyimir.fsf@gouders.net \
    --to=dirk@gouders.net \
    --cc=andy.koppe@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox