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

Re: [PATCH 1/2] utf8: respect bounds in utf8_strnwidth()

From
Dirk Gouders <dirk@gouders.net>
Date
Nov 17, 2026, 17:57 UTC
Message-ID
<ghbj7nmjd7.fsf@gouders.net>
In-Reply-To
<xmqqse0zfn2n.fsf@gitster.g>
On Tue, Nov 17 2026, Junio C Hamano wrote:
Show 16 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
>
>> The problem is well spotted, but isn't the blame slightly misplaced?
>> ...
>> Even if display_mode_esc_sequence_len() were updated to take the
>> remaining number of bytes in string (which I think *is* a good
>> change, by the way), ...
>
> An update to display_mode_esc_sequence_len() to take the length
> limit, including its fallout, is shown below, and it does not look
> too bad.  Not even compile-tested, but it should be sufficient as an
> illustration of the idea.
>
> This does not make your "after display_mode_esc_sequence_len()
> skips, make sure if we still have remaining string to grab one UTF-8
> character" change unnecessary, as I said.

My v2 would have introduced two new functions (see attached) to replace display_mode_esc_sequence_len():

• size_t ansi_sgr_nlen(const char *str, size_t len); • size_t ansi_sgr_nlen_multi(const char *s, size_t len);

The latter one to help (most of all) users who want to eat *all* leading SGR sequences so that they don't need loops.

But I saw you already posted a patch and that is also fine, of course.
Dirk
diff --git a/utf8.c b/utf8.c
index 96460cc414..1e3887480f 100644
--- a/utf8.c
+++ b/utf8.c
@@ -30,6 +30,44 @@ size_t display_mode_esc_sequence_len(const char *s)
 	return p - s;
 }
 
+size_t ansi_sgr_nlen(const char *str, size_t len)
+{
+	const char *p = str;
+	const char *p_max = str + len;
+
+	if (len < 3)
+		return 0;
+
+	if (*p++ != '\033')
+		return 0;
+	if (*p++ != '[')
+		return 0;
+
+	while (p < p_max && (isdigit(*p) || *p == ';'))
+		p++;
+
+	if (p == p_max || *p != 'm')
+		return 0;
+
+	return p - str;
+}
+
+size_t ansi_sgr_nlen_multi(const char *s, size_t len)
+{
+	size_t sgr_len = 0;
+
+	while (sgr_len < len) {
+		size_t count = ansi_sgr_nlen(s + sgr_len, len - sgr_len);
+
+		if (!count)
+			break;
+
+		sgr_len += count;
+	}
+
+	return sgr_len;
+}
+
 /* auxiliary function for binary search in interval table */
 static int bisearch(ucs_char_t ucs, const struct interval *table, int max)
 {
Previous: Junio C HamanoNext: Junio C Hamano
Message 6 of 8 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

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.