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>, Dirk Gouders <dirk@gouders.net>
Subject: [PATCH v3 4/4] utf8: respect boundaries in utf8_strnwidth()
Date: Fri, 20 Nov 2026 15:44:27 +0100	[thread overview]
Message-ID: <20261120-master-v3-4-d28e8c75d26d@gouders.net> (raw)
In-Reply-To: <20261120-master-v3-0-d28e8c75d26d@gouders.net>

utf8_strnwidth() can be called with boundaries that effectively cause
incomplete multibyte sequences and we should return "len" in such
cases.

But, utf8_width is used without its optional boundary argument and
will thus work beyond the given boundary.

Fix this by using the boundary argument for utf8_width.
Add some tests that cover such situations.

Signed-off-by: Dirk Gouders <dirk@gouders.net>
---
 t/unit-tests/u-utf8-ansi.c  | 26 ++++++++++++++++++++++++++
 t/unit-tests/u-utf8-width.c |  9 +++++++++
 utf8.c                      | 40 +++++++++++++++++++++++++++++-----------
 utf8.h                      |  1 +
 4 files changed, 65 insertions(+), 11 deletions(-)

diff --git a/t/unit-tests/u-utf8-ansi.c b/t/unit-tests/u-utf8-ansi.c
index cb75c8612b..909643d07d 100644
--- a/t/unit-tests/u-utf8-ansi.c
+++ b/t/unit-tests/u-utf8-ansi.c
@@ -2,6 +2,32 @@
 #include "utf8.h"
 #include "strbuf.h"
 
+/*
+ * Test the greedy function display_mode_esc_sequence_len_all().
+ */
+void test_utf8_ansi__sgr_len_greedy(void)
+{
+	char *s1 = (char *)"no ESC sequence";
+	char *s2 = (char *)"\033[m";
+	char *s3 = (char *)"\033[0;31m";
+	char *s4 = (char *)"\033[0m\033[31m";
+
+	/* no sequences */
+	cl_assert_equal_i(0, display_mode_esc_sequence_len_all("", ""));
+	cl_assert_equal_i(0, display_mode_esc_sequence_len_all(s1, s1 + 5));
+
+	/* single sequences */
+	cl_assert_equal_i(3, display_mode_esc_sequence_len_all(s2, s2 + 3));
+	cl_assert_equal_i(7, display_mode_esc_sequence_len_all(s3, s3 + 7));
+	cl_assert_equal_i(0, display_mode_esc_sequence_len_all(s3, s3 + 3));
+
+	/* multiple sequences */
+	cl_assert_equal_i(4, display_mode_esc_sequence_len_all(s4, s4 + 4));
+	cl_assert_equal_i(9, display_mode_esc_sequence_len_all(s4, s4 + 9));
+	cl_assert_equal_i(0, display_mode_esc_sequence_len_all(s4, s4 + 3));
+	cl_assert_equal_i(4, display_mode_esc_sequence_len_all(s4, s4 + 8));
+}
+
 /*
  * Test display_mode_esc_sequence_len() for NUL terminated strings.
  */
diff --git a/t/unit-tests/u-utf8-width.c b/t/unit-tests/u-utf8-width.c
index 5e2b636db9..c4df4dd6a6 100644
--- a/t/unit-tests/u-utf8-width.c
+++ b/t/unit-tests/u-utf8-width.c
@@ -2,6 +2,15 @@
 #include "utf8.h"
 #include "strbuf.h"
 
+/*
+ * Test utf8_strnwidth for cjk characters invalidated by len argument.
+ */
+void test_utf8_width__strnwidth_cjk_chopped(void)
+{
+	cl_assert_equal_i(1, utf8_strnwidth("三bytes long, 二columns wide", 1, 1));
+	cl_assert_equal_i(2, utf8_strnwidth("三bytes long, 二columns wide", 3, 1));
+}
+
 /*
  * Test utf8_strnwidth for strings with SGR sequences and corner case length
  * arguments.
diff --git a/utf8.c b/utf8.c
index 493f723e3b..9478985cbb 100644
--- a/utf8.c
+++ b/utf8.c
@@ -39,6 +39,26 @@ size_t display_mode_esc_sequence_len(const char *start, const char *end)
 	return p - start;
 }
 
+/*
+ * Count the lenght of all leading ANSI display mode escape sequences that
+ * are a prefix of the string at start up to end.
+ */
+size_t display_mode_esc_sequence_len_all(const char *start, const char *end)
+{
+	size_t seq_len = 0;
+
+	while (start + seq_len < end) {
+		size_t count = display_mode_esc_sequence_len(start + seq_len, end);
+
+		if (!count)
+			break;
+
+		seq_len += count;
+	}
+
+	return seq_len;
+}
+
 /* auxiliary function for binary search in interval table */
 static int bisearch(ucs_char_t ucs, const struct interval *table, int max)
 {
@@ -224,17 +244,18 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
 
 	while (string && string < end) {
 		int glyph_width;
-		size_t skip;
+		size_t remainder;
 
 		if (skip_ansi) {
-			while ((skip = display_mode_esc_sequence_len(string, end)))
-				string += skip;
+			string += display_mode_esc_sequence_len_all(string, end);
 
 			if (string >= end)
 				break;
 		}
 
-		glyph_width = utf8_width(&string, NULL);
+		remainder = end - string;
+
+		glyph_width = utf8_width(&string, &remainder);
 		if (glyph_width > 0)
 			width += glyph_width;
 	}
@@ -311,10 +332,8 @@ void strbuf_add_wrapped_text(struct strbuf *buf,
 
 	for (;;) {
 		char c;
-		size_t skip;
 
-		while ((skip = display_mode_esc_sequence_len(text, end)))
-			text += skip;
+		text += display_mode_esc_sequence_len_all(text, end);
 
 		c = *text;
 		if (!c || isspace(c)) {
@@ -392,10 +411,9 @@ 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, end))) {
-			strbuf_add(&dst, src, n);
-			src += n;
-		}
+		n = display_mode_esc_sequence_len_all(src, end);
+		strbuf_add(&dst, src, n);
+		src += n;
 
 		if (src >= end)
 			break;
diff --git a/utf8.h b/utf8.h
index 7eb7bf042a..fdde5af782 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 *start, const char *end);
+size_t display_mode_esc_sequence_len_all(const char *start, const char *end);
 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


  parent reply	other threads:[~2026-11-20 14:44 UTC|newest]

Thread overview: 41+ 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   ` Dirk Gouders [this message]
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-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

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=20261120-master-v3-4-d28e8c75d26d@gouders.net \
    --to=dirk@gouders.net \
    --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