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

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

From
Dirk Gouders <dirk@gouders.net>
Date
Nov 16, 2026, 18:57 UTC
Message-ID
<20261116-master-v1-1-dc2a50ce0aa0@gouders.net>
In-Reply-To
<20261116-master-v1-0-dc2a50ce0aa0@gouders.net>

utf8_strnwidth() takes a len argument, but because display_mode_esc_sequence_len() works without an explicit boundary, there are cases when utf8_strnwidth() operates beyond the given len boundary.

Fix this by adding a boundary-check after looping with display_mode_esc_sequence_len() to ensure we operate strictly within the given len.

Further, add three corner case tests of which two will fail without
the fix:
------------------------------------------------------------------------
not ok 257 - utf8_width::strnwidth_ansi
    ---
    reason: |
      Expected comparison to hold: 0 == utf8_strnwidth("\033[31mhello", 4, 1)
      0 != 1
    at:
      file: 't/unit-tests/u-utf8-width.c'
      line: 11
      function: 'test_utf8_width__strnwidth_ansi'
    ...
------------------------------------------------------------------------

With the fix: ------------------------------------------------------------------------ ok 257 - utf8_width::strnwidth_ansi ------------------------------------------------------------------------

Signed-off-by: Dirk Gouders <dirk@gouders.net>
---
 t/unit-tests/u-utf8-width.c | 11 +++++++++++
 utf8.c                      | 10 +++++++---
 2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/t/unit-tests/u-utf8-width.c b/t/unit-tests/u-utf8-width.c
index 86e09c3574..60538fc26f 100644
--- a/t/unit-tests/u-utf8-width.c
+++ b/t/unit-tests/u-utf8-width.c
@@ -2,6 +2,17 @@
 #include "utf8.h"
 #include "strbuf.h"
 
+/*
+ * Test utf8_strnwidth for strings with SGR sequences and corner case length
+ * arguments.
+ */
+void test_utf8_width__strnwidth_ansi(void)
+{
+	cl_assert_equal_i(0, utf8_strnwidth("\033[31mhello", 4, 1));
+	cl_assert_equal_i(0, utf8_strnwidth("\033[31mhello", 5, 1));
+	cl_assert_equal_i(1, utf8_strnwidth("\033[31mhello", 6, 1));
+}
+
 /*
  * Test utf8_strnwidth with various Chinese strings
  * Chinese characters typically have a width of 2 columns when displayed
diff --git a/utf8.c b/utf8.c
index 96460cc414..7e050bb2c4 100644
--- a/utf8.c
+++ b/utf8.c
@@ -217,9 +217,13 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
 		int glyph_width;
 		size_t skip;
 
-		while (skip_ansi &&
-		       (skip = display_mode_esc_sequence_len(string)) != 0)
-			string += skip;
+		if (skip_ansi) {
+			while ((skip = display_mode_esc_sequence_len(string)) != 0)
+				string += skip;
+
+			if (string >= orig + len)
+				break;
+		}
 
 		glyph_width = utf8_width(&string, NULL);
 		if (glyph_width > 0)
-- 
2.55.0
Previous: Dirk GoudersNext: Junio C Hamano
Message 2 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.