All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dirk Gouders <dirk@gouders.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 1/2] utf8: respect bounds in utf8_strnwidth()
Date: Tue, 17 Nov 2026 16:15:00 +0100	[thread overview]
Message-ID: <ghcxs3v6a3.fsf@gouders.net> (raw)
In-Reply-To: <xmqqld6siau9.fsf@gitster.g>

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

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

Yes, I'm not sure if that term is correct but the main problem seems to
be a "contract mismatch": a function that accepts a boundary argument
calls one function display_mode_esc_sequence_len() that doesn't have an
explicit boundary and for a second one utf8_width() it doesn't use the
optional boundary argument.

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

Correct.

I am currently preparing a v2 that adresses all the above.
I will try to give that v2 sufficient time to hopefully reduce errors in
my workflow and not steal other people's time.

Regards,

Dirk

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

  reply	other threads:[~2026-11-17 15:15 UTC|newest]

Thread overview: 8+ 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 [this message]
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

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=ghcxs3v6a3.fsf@gouders.net \
    --to=dirk@gouders.net \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.