From: Dirk Gouders <dirk@gouders.net>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
Dirk Gouders <dirk@gouders.net>,
Andy Koppe <andy.koppe@gmail.com>
Subject: [PATCH v3 1/4] utf8: introduce bounds for display_mode_esc_sequence_len()
Date: Fri, 20 Nov 2026 15:44:24 +0100 [thread overview]
Message-ID: <20261120-master-v3-1-d28e8c75d26d@gouders.net> (raw)
In-Reply-To: <20261120-master-v3-0-d28e8c75d26d@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.
Introduce an optional end pointer to display_mode_esc_sequence_len()
which then never reads *end or beyond when end is not NULL.
If end is NULL, display_mode_esc_sequence_len() assumes a
NUL terminated string as before.
Andy Koppe pointed out the risk of performance degrading to O(n²)
when functions process strings and have to check each position for an
escape sequence [1]. This change doesn't carry this risk.
Further, add thorough tests for both end pointer cases.
[1] https://lore.kernel.org/git/CAHWeT-aWx1VwUjM+hZ--jjSAbcMenz5Mw11y9ouFavGndUMrSA@mail.gmail.com/
Cc: Andy Koppe <andy.koppe@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Dirk Gouders <dirk@gouders.net>
---
Makefile | 1 +
pretty.c | 2 +-
t/meson.build | 1 +
t/unit-tests/u-utf8-ansi.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++
utf8.c | 34 ++++++++++++++++----------
utf8.h | 2 +-
6 files changed, 85 insertions(+), 14 deletions(-)
diff --git a/Makefile b/Makefile
index 4c84eea6e2..db8f7f5c60 100644
--- a/Makefile
+++ b/Makefile
@@ -1563,6 +1563,7 @@ CLAR_TEST_SUITES += u-string-list
CLAR_TEST_SUITES += u-strvec
CLAR_TEST_SUITES += u-trailer
CLAR_TEST_SUITES += u-urlmatch-normalization
+CLAR_TEST_SUITES += u-utf8-ansi
CLAR_TEST_SUITES += u-utf8-width
CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
diff --git a/pretty.c b/pretty.c
index d8a9f370f6..58dfeb61f4 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1848,7 +1848,7 @@ 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))
break;
/*
* got a good ansi sequence, put it back to
diff --git a/t/meson.build b/t/meson.build
index 1cf1d95f70..ae7479a98e 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -26,6 +26,7 @@ clar_test_suites = [
'unit-tests/u-strvec.c',
'unit-tests/u-trailer.c',
'unit-tests/u-urlmatch-normalization.c',
+ 'unit-tests/u-utf8-ansi.c',
'unit-tests/u-utf8-width.c',
]
diff --git a/t/unit-tests/u-utf8-ansi.c b/t/unit-tests/u-utf8-ansi.c
new file mode 100644
index 0000000000..cb75c8612b
--- /dev/null
+++ b/t/unit-tests/u-utf8-ansi.c
@@ -0,0 +1,59 @@
+#include "unit-test.h"
+#include "utf8.h"
+#include "strbuf.h"
+
+/*
+ * Test display_mode_esc_sequence_len() for NUL terminated strings.
+ */
+void test_utf8_ansi__sgr_len_null(void)
+{
+ cl_assert_equal_i(0, display_mode_esc_sequence_len("", NULL));
+
+ cl_assert_equal_i(0, display_mode_esc_sequence_len("\033", NULL));
+ cl_assert_equal_i(0, display_mode_esc_sequence_len("\033[", NULL));
+ cl_assert_equal_i(3, display_mode_esc_sequence_len("\033[m", NULL));
+
+ cl_assert_equal_i(0, display_mode_esc_sequence_len("\033[3", NULL));
+ cl_assert_equal_i(0, display_mode_esc_sequence_len("\033[31", NULL));
+ cl_assert_equal_i(5, display_mode_esc_sequence_len("\033[31m", NULL));
+
+ cl_assert_equal_i(0, display_mode_esc_sequence_len("\033[31;", NULL));
+ cl_assert_equal_i(6, display_mode_esc_sequence_len("\033[31;m", NULL));
+ cl_assert_equal_i(7, display_mode_esc_sequence_len("\033[31;0m", NULL));
+}
+
+/*
+ * Test display_mode_esc_sequence_len() with end pointers.
+ */
+void test_utf8_ansi__sgr_len_end_ptr(void)
+{
+ char *sgr_1 = (char *)"\033[m";
+ char *sgr_2 = (char *)"\033[31m";
+ char *sgr_3 = (char *)"\033[31;m";
+ char *sgr_4 = (char *)"\033[31;0m";
+
+ cl_assert_equal_i(0, display_mode_esc_sequence_len("", ""));
+
+ cl_assert_equal_i(0, display_mode_esc_sequence_len(sgr_1, sgr_1 + 1));
+ cl_assert_equal_i(0, display_mode_esc_sequence_len(sgr_1, sgr_1 + 2));
+ cl_assert_equal_i(3, display_mode_esc_sequence_len(sgr_1, sgr_1 + 3));
+
+ cl_assert_equal_i(0, display_mode_esc_sequence_len(sgr_2, sgr_2 + 1));
+ cl_assert_equal_i(0, display_mode_esc_sequence_len(sgr_2, sgr_2 + 2));
+ cl_assert_equal_i(0, display_mode_esc_sequence_len(sgr_2, sgr_2 + 3));
+ cl_assert_equal_i(0, display_mode_esc_sequence_len(sgr_2, sgr_2 + 4));
+ cl_assert_equal_i(5, display_mode_esc_sequence_len(sgr_2, sgr_2 + 5));
+
+ cl_assert_equal_i(6, display_mode_esc_sequence_len(sgr_3, sgr_3 + sizeof(sgr_3) - 1));
+ cl_assert_equal_i(7, display_mode_esc_sequence_len(sgr_4, sgr_4 + sizeof(sgr_4) - 1));
+}
+
+/*
+ * Test display_mode_esc_sequence_len() for swapped start and end pointers.
+ */
+void test_utf8_ansi__sgr_len_start_end_swap(void)
+{
+ char *sgr_1 = (char *)"\033[m";
+
+ cl_assert_equal_i(0, display_mode_esc_sequence_len(sgr_1 + 3, sgr_1));
+}
diff --git a/utf8.c b/utf8.c
index 96460cc414..48aa189624 100644
--- a/utf8.c
+++ b/utf8.c
@@ -16,18 +16,27 @@ 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 string starting at start,
+ * or 0 if the string does not begin with a complete such sequence.
+ * If end is NULL, assume a NUL terminated string, else never look at *end or
+ * beyond.
+ */
+size_t display_mode_esc_sequence_len(const char *start, const char *end)
{
- const char *p = s;
- if (*p++ != '\033')
+ const char *p = start;
+
+ if ((end && end <= p) || *p++ != '\033')
return 0;
- if (*p++ != '[')
+ if ((end && end <= p) || *p++ != '[')
return 0;
- while (isdigit(*p) || *p == ';')
+ while (((end && p < end) || (!end && *p)) && (isdigit(*p) || *p == ';'))
p++;
- if (*p++ != 'm')
+ if ((end && end <= p) || (!end && !*p) || *p++ != 'm')
return 0;
- return p - s;
+
+ return p - start;
}
/* auxiliary function for binary search in interval table */
@@ -210,15 +219,15 @@ int utf8_width(const char **start, size_t *remainder_p)
*/
int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
{
- const char *orig = string;
+ const char *end = string + len;
size_t width = 0;
- while (string && string < orig + len) {
+ while (string && string < end) {
int glyph_width;
size_t skip;
while (skip_ansi &&
- (skip = display_mode_esc_sequence_len(string)) != 0)
+ (skip = display_mode_esc_sequence_len(string, end)))
string += skip;
glyph_width = utf8_width(&string, NULL);
@@ -279,6 +288,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 +309,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 += skip;
c = *text;
@@ -378,7 +388,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))) {
strbuf_add(&dst, src, n);
src += n;
}
diff --git a/utf8.h b/utf8.h
index cf8ecb0f21..7eb7bf042a 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 *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
next prev 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 ` Dirk Gouders [this message]
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 ` [PATCH v3 4/4] utf8: respect boundaries in utf8_strnwidth() Dirk Gouders
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-1-d28e8c75d26d@gouders.net \
--to=dirk@gouders.net \
--cc=andy.koppe@gmail.com \
--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