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

[PATCH] utf8: make display_mode_esc_sequence_len() take a length

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 17, 2026, 16:50 UTC
Message-ID
<xmqqmrr7fllq.fsf@gitster.g>

display_mode_esc_sequence_len() scans for an ANSI display mode escape sequence (e.g. "ESC [ 1 ; 31 m"), relying on NUL termination to stop. Some callers, however, use a counted <ptr, len> buffer or need to stop at a specific position.

Teach it to take a buffer length and never read at or beyond ptr[len], returning the length of a complete sequence at the start or 0 otherwise.

Update the callers to pass the remaining byte count. For strbuf_add_wrapped_text(), which takes a NUL-terminated string, compute the end once upfront instead of calling strlen() each time.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * Copy of https://lore.kernel.org/git/xmqqse0zfn2n.fsf@gitster.g/
   with a proposed commit log message.
 pretty.c |  3 ++-
 utf8.c   | 27 ++++++++++++++++++---------
 utf8.h   |  2 +-
 3 files changed, 21 insertions(+), 11 deletions(-)
diff --git a/pretty.c b/pretty.c
index 0bc8ad8a9a..a20fd61909 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1834,7 +1834,8 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
 			while (p > sb->buf && ch - p < 10 && *p != '\033')
 				p--;
 			if (*p != '\033' ||
-			    ch + 1 - p != display_mode_esc_sequence_len(p))
+			    ch + 1 - p != display_mode_esc_sequence_len(p,
+								ch + 1 - p))
 				break;
 			/*
 			 * got a good ansi sequence, put it back to
diff --git a/utf8.c b/utf8.c
index 96460cc414..544c5329f3 100644
--- a/utf8.c
+++ b/utf8.c
@@ -16,16 +16,23 @@ 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(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;
 }
@@ -218,7 +225,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(string,
+							     orig + len - string)) != 0)
 			string += skip;
 
 		glyph_width = utf8_width(&string, NULL);
@@ -279,6 +287,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 +308,7 @@ 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(text, end - text)))
 			text += skip;
 
 		c = *text;
@@ -378,7 +387,7 @@ 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(src, end - src))) {
 			strbuf_add(&dst, src, n);
 			src += n;
 		}
diff --git a/utf8.h b/utf8.h
index cf8ecb0f21..fb2227a9e2 100644
--- a/utf8.h
+++ b/utf8.h
@@ -5,7 +5,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(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.56.1-740-gf38658f3d0
Next: Junio C Hamano
Message 1 of 15 in “utf8: make display_mode_esc_sequence_len() take a length”
  1. utf8: make display_mode_esc_sequence_len() take a lengthJunio C Hamano, Nov 17, 2026
  2. utf8: add display_mode_esc_sequence_len_counted()Junio C Hamano, Nov 17, 2026
  3. Dirk GoudersNov 17, 2026
  4. Junio C HamanoNov 17, 2026
  5. Dirk GoudersNov 17, 2026
  6. Junio C HamanoNov 18, 2026
  7. Dirk GoudersNov 18, 2026
  8. Andy KoppeNov 17, 2026
  9. Junio C HamanoNov 17, 2026
  10. Andy KoppeNov 18, 2026
  11. Dirk GoudersNov 18, 2026
  12. Andy KoppeNov 18, 2026
  13. Dirk GoudersNov 18, 2026
  14. Andy KoppeNov 18, 2026
  15. Dirk GoudersNov 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.