Git development
 help / color / mirror / Atom feed
From: Dirk Gouders <dirk@gouders.net>
To: git@vger.kernel.org
Cc: "Junio C Hamano <gitster@pobox.com>,
	Andy Koppe" <andy.koppe@gmail.com>,
	Dirk Gouders <dirk@gouders.net>,
	Junio C Hamano <gitster@pobox.com>
Subject: [PATCH v6 1/4] utf8: add display_mode_esc_sequence_len_counted()
Date: Wed, 25 Nov 2026 19:19:41 +0100	[thread overview]
Message-ID: <20261125-master-v6-1-cb60af83d189@gouders.net> (raw)
In-Reply-To: <20261125-master-v6-0-cb60af83d189@gouders.net>

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() knows 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.

Use the counted variant in utf8_strnwidth() for correctness, and in
strbuf_utf8_replace() where the length can easily be calculated.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Helped-by: Andy Koppe <andy.koppe@gmail.com>
[ak: warned against O(n²) degradation and suggested to use SIZE_MAX]
Signed-off-by: Dirk Gouders <dirk@gouders.net>
---
 Makefile                   |  1 +
 t/meson.build              |  1 +
 t/unit-tests/u-utf8-ansi.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++
 utf8.c                     | 35 +++++++++++++------
 utf8.h                     |  1 +
 5 files changed, 113 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile
index 4c84eea6e2..db8f7f5c60 100644
--- a/Makefile
+++ b/Makefile
@@ -1563,6 +1563,7 @@ CLAR_TEST_SUITES += u-string-list
 CLAR_TEST_SUITES += u-strvec
 CLAR_TEST_SUITES += u-trailer
 CLAR_TEST_SUITES += u-urlmatch-normalization
+CLAR_TEST_SUITES += u-utf8-ansi
 CLAR_TEST_SUITES += u-utf8-width
 CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
 CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
diff --git a/t/meson.build b/t/meson.build
index 1cf1d95f70..ae7479a98e 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -26,6 +26,7 @@ clar_test_suites = [
   'unit-tests/u-strvec.c',
   'unit-tests/u-trailer.c',
   'unit-tests/u-urlmatch-normalization.c',
+  'unit-tests/u-utf8-ansi.c',
   'unit-tests/u-utf8-width.c',
 ]
 
diff --git a/t/unit-tests/u-utf8-ansi.c b/t/unit-tests/u-utf8-ansi.c
new file mode 100644
index 0000000000..a3c76b1ed3
--- /dev/null
+++ b/t/unit-tests/u-utf8-ansi.c
@@ -0,0 +1,85 @@
+#include "unit-test.h"
+#include "utf8.h"
+
+/*
+ * Test display_mode_esc_sequence_len_counted() for NUL-terminated strings.
+ */
+void test_utf8_ansi__sgr_len_counted(void)
+{
+	cl_assert_equal_u(0, display_mode_esc_sequence_len_counted("", SIZE_MAX));
+
+	cl_assert_equal_u(0, display_mode_esc_sequence_len_counted("\033", SIZE_MAX));
+	cl_assert_equal_u(0, display_mode_esc_sequence_len_counted("\033[", SIZE_MAX));
+	cl_assert_equal_u(3, display_mode_esc_sequence_len_counted("\033[m", SIZE_MAX));
+	cl_assert_equal_u(3, display_mode_esc_sequence_len_counted("\033[ma", SIZE_MAX));
+
+	cl_assert_equal_u(0, display_mode_esc_sequence_len_counted("\033[3", SIZE_MAX));
+	cl_assert_equal_u(0, display_mode_esc_sequence_len_counted("\033[31", SIZE_MAX));
+	cl_assert_equal_u(5, display_mode_esc_sequence_len_counted("\033[31m", SIZE_MAX));
+	cl_assert_equal_u(5, display_mode_esc_sequence_len_counted("\033[31ma", SIZE_MAX));
+
+	cl_assert_equal_u(0, display_mode_esc_sequence_len_counted("\033[31;", SIZE_MAX));
+	cl_assert_equal_u(6, display_mode_esc_sequence_len_counted("\033[31;m", SIZE_MAX));
+	cl_assert_equal_u(6, display_mode_esc_sequence_len_counted("\033[31;ma", SIZE_MAX));
+
+	cl_assert_equal_u(7, display_mode_esc_sequence_len_counted("\033[31;0m", SIZE_MAX));
+	cl_assert_equal_u(7, display_mode_esc_sequence_len_counted("\033[31;0ma", SIZE_MAX));
+}
+
+/*
+ * Same test as above for the wrapper display_mode_esc_sequence_len().
+ */
+void test_utf8_ansi__sgr_len(void)
+{
+	cl_assert_equal_u(0, display_mode_esc_sequence_len(""));
+
+	cl_assert_equal_u(0, display_mode_esc_sequence_len("\033"));
+	cl_assert_equal_u(0, display_mode_esc_sequence_len("\033["));
+	cl_assert_equal_u(3, display_mode_esc_sequence_len("\033[m"));
+	cl_assert_equal_u(3, display_mode_esc_sequence_len("\033[ma"));
+
+	cl_assert_equal_u(0, display_mode_esc_sequence_len("\033[3"));
+	cl_assert_equal_u(0, display_mode_esc_sequence_len("\033[31"));
+	cl_assert_equal_u(5, display_mode_esc_sequence_len("\033[31m"));
+	cl_assert_equal_u(5, display_mode_esc_sequence_len("\033[31ma"));
+
+	cl_assert_equal_u(0, display_mode_esc_sequence_len("\033[31;"));
+	cl_assert_equal_u(6, display_mode_esc_sequence_len("\033[31;m"));
+	cl_assert_equal_u(6, display_mode_esc_sequence_len("\033[31;ma"));
+
+	cl_assert_equal_u(7, display_mode_esc_sequence_len("\033[31;0m"));
+	cl_assert_equal_u(7, display_mode_esc_sequence_len("\033[31;0ma"));
+}
+
+/*
+ * Test display_mode_esc_sequence_len_counted() with length limits.
+ */
+void test_utf8_ansi__sgr_len_limited(void)
+{
+	/* sequences followed by one non-sequence character */
+	char sgr_1[] = "\033[ma";
+	char sgr_2[] = "\033[31ma";
+	char sgr_3[] = "\033[31;ma";
+	char sgr_4[] = "\033[31;0ma";
+
+	cl_assert_equal_u(0, display_mode_esc_sequence_len_counted(sgr_1, 0));
+	cl_assert_equal_u(0, display_mode_esc_sequence_len_counted(sgr_1, 1));
+	cl_assert_equal_u(0, display_mode_esc_sequence_len_counted(sgr_1, 2));
+	cl_assert_equal_u(3, display_mode_esc_sequence_len_counted(sgr_1, 3));
+	cl_assert_equal_u(3, display_mode_esc_sequence_len_counted(sgr_1, 4));
+
+	cl_assert_equal_u(0, display_mode_esc_sequence_len_counted(sgr_2, 3));
+	cl_assert_equal_u(0, display_mode_esc_sequence_len_counted(sgr_2, 4));
+	cl_assert_equal_u(5, display_mode_esc_sequence_len_counted(sgr_2, 5));
+	cl_assert_equal_u(5, display_mode_esc_sequence_len_counted(sgr_2, 6));
+
+	cl_assert_equal_u(6, display_mode_esc_sequence_len_counted(sgr_3,
+								   sizeof(sgr_3) - 2));
+	cl_assert_equal_u(6, display_mode_esc_sequence_len_counted(sgr_3,
+								   sizeof(sgr_3) - 1));
+
+	cl_assert_equal_u(7, display_mode_esc_sequence_len_counted(sgr_4,
+								   sizeof(sgr_4) - 2));
+	cl_assert_equal_u(7, display_mode_esc_sequence_len_counted(sgr_4,
+								   sizeof(sgr_4) - 1));
+}
diff --git a/utf8.c b/utf8.c
index 96460cc414..b161a5ee48 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 (len <= i || s[i++] != '\033')
 		return 0;
-	if (*p++ != '[')
+	if (len <= i || s[i++] != '[')
 		return 0;
-	while (isdigit(*p) || *p == ';')
-		p++;
-	if (*p++ != 'm')
+	while (i < len && (isdigit(s[i]) || s[i] == ';'))
+		i++;
+	if (len <= i || s[i++] != 'm')
 		return 0;
-	return p - s;
+	return i;
+}
+
+/* Same as above, but for a NUL-terminated string "s". */
+size_t display_mode_esc_sequence_len(const char *s)
+{
+	return display_mode_esc_sequence_len_counted(s, SIZE_MAX);
 }
 
 /* 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);
@@ -378,7 +392,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);

-- 
2.55.0


  reply	other threads:[~2026-11-25 18:20 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
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     ` Dirk Gouders [this message]
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=20261125-master-v6-1-cb60af83d189@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