Thread View: linux.kernel.git
3 messages
3 total messages
Started by Junio C Hamano
Tue, 17 Nov 2026 00:02
Re: [PATCH 1/2] utf8: respect bounds in utf8_strnwidth()
Author: Junio C Hamano
Date: Tue, 17 Nov 2026 00:02
Date: Tue, 17 Nov 2026 00:02
76 lines
15303 bytes
15303 bytes
Dirk Gouders <dirk@gouders.net> writes:
> 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.
The problem is well spotted, but isn't the blame slightly misplaced?
The preimage with missing context looks like this:
int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
{
const char *orig = string;
size_t width = 0;
while (string && string < orig + len) {
int glyph_width;
size_t skip;
while (skip_ansi &&
(skip = display_mode_esc_sequence_len(string)) != 0)
string += skip;
glyph_width = utf8_width(&string, NULL);
if (glyph_width > 0)
width += glyph_width;
}
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), the loop it is in may cause the entirety of the
string up to the "len" bytes to be skipped. Because the original
lacks a guard before the call to utf8_width(), however, we end up
calling it even if string points beyond the given region of bytes
orig[0,len).
If you blame the lack of such a guard before utf8_width() (or after
the loop that has display_mode_esc_sequence_len()), it would match
the proposed fix. But I doubt it is entirely fair to blame the lack
of an explicit boundary in display_mode_esc_sequence_len() for the
breakage you are addressing.
> Fix this by adding a boundary-check after looping with
> display_mode_esc_sequence_len() to ensure we operate strictly within
> the given len.
If the incoming "string" is at the end of an mmapped region (beyond
the end of which nothing is mapped), reading string[len] would give
you SIGBUS, so display_mode_esc_sequence_len() would already have
taken you into trouble. In other words, even though such a check
before utf8_width() is better than nothing, in a sense it is too
late, no?
> 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)
Re: [PATCH 1/2] utf8: respect bounds in utf8_strnwidth()
Author: Junio C Hamano
Date: Tue, 17 Nov 2026 16:18
Date: Tue, 17 Nov 2026 16:18
121 lines
16443 bytes
16443 bytes
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.
pretty.c | 3 ++-
utf8.c | 27 ++++++++++++++++++---------
utf8.h | 2 +-
3 files changed, 21 insertions(+), 11 deletions(-)
diff --git c/pretty.c w/pretty.c
index 0bc8ad8a9a..a20fd61909 100644
--- c/pretty.c
+++ w/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 c/utf8.c w/utf8.c
index 96460cc414..544c5329f3 100644
--- c/utf8.c
+++ w/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 c/utf8.h w/utf8.h
index cf8ecb0f21..fb2227a9e2 100644
--- c/utf8.h
+++ w/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);
Re: [PATCH 1/2] utf8: respect bounds in utf8_strnwidth()
Author: Junio C Hamano
Date: Tue, 17 Nov 2026 18:11
Date: Tue, 17 Nov 2026 18:11
29 lines
13861 bytes
13861 bytes
Dirk Gouders <dirk@gouders.net> writes: >> 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(): A two-function arrangement would be a good idea for maintainability. Add display_mode_esc_sequence_len_counted() to use the counted string API, with display_mode_esc_sequence_len() keeping the original calling convention (i.e., we call strlen(s) then call the new helper). I was too lazy to do the function pair in the illustration patch, which caused all existing callers to be affected, but given that there is another topic or two that are just being proposed that would add more callers to this function, it probably makes sense to keep a function with the NUL-terminated string API as before, and add the counted interface for callers that care, like the one you are fixing. Thanks.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads