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>,
	Andy Koppe <andy.koppe@gmail.com>
Subject: [PATCH v4 0/5] utf8: bounds-related fixes and a doc fix
Date: Fri, 20 Nov 2026 16:11:23 +0100	[thread overview]
Message-ID: <20261120-master-v4-0-5ff523db7285@gouders.net> (raw)
In-Reply-To: <20261116-master-v1-0-dc2a50ce0aa0@gouders.net>

A patch here on the list made me look at
display_mode_esc_sequence_len() in utf8.c and I found two details in
the file that are perhaps worth a fix:

1) utf8_strnwidth() disregards the given len argument when fed
   with proper string and len.

2) The comment for utf8_strnwidth() is incorrect: it states the
   function returns strlen() for cases when it actually returns its
   len argument.

Signed-off-by: Dirk Gouders <dirk@gouders.net>
---
Changes in v4:
- Resend un-squashed v3

Changes in v3:
- 1/5: * fix sorting order in t/meson.build
       * fix risk of O(n²) degradation:
         drop the wrapper function and introduce an end pointer to
         display_mode_esc_sequence_len()
         (I was so used to len arguments for string handling functions
          that I only now realized the beauty of optionally optional
          end pointers for string handling. I know: beauty is in the
          eye of the beholder ;-))
       * try to fix an overly long line
- 2/5: also fix a further error concerning "null-terminated"
- 3/5: * drop excess SP in if-statement
- 5/5: this actually is a POC, not fixing a bug:
       users of display_mode_esc_sequence_len() often need the length
       of all leading SGR sequences and here is a function that does that.
       I mentioned such a function in the discussion for v1.
- Link to v2: https://patch.msgid.link/20261118-master-v2-0-3f2aaec15d31@gouders.net

Changes in v2:
- Add a patch by Junio that introduces a bounded function display_mode_esc_sequence_len_counted()
  I added some tests for this new function.
- 2/2 became 2/4
- 3/4 is the modified 1/2 and uses the new
  display_mode_esc_sequence_len_counted() so that the while loop stays
  within the given bounds.
  The commit message was adjusted.
  One of the tests was wrong: "\033" has width 0, so the sum must be 3 instead of 4.
- 4/4 is an addition to fix a similar problem with utf8_width().
  That function is used without its optional bounds argument and
  can violate boundaries as well.  Test for such cases are added.
- Link to v1: https://patch.msgid.link/20261116-master-v1-0-dc2a50ce0aa0@gouders.net

---
Dirk Gouders (4):
      utf8: fix comment for utf8_strnwidth()
      utf8: respect string boundaries after skipping ANSI escape sequences
      utf8: respect boundaries in utf8_strnwidth()
      utf8: introduce display_mode_esc_sequence_len_all()

Junio C Hamano (1):
      utf8: introduce bounds for display_mode_esc_sequence_len()

 Makefile                    |  1 +
 pretty.c                    |  2 +-
 t/meson.build               |  1 +
 t/unit-tests/u-utf8-ansi.c  | 85 +++++++++++++++++++++++++++++++++++++++++++++
 t/unit-tests/u-utf8-width.c | 20 +++++++++++
 utf8.c                      | 78 +++++++++++++++++++++++++++++------------
 utf8.h                      |  3 +-
 7 files changed, 165 insertions(+), 25 deletions(-)

Range-diff versus v3:

1:  122df176a3 = 1:  ce2736da52 utf8: introduce bounds for display_mode_esc_sequence_len()
2:  6cb2da91ae = 2:  ec70d5e553 utf8: fix comment for utf8_strnwidth()
3:  d62360d690 = 3:  0e09e16822 utf8: respect string boundaries after skipping ANSI escape sequences
-:  ---------- > 4:  f49010e6c5 utf8: respect boundaries in utf8_strnwidth()
4:  caca770f8b ! 5:  eb428dfc47 utf8: respect boundaries in utf8_strnwidth()
    @@ Metadata
     Author: Dirk Gouders <dirk@gouders.net>
     
      ## Commit message ##
    -    utf8: respect boundaries in utf8_strnwidth()
    +    utf8: introduce display_mode_esc_sequence_len_all()
     
    -    utf8_strnwidth() can be called with boundaries that effectively cause
    -    incomplete multibyte sequences and we should return "len" in such
    -    cases.
    +    Most users of display_mode_esc_sequence_len() need to know the length
    +    of *all* leading ANSI display mode escape sequences and use
    +    display_mode_esc_sequence_len() in a loop.
     
    -    But, utf8_width is used without its optional boundary argument and
    -    will thus work beyond the given boundary.
    +    Introduce display_mode_esc_sequence_len_all() for convenience and
    +    to reduce the risk of bugs.
     
    -    Fix this by using the boundary argument for utf8_width.
    -    Add some tests that cover such situations.
    +    Add tests for this function as well.
     
         Signed-off-by: Dirk Gouders <dirk@gouders.net>
     
    @@ t/unit-tests/u-utf8-ansi.c
       * Test display_mode_esc_sequence_len() for NUL terminated strings.
       */
     
    - ## t/unit-tests/u-utf8-width.c ##
    -@@
    - #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.
    -
      ## utf8.c ##
     @@ utf8.c: size_t display_mode_esc_sequence_len(const char *start, const char *end)
      	return p - start;
    @@ utf8.c: size_t display_mode_esc_sequence_len(const char *start, const char *end)
      static int bisearch(ucs_char_t ucs, const struct interval *table, int max)
      {
     @@ utf8.c: 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;
    + 		size_t remainder;
      
      		if (skip_ansi) {
     -			while ((skip = display_mode_esc_sequence_len(string, end)))
    @@ utf8.c: int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
      
      			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;
    - 	}
     @@ utf8.c: void strbuf_add_wrapped_text(struct strbuf *buf,
      
      	for (;;) {

---
base-commit: 7fb6055e678f800320d8ab8cd8f201d9493d1ba0
change-id: 20261116-master-69b171612823


  parent reply	other threads:[~2026-11-20 15:11 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   ` [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 ` Dirk Gouders [this message]
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-v4-0-5ff523db7285@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