From: Junio C Hamano <gitster@pobox.com>
To: Dirk Gouders <dirk@gouders.net>
Cc: git@vger.kernel.org
Subject: Re: [PATCH v2 4/4] utf8: respect boundaries in utf8_strnwidth()
Date: Wed, 18 Nov 2026 13:50:25 -0800 [thread overview]
Message-ID: <xmqqo6bl3j32.fsf@gitster.g> (raw)
In-Reply-To: <20261118-master-v2-4-3f2aaec15d31@gouders.net> (Dirk Gouders's message of "Wed, 18 Nov 2026 22:08:53 +0100")
Dirk Gouders <dirk@gouders.net> writes:
> 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.
>
> 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;
> }
prev parent reply other threads:[~2026-11-18 21:50 UTC|newest]
Thread overview: 15+ 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-18 21:08 ` [PATCH v2 2/4] utf8: fix comment for utf8_strnwidth() 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 [this message]
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=xmqqo6bl3j32.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=dirk@gouders.net \
--cc=git@vger.kernel.org \
/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