git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()

From
Dirk Gouders <dirk@gouders.net>
Date
Nov 18, 2026, 21:08 UTC
Message-ID
<20261118-master-v2-1-3f2aaec15d31@gouders.net>
In-Reply-To
<20261118-master-v2-0-3f2aaec15d31@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() 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.

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.

Also, add some tests for the new function.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Dirk Gouders <dirk@gouders.net>
---
 Makefile                   |  1 +
 t/meson.build              |  1 +
 t/unit-tests/u-utf8-ansi.c | 14 ++++++++++++++
 utf8.c                     | 35 ++++++++++++++++++++++++++---------
 utf8.h                     |  1 +
 5 files changed, 43 insertions(+), 9 deletions(-)
diff --git a/Makefile b/Makefile
index 4c84eea6e2..3c8dc9ca40 100644
--- a/Makefile
+++ b/Makefile
@@ -1564,6 +1564,7 @@ CLAR_TEST_SUITES += u-strvec
 CLAR_TEST_SUITES += u-trailer
 CLAR_TEST_SUITES += u-urlmatch-normalization
 CLAR_TEST_SUITES += u-utf8-width
+CLAR_TEST_SUITES += u-utf8-ansi
 CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
 CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
diff --git a/t/meson.build b/t/meson.build
index 1cf1d95f70..e224df58c8 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -27,6 +27,7 @@ clar_test_suites = [
   'unit-tests/u-trailer.c',
   'unit-tests/u-urlmatch-normalization.c',
   'unit-tests/u-utf8-width.c',
+  'unit-tests/u-utf8-ansi.c',
 ]
 
 clar_sources = [
diff --git a/t/unit-tests/u-utf8-ansi.c b/t/unit-tests/u-utf8-ansi.c
new file mode 100644
index 0000000000..c19d8bce78
--- /dev/null
+++ b/t/unit-tests/u-utf8-ansi.c
@@ -0,0 +1,14 @@
+#include "unit-test.h"
+#include "utf8.h"
+#include "strbuf.h"
+
+/*
+ * Test len limited functions for ANSI SGR lengths.
+ */
+void test_utf8_ansi__sgr_len(void)
+{
+	cl_assert_equal_i(0, display_mode_esc_sequence_len_counted("\033[m", 2));
+	cl_assert_equal_i(3, display_mode_esc_sequence_len_counted("\033[m", 3));
+	cl_assert_equal_i(5, display_mode_esc_sequence_len_counted("\033[31m", 5));
+	cl_assert_equal_i(7, display_mode_esc_sequence_len_counted("\033[0;31m", 7));
+}
diff --git a/utf8.c b/utf8.c
index 96460cc414..d0546ab31f 100644
--- a/utf8.c
+++ b/utf8.c
@@ -16,20 +16,33 @@ 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')
+	const char *p = s, *end = s + len;
+
+	if (end <= p || *p++ != '\033')
 		return 0;
-	if (*p++ != '[')
+	if (end <= p || *p++ != '[')
 		return 0;
-	while (isdigit(*p) || *p == ';')
+	while (p < end && (isdigit(*p) || *p == ';'))
 		p++;
-	if (*p++ != 'm')
+	if (end <= p || *p++ != 'm')
 		return 0;
 	return p - s;
 }
 
+/* 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, strlen(s));
+}
+
 /* auxiliary function for binary search in interval table */
 static int bisearch(ucs_char_t ucs, const struct interval *table, int max)
 {
@@ -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);
 	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;
 
 		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);
-- 
2.55.0
Previous: Dirk GoudersNext: Andy Koppe
Message 10 of 26 in “utf8: two minor fixes”
  1. 0/2 utf8: two minor fixesDirk Gouders, Nov 16, 2026
  2. 1/2 utf8: respect bounds in utf8_strnwidth()Dirk Gouders, Nov 16, 2026
  3. Junio C HamanoNov 17, 2026
  4. Dirk GoudersNov 17, 2026
  5. Junio C HamanoNov 17, 2026
  6. Dirk GoudersNov 17, 2026
  7. Junio C HamanoNov 17, 2026
  8. 2/2 utf8: fix comment for utf8_strnwidth()Dirk Gouders, Nov 16, 2026
  9. 0/4 utf8: bounds-related fixes and a doc fixDirk Gouders, Nov 18, 2026
  10. 1/4 utf8: add display_mode_esc_sequence_len_counted()Dirk Gouders, Nov 18, 2026
  11. Andy KoppeNov 19, 2026
  12. Dirk GoudersNov 19, 2026
  13. Andy KoppeNov 19, 2026
  14. Andy KoppeNov 19, 2026
  15. Dirk GoudersNov 19, 2026
  16. Dirk GoudersNov 19, 2026
  17. Dirk GoudersNov 19, 2026
  18. Junio C HamanoNov 19, 2026
  19. Dirk GoudersNov 19, 2026
  20. 2/4 utf8: fix comment for utf8_strnwidth()Dirk Gouders, Nov 18, 2026
  21. Andy KoppeNov 18, 2026
  22. Dirk GoudersNov 19, 2026
  23. 3/4 utf8: respect string boundaries after skipping ANSI escape sequencesDirk Gouders, Nov 18, 2026
  24. Junio C HamanoNov 18, 2026
  25. 4/4 utf8: respect boundaries in utf8_strnwidth()Dirk Gouders, Nov 18, 2026
  26. Junio C HamanoNov 18, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.