From: Junio C Hamano <gitster@pobox.com>
To: Dirk Gouders <dirk@gouders.net>
Cc: git@vger.kernel.org, "Junio C Hamano <gitster@pobox.com>,
Andy Koppe" <andy.koppe@gmail.com>
Subject: Re: [PATCH v5 1/4] utf8: add display_mode_esc_sequence_len_counted()
Date: Mon, 23 Nov 2026 09:04:02 -0800 [thread overview]
Message-ID: <xmqqa4mza399.fsf@gitster.g> (raw)
In-Reply-To: <20261122-master-v5-1-313bd1bb3c45@gouders.net> (Dirk Gouders's message of "Sun, 22 Nov 2026 15:51:24 +0100")
Dirk Gouders <dirk@gouders.net> writes:
> From: Junio C Hamano <gitster@pobox.com>
>
> display_mode_esc_sequence_len() scans for an ANSI display mode
> escape sequence (e.g. "ESC [ 1 ; 31 m") at the start of a string,
> relying on NUL termination to stop. Some callers, however, work on
> a counted buffer: utf8_strnwidth() takes <string, len> and must not
> scan past it, while strbuf_utf8_replace() and
> strbuf_add_wrapped_text() know (or can cheaply learn) the end.
>
> Add display_mode_esc_sequence_len_counted() taking <ptr, len>,
> which never reads at or beyond ptr[len], and make
> display_mode_esc_sequence_len() a thin wrapper for NUL-terminated
> strings.
Good.
> Use the counted variant in utf8_strnwidth() for correctness, and in
> strbuf_utf8_replace() and strbuf_add_wrapped_text() (computing the
> end once upfront in the latter) to avoid measuring the remaining
> text in each iteration.
OK, but the latter needs a slight fix (see below).
> [Dirk Gouders: allow SIZE_MAX to be passed for NUL-terminated strings
> to avoid O(n²) degradation as suggested by Andy Koppe. Also add
> tests for these functions.]
>
> Helped-by: Andy Koppe <andy.koppe@gmail.com>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> Signed-off-by: Dirk Gouders <dirk@gouders.net>
Minor nit. These ought to be a chronological record of custodial chain.
The original author's sign-off comes first, then Helped-by and [note on
the help in square brackets], and finally the sign-off of person who is
relaying the final package.
> diff --git a/t/unit-tests/u-utf8-ansi.c b/t/unit-tests/u-utf8-ansi.c
> new file mode 100644
> index 0000000000..d969737000
> --- /dev/null
> +++ b/t/unit-tests/u-utf8-ansi.c
> @@ -0,0 +1,86 @@
> +#include "unit-test.h"
> +#include "utf8.h"
> +#include "strbuf.h"
Do we use <strbuf.h> at this stage? I do not think so (and
<unit-test.h> includes it anyway).
> +
> +/*
> + * Test display_mode_esc_sequence_len_counted() for NUL-terminated strings.
> + */
> +void test_utf8_ansi__sgr_len_counted(void)
> +{
> + cl_assert_equal_i(0, display_mode_esc_sequence_len_counted("", SIZE_MAX));
> +
> + cl_assert_equal_i(0, display_mode_esc_sequence_len_counted("\033", SIZE_MAX));
> +...
> + cl_assert_equal_i(7, display_mode_esc_sequence_len_counted("\033[31;0ma", SIZE_MAX));
It is sad that these lines have to become this long.
As the values returned from display_mode_esc_sequence_len_counted() and
friends are all size_t, not signed, cl_assert_equal_u() would be a much
more appropriate assertion here.
See how t/unit-tests/u-odb-inmemory.c:cl_assert_object_info() nearby
uses it to check values of type size_t.
> +/*
> + * Test display_mode_esc_sequence_len_counted() with end pointers.
Probably with "length limits" instead of "end pointers" in this
iteration.
> diff --git a/utf8.c b/utf8.c
> index 96460cc414..503edb8edc 100644
> --- a/utf8.c
> +++ b/utf8.c
> @@ -16,18 +16,31 @@ struct interval {
> ucs_char_t last;
> };
>
> -size_t display_mode_esc_sequence_len(const char *s)
> +/*
> + * Return the length of the ANSI display mode escape sequence (e.g.,
> + * "\033[1;31m") that appears at the beginning of the "len" bytes
> + * of string "s", or 0 if the bytes do not begin with a complete
> + * such sequence. Never looks at s[len] or beyond.
> + */
> +size_t display_mode_esc_sequence_len_counted(const char *s, size_t len)
> {
> - const char *p = s;
> - if (*p++ != '\033')
> + size_t i = 0;
> +
> + if (i >= len || s[i++] != '\033')
> return 0;
> - if (*p++ != '[')
> + if (i >= len || s[i++] != '[')
> return 0;
> - while (isdigit(*p) || *p == ';')
> - p++;
> - if (*p++ != 'm')
> + while (i < len && (isdigit(s[i]) || s[i] == ';'))
> + i++;
> + if (i >= len || s[i++] != 'm')
> return 0;
Among 4 comparisons between i and len, only one of them follow the
"textual order reflects actual order" principle. Can we revert it back
to the version I gave you to compare (len <= i) instead, if we allow the
commit to claim to be from me, as I wouldn't write (i >= len) in these
places if I were writing this patch myself.
> + return i;
> +}
> /* auxiliary function for binary search in interval table */
> @@ -218,7 +231,8 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
> size_t skip;
>
> while (skip_ansi &&
> - (skip = display_mode_esc_sequence_len(string)) != 0)
> + (skip = display_mode_esc_sequence_len_counted(string,
> + orig + len - string)) != 0)
> string += skip;
>
> glyph_width = utf8_width(&string, NULL);
> @@ -279,6 +293,7 @@ void strbuf_add_wrapped_text(struct strbuf *buf,
> {
> int indent, w, assume_utf8 = 1;
> const char *bol, *space, *start = text;
> + const char *end = text + strlen(text);
This strlen() call is unconditionally made, even before the check to see
if we divert to strbuf_add_indented_text() with "if (width <= 0)";
measuring the length of text in that code path is wasted.
> size_t orig_len = buf->len;
>
> if (width <= 0) {
> @@ -299,7 +314,8 @@ void strbuf_add_wrapped_text(struct strbuf *buf,
> char c;
> size_t skip;
>
> - while ((skip = display_mode_esc_sequence_len(text)))
> + while ((skip = display_mode_esc_sequence_len_counted(text,
> + end - text)))
> text += skip;
OK, I guess.
It is a bit sad that we cannot use the trick similar to passing SIZE_MAX
to avoid having to prescan to find "end", though.
> c = *text;
> @@ -378,7 +394,8 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
> int glyph_width;
> size_t n;
>
> - while ((n = display_mode_esc_sequence_len(src))) {
> + while ((n = display_mode_esc_sequence_len_counted(src,
> + end - src))) {
> strbuf_add(&dst, src, n);
> src += n;
> }
> diff --git a/utf8.h b/utf8.h
> index cf8ecb0f21..57ca2ab270 100644
> --- a/utf8.h
> +++ b/utf8.h
> @@ -6,6 +6,7 @@ struct strbuf;
> typedef unsigned int ucs_char_t; /* assuming 32bit int */
>
> size_t display_mode_esc_sequence_len(const char *s);
> +size_t display_mode_esc_sequence_len_counted(const char *s, size_t len);
> int utf8_width(const char **start, size_t *remainder_p);
> int utf8_strnwidth(const char *string, size_t len, int skip_ansi);
> int utf8_strwidth(const char *string);
next prev parent reply other threads:[~2026-11-23 17:04 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 [this message]
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
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=xmqqa4mza399.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=andy.koppe@gmail.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