{"thread":{"id":"66858","subject":"[PATCH 0/2] utf8: two minor fixes","startedAt":"2026-11-16T18:56:59Z","lastAt":"2026-11-19T22:59:12Z","messageCount":26,"participants":["Dirk Gouders","Junio C Hamano","Andy Koppe"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"558569","messageId":"20261116-master-v1-0-dc2a50ce0aa0@gouders.net","threadId":"66858","inReplyTo":null,"subject":"[PATCH 0/2] utf8: two minor fixes","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-16T18:56:59Z","receivedAt":"2026-11-16T18:56:59Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"A patch here on the list made me look at\ndisplay_mode_esc_sequence_len() in utf8.c and I found two details in\nthe file that are perhaps worth a fix:\n\n1) utf8_strnwidth() disregards the given len argument when fed\n   with proper string and len.\n\n2) The comment for utf8_strnwidth() is incorrect: it states the\n   function returns strlen() for cases when it actually returns its\n   len argument.\n\nSigned-off-by: Dirk Gouders <dirk@gouders.net>\n---\nDirk Gouders (2):\n      utf8: respect bounds in utf8_strnwidth()\n      utf8: fix comment for utf8_strnwidth()\n\n t/unit-tests/u-utf8-width.c | 11 +++++++++++\n utf8.c                      | 12 ++++++++----\n 2 files changed, 19 insertions(+), 4 deletions(-)\n\n\n---\nbase-commit: 7fb6055e678f800320d8ab8cd8f201d9493d1ba0\nchange-id: 20261116-master-69b171612823\n\n\n"},{"id":"558570","messageId":"20261116-master-v1-1-dc2a50ce0aa0@gouders.net","threadId":"66858","inReplyTo":"20261116-master-v1-0-dc2a50ce0aa0@gouders.net","subject":"[PATCH 1/2] utf8: respect bounds in utf8_strnwidth()","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-16T18:57:00Z","receivedAt":"2026-11-16T18:57:00Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"utf8_strnwidth() takes a len argument, but because\ndisplay_mode_esc_sequence_len() works without an explicit boundary,\nthere are cases when utf8_strnwidth() operates beyond the given len\nboundary.\n\nFix this by adding a boundary-check after looping with\ndisplay_mode_esc_sequence_len() to ensure we operate strictly within\nthe given len.\n\nFurther, add three corner case tests of which two will fail without\nthe fix:\n------------------------------------------------------------------------\nnot ok 257 - utf8_width::strnwidth_ansi\n    ---\n    reason: |\n      Expected comparison to hold: 0 == utf8_strnwidth(\"\\033[31mhello\", 4, 1)\n      0 != 1\n    at:\n      file: 't/unit-tests/u-utf8-width.c'\n      line: 11\n      function: 'test_utf8_width__strnwidth_ansi'\n    ...\n------------------------------------------------------------------------\n\nWith the fix:\n------------------------------------------------------------------------\nok 257 - utf8_width::strnwidth_ansi\n------------------------------------------------------------------------\n\nSigned-off-by: Dirk Gouders <dirk@gouders.net>\n---\n t/unit-tests/u-utf8-width.c | 11 +++++++++++\n utf8.c                      | 10 +++++++---\n 2 files changed, 18 insertions(+), 3 deletions(-)\n\ndiff --git a/t/unit-tests/u-utf8-width.c b/t/unit-tests/u-utf8-width.c\nindex 86e09c3574..60538fc26f 100644\n--- a/t/unit-tests/u-utf8-width.c\n+++ b/t/unit-tests/u-utf8-width.c\n@@ -2,6 +2,17 @@\n #include \"utf8.h\"\n #include \"strbuf.h\"\n \n+/*\n+ * Test utf8_strnwidth for strings with SGR sequences and corner case length\n+ * arguments.\n+ */\n+void test_utf8_width__strnwidth_ansi(void)\n+{\n+\tcl_assert_equal_i(0, utf8_strnwidth(\"\\033[31mhello\", 4, 1));\n+\tcl_assert_equal_i(0, utf8_strnwidth(\"\\033[31mhello\", 5, 1));\n+\tcl_assert_equal_i(1, utf8_strnwidth(\"\\033[31mhello\", 6, 1));\n+}\n+\n /*\n  * Test utf8_strnwidth with various Chinese strings\n  * Chinese characters typically have a width of 2 columns when displayed\ndiff --git a/utf8.c b/utf8.c\nindex 96460cc414..7e050bb2c4 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -217,9 +217,13 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n \t\tint glyph_width;\n \t\tsize_t skip;\n \n-\t\twhile (skip_ansi &&\n-\t\t       (skip = display_mode_esc_sequence_len(string)) != 0)\n-\t\t\tstring += skip;\n+\t\tif (skip_ansi) {\n+\t\t\twhile ((skip = display_mode_esc_sequence_len(string)) != 0)\n+\t\t\t\tstring += skip;\n+\n+\t\t\tif (string >= orig + len)\n+\t\t\t\tbreak;\n+\t\t}\n \n \t\tglyph_width = utf8_width(&string, NULL);\n \t\tif (glyph_width > 0)\n\n-- \n2.55.0\n\n\n"},{"id":"558571","messageId":"20261116-master-v1-2-dc2a50ce0aa0@gouders.net","threadId":"66858","inReplyTo":"20261116-master-v1-0-dc2a50ce0aa0@gouders.net","subject":"[PATCH 2/2] utf8: fix comment for utf8_strnwidth()","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-16T18:57:01Z","receivedAt":"2026-11-16T18:57:01Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"The comment for utf8_strnwidth() states it returns strlen() for cases\nwhen the given string doesn't look like valid utf8, but it returns its\nargument len in such cases.\n\nFix the comment accordingly.\n\nSigned-off-by: Dirk Gouders <dirk@gouders.net>\n---\n utf8.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/utf8.c b/utf8.c\nindex 7e050bb2c4..e6d1510b60 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -205,7 +205,7 @@ int utf8_width(const char **start, size_t *remainder_p)\n \n /*\n  * Returns the total number of columns required by a null-terminated\n- * string, assuming that the string is utf8.  Returns strlen() instead\n+ * string, assuming that the string is utf8.  Returns len instead\n  * if the string does not look like a valid utf8 string.\n  */\n int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n\n-- \n2.55.0\n\n\n"},{"id":"558581","messageId":"xmqqld6siau9.fsf@gitster.g","threadId":"66858","inReplyTo":"20261116-master-v1-1-dc2a50ce0aa0@gouders.net","subject":"Re: [PATCH 1/2] utf8: respect bounds in utf8_strnwidth()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-17T00:02:38Z","receivedAt":"2026-11-17T00:02:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Dirk Gouders <dirk@gouders.net> writes:\n\n> utf8_strnwidth() takes a len argument, but because\n> display_mode_esc_sequence_len() works without an explicit boundary,\n> there are cases when utf8_strnwidth() operates beyond the given len\n> boundary.\n\nThe problem is well spotted, but isn't the blame slightly misplaced?\nThe preimage with missing context looks like this:\n\n    int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n    {\n            const char *orig = string;\n            size_t width = 0;\n\n            while (string && string < orig + len) {\n                    int glyph_width;\n                    size_t skip;\n\n                    while (skip_ansi &&\n                           (skip = display_mode_esc_sequence_len(string)) != 0)\n                            string += skip;\n\n                    glyph_width = utf8_width(&string, NULL);\n                    if (glyph_width > 0)\n                            width += glyph_width;\n            }\n\nEven if display_mode_esc_sequence_len() were updated to take the\nremaining number of bytes in string (which I think *is* a good\nchange, by the way), the loop it is in may cause the entirety of the\nstring up to the \"len\" bytes to be skipped.  Because the original\nlacks a guard before the call to utf8_width(), however, we end up\ncalling it even if string points beyond the given region of bytes\norig[0,len).\n\nIf you blame the lack of such a guard before utf8_width() (or after\nthe loop that has display_mode_esc_sequence_len()), it would match\nthe proposed fix.  But I doubt it is entirely fair to blame the lack\nof an explicit boundary in display_mode_esc_sequence_len() for the\nbreakage you are addressing.\n\n> Fix this by adding a boundary-check after looping with\n> display_mode_esc_sequence_len() to ensure we operate strictly within\n> the given len.\n\nIf the incoming \"string\" is at the end of an mmapped region (beyond\nthe end of which nothing is mapped), reading string[len] would give\nyou SIGBUS, so display_mode_esc_sequence_len() would already have\ntaken you into trouble.  In other words, even though such a check\nbefore utf8_width() is better than nothing, in a sense it is too\nlate, no?\n\n\n> diff --git a/utf8.c b/utf8.c\n> index 96460cc414..7e050bb2c4 100644\n> --- a/utf8.c\n> +++ b/utf8.c\n> @@ -217,9 +217,13 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n>  \t\tint glyph_width;\n>  \t\tsize_t skip;\n>  \n> -\t\twhile (skip_ansi &&\n> -\t\t       (skip = display_mode_esc_sequence_len(string)) != 0)\n> -\t\t\tstring += skip;\n> +\t\tif (skip_ansi) {\n> +\t\t\twhile ((skip = display_mode_esc_sequence_len(string)) != 0)\n> +\t\t\t\tstring += skip;\n> +\n> +\t\t\tif (string >= orig + len)\n> +\t\t\t\tbreak;\n> +\t\t}\n>  \n>  \t\tglyph_width = utf8_width(&string, NULL);\n>  \t\tif (glyph_width > 0)\n\n"},{"id":"558622","messageId":"ghcxs3v6a3.fsf@gouders.net","threadId":"66858","inReplyTo":"xmqqld6siau9.fsf@gitster.g","subject":"Re: [PATCH 1/2] utf8: respect bounds in utf8_strnwidth()","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-17T15:15:00Z","receivedAt":"2026-11-17T15:15:00Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"On Mon, Nov 16 2026, Junio C Hamano wrote:\n\n> Dirk Gouders <dirk@gouders.net> writes:\n>\n>> utf8_strnwidth() takes a len argument, but because\n>> display_mode_esc_sequence_len() works without an explicit boundary,\n>> there are cases when utf8_strnwidth() operates beyond the given len\n>> boundary.\n>\n> The problem is well spotted, but isn't the blame slightly misplaced?\n> The preimage with missing context looks like this:\n>\n>     int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n>     {\n>             const char *orig = string;\n>             size_t width = 0;\n>\n>             while (string && string < orig + len) {\n>                     int glyph_width;\n>                     size_t skip;\n>\n>                     while (skip_ansi &&\n>                            (skip = display_mode_esc_sequence_len(string)) != 0)\n>                             string += skip;\n>\n>                     glyph_width = utf8_width(&string, NULL);\n>                     if (glyph_width > 0)\n>                             width += glyph_width;\n>             }\n>\n> Even if display_mode_esc_sequence_len() were updated to take the\n> remaining number of bytes in string (which I think *is* a good\n> change, by the way), the loop it is in may cause the entirety of the\n> string up to the \"len\" bytes to be skipped.  Because the original\n> lacks a guard before the call to utf8_width(), however, we end up\n> calling it even if string points beyond the given region of bytes\n> orig[0,len).\n>\n> If you blame the lack of such a guard before utf8_width() (or after\n> the loop that has display_mode_esc_sequence_len()), it would match\n> the proposed fix.  But I doubt it is entirely fair to blame the lack\n> of an explicit boundary in display_mode_esc_sequence_len() for the\n> breakage you are addressing.\n\nYes, I'm not sure if that term is correct but the main problem seems to\nbe a \"contract mismatch\": a function that accepts a boundary argument\ncalls one function display_mode_esc_sequence_len() that doesn't have an\nexplicit boundary and for a second one utf8_width() it doesn't use the\noptional boundary argument.\n\n>> Fix this by adding a boundary-check after looping with\n>> display_mode_esc_sequence_len() to ensure we operate strictly within\n>> the given len.\n>\n> If the incoming \"string\" is at the end of an mmapped region (beyond\n> the end of which nothing is mapped), reading string[len] would give\n> you SIGBUS, so display_mode_esc_sequence_len() would already have\n> taken you into trouble.  In other words, even though such a check\n> before utf8_width() is better than nothing, in a sense it is too\n> late, no?\n\nCorrect.\n\nI am currently preparing a v2 that adresses all the above.\nI will try to give that v2 sufficient time to hopefully reduce errors in\nmy workflow and not steal other people's time.\n\nRegards,\n\nDirk\n\n>> diff --git a/utf8.c b/utf8.c\n>> index 96460cc414..7e050bb2c4 100644\n>> --- a/utf8.c\n>> +++ b/utf8.c\n>> @@ -217,9 +217,13 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n>>  \t\tint glyph_width;\n>>  \t\tsize_t skip;\n>>  \n>> -\t\twhile (skip_ansi &&\n>> -\t\t       (skip = display_mode_esc_sequence_len(string)) != 0)\n>> -\t\t\tstring += skip;\n>> +\t\tif (skip_ansi) {\n>> +\t\t\twhile ((skip = display_mode_esc_sequence_len(string)) != 0)\n>> +\t\t\t\tstring += skip;\n>> +\n>> +\t\t\tif (string >= orig + len)\n>> +\t\t\t\tbreak;\n>> +\t\t}\n>>  \n>>  \t\tglyph_width = utf8_width(&string, NULL);\n>>  \t\tif (glyph_width > 0)\n\n"},{"id":"558626","messageId":"xmqqse0zfn2n.fsf@gitster.g","threadId":"66858","inReplyTo":"xmqqld6siau9.fsf@gitster.g","subject":"Re: [PATCH 1/2] utf8: respect bounds in utf8_strnwidth()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-17T16:18:56Z","receivedAt":"2026-11-17T16:18:56Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> The problem is well spotted, but isn't the blame slightly misplaced?\n> ...\n> Even if display_mode_esc_sequence_len() were updated to take the\n> remaining number of bytes in string (which I think *is* a good\n> change, by the way), ...\n\nAn update to display_mode_esc_sequence_len() to take the length\nlimit, including its fallout, is shown below, and it does not look\ntoo bad.  Not even compile-tested, but it should be sufficient as an\nillustration of the idea.\n\nThis does not make your \"after display_mode_esc_sequence_len()\nskips, make sure if we still have remaining string to grab one UTF-8\ncharacter\" change unnecessary, as I said.\n\n\n pretty.c |  3 ++-\n utf8.c   | 27 ++++++++++++++++++---------\n utf8.h   |  2 +-\n 3 files changed, 21 insertions(+), 11 deletions(-)\n\ndiff --git c/pretty.c w/pretty.c\nindex 0bc8ad8a9a..a20fd61909 100644\n--- c/pretty.c\n+++ w/pretty.c\n@@ -1834,7 +1834,8 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */\n \t\t\twhile (p > sb->buf && ch - p < 10 && *p != '\\033')\n \t\t\t\tp--;\n \t\t\tif (*p != '\\033' ||\n-\t\t\t    ch + 1 - p != display_mode_esc_sequence_len(p))\n+\t\t\t    ch + 1 - p != display_mode_esc_sequence_len(p,\n+\t\t\t\t\t\t\t\tch + 1 - p))\n \t\t\t\tbreak;\n \t\t\t/*\n \t\t\t * got a good ansi sequence, put it back to\ndiff --git c/utf8.c w/utf8.c\nindex 96460cc414..544c5329f3 100644\n--- c/utf8.c\n+++ w/utf8.c\n@@ -16,16 +16,23 @@ struct interval {\n \tucs_char_t last;\n };\n \n-size_t display_mode_esc_sequence_len(const char *s)\n+/*\n+ * Return the length of the ANSI display mode escape sequence (e.g.,\n+ * \"\\033[1;31m\") that appears at the beginning of the \"len\" bytes\n+ * of string \"s\", or 0 if the bytes do not begin with a complete\n+ * such sequence.  Never looks at s[len] or beyond.\n+ */\n+size_t display_mode_esc_sequence_len(const char *s, size_t len)\n {\n-\tconst char *p = s;\n-\tif (*p++ != '\\033')\n+\tconst char *p = s, *end = s + len;\n+\n+\tif (end <= p || *p++ != '\\033')\n \t\treturn 0;\n-\tif (*p++ != '[')\n+\tif (end <= p || *p++ != '[')\n \t\treturn 0;\n-\twhile (isdigit(*p) || *p == ';')\n+\twhile (p < end && (isdigit(*p) || *p == ';'))\n \t\tp++;\n-\tif (*p++ != 'm')\n+\tif (end <= p || *p++ != 'm')\n \t\treturn 0;\n \treturn p - s;\n }\n@@ -218,7 +225,8 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n \t\tsize_t skip;\n \n \t\twhile (skip_ansi &&\n-\t\t       (skip = display_mode_esc_sequence_len(string)) != 0)\n+\t\t       (skip = display_mode_esc_sequence_len(string,\n+\t\t\t\t\t\t\t     orig + len - string)) != 0)\n \t\t\tstring += skip;\n \n \t\tglyph_width = utf8_width(&string, NULL);\n@@ -279,6 +287,7 @@ void strbuf_add_wrapped_text(struct strbuf *buf,\n {\n \tint indent, w, assume_utf8 = 1;\n \tconst char *bol, *space, *start = text;\n+\tconst char *end = text + strlen(text);\n \tsize_t orig_len = buf->len;\n \n \tif (width <= 0) {\n@@ -299,7 +308,7 @@ void strbuf_add_wrapped_text(struct strbuf *buf,\n \t\tchar c;\n \t\tsize_t skip;\n \n-\t\twhile ((skip = display_mode_esc_sequence_len(text)))\n+\t\twhile ((skip = display_mode_esc_sequence_len(text, end - text)))\n \t\t\ttext += skip;\n \n \t\tc = *text;\n@@ -378,7 +387,7 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,\n \t\tint glyph_width;\n \t\tsize_t n;\n \n-\t\twhile ((n = display_mode_esc_sequence_len(src))) {\n+\t\twhile ((n = display_mode_esc_sequence_len(src, end - src))) {\n \t\t\tstrbuf_add(&dst, src, n);\n \t\t\tsrc += n;\n \t\t}\ndiff --git c/utf8.h w/utf8.h\nindex cf8ecb0f21..fb2227a9e2 100644\n--- c/utf8.h\n+++ w/utf8.h\n@@ -5,7 +5,7 @@ struct strbuf;\n \n typedef unsigned int ucs_char_t;  /* assuming 32bit int */\n \n-size_t display_mode_esc_sequence_len(const char *s);\n+size_t display_mode_esc_sequence_len(const char *s, size_t len);\n int utf8_width(const char **start, size_t *remainder_p);\n int utf8_strnwidth(const char *string, size_t len, int skip_ansi);\n int utf8_strwidth(const char *string);\n\n"},{"id":"558641","messageId":"ghbj7nmjd7.fsf@gouders.net","threadId":"66858","inReplyTo":"xmqqse0zfn2n.fsf@gitster.g","subject":"Re: [PATCH 1/2] utf8: respect bounds in utf8_strnwidth()","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-17T17:57:08Z","receivedAt":"2026-11-17T17:57:08Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"On Tue, Nov 17 2026, Junio C Hamano wrote:\n\n> Junio C Hamano <gitster@pobox.com> writes:\n>\n>> The problem is well spotted, but isn't the blame slightly misplaced?\n>> ...\n>> Even if display_mode_esc_sequence_len() were updated to take the\n>> remaining number of bytes in string (which I think *is* a good\n>> change, by the way), ...\n>\n> An update to display_mode_esc_sequence_len() to take the length\n> limit, including its fallout, is shown below, and it does not look\n> too bad.  Not even compile-tested, but it should be sufficient as an\n> illustration of the idea.\n>\n> This does not make your \"after display_mode_esc_sequence_len()\n> skips, make sure if we still have remaining string to grab one UTF-8\n> character\" change unnecessary, as I said.\n\nMy v2 would have introduced two new functions (see attached) to replace\ndisplay_mode_esc_sequence_len():\n\n• size_t ansi_sgr_nlen(const char *str, size_t len);\n• size_t ansi_sgr_nlen_multi(const char *s, size_t len);\n\nThe latter one to help (most of all) users who want to eat *all* leading\nSGR sequences so that they don't need loops.\n\nBut I saw you already posted a patch and that is also fine, of course.\n\nDirk\n\ndiff --git a/utf8.c b/utf8.c\nindex 96460cc414..1e3887480f 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -30,6 +30,44 @@ size_t display_mode_esc_sequence_len(const char *s)\n \treturn p - s;\n }\n \n+size_t ansi_sgr_nlen(const char *str, size_t len)\n+{\n+\tconst char *p = str;\n+\tconst char *p_max = str + len;\n+\n+\tif (len < 3)\n+\t\treturn 0;\n+\n+\tif (*p++ != '\\033')\n+\t\treturn 0;\n+\tif (*p++ != '[')\n+\t\treturn 0;\n+\n+\twhile (p < p_max && (isdigit(*p) || *p == ';'))\n+\t\tp++;\n+\n+\tif (p == p_max || *p != 'm')\n+\t\treturn 0;\n+\n+\treturn p - str;\n+}\n+\n+size_t ansi_sgr_nlen_multi(const char *s, size_t len)\n+{\n+\tsize_t sgr_len = 0;\n+\n+\twhile (sgr_len < len) {\n+\t\tsize_t count = ansi_sgr_nlen(s + sgr_len, len - sgr_len);\n+\n+\t\tif (!count)\n+\t\t\tbreak;\n+\n+\t\tsgr_len += count;\n+\t}\n+\n+\treturn sgr_len;\n+}\n+\n /* auxiliary function for binary search in interval table */\n static int bisearch(ucs_char_t ucs, const struct interval *table, int max)\n {\n\n"},{"id":"558642","messageId":"xmqqa4n7fhv0.fsf@gitster.g","threadId":"66858","inReplyTo":"ghbj7nmjd7.fsf@gouders.net","subject":"Re: [PATCH 1/2] utf8: respect bounds in utf8_strnwidth()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-17T18:11:31Z","receivedAt":"2026-11-17T18:11:31Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Dirk Gouders <dirk@gouders.net> writes:\n\n>> An update to display_mode_esc_sequence_len() to take the length\n>> limit, including its fallout, is shown below, and it does not look\n>> too bad.  Not even compile-tested, but it should be sufficient as an\n>> illustration of the idea.\n>>\n>> This does not make your \"after display_mode_esc_sequence_len()\n>> skips, make sure if we still have remaining string to grab one UTF-8\n>> character\" change unnecessary, as I said.\n>\n> My v2 would have introduced two new functions (see attached) to replace\n> display_mode_esc_sequence_len():\n\nA two-function arrangement would be a good idea for maintainability.\n\nAdd display_mode_esc_sequence_len_counted() to use the counted\nstring API, with display_mode_esc_sequence_len() keeping the\noriginal calling convention (i.e., we call strlen(s) then call the\nnew helper).  I was too lazy to do the function pair in the\nillustration patch, which caused all existing callers to be\naffected, but given that there is another topic or two that are just\nbeing proposed that would add more callers to this function, it\nprobably makes sense to keep a function with the NUL-terminated\nstring API as before, and add the counted interface for callers that\ncare, like the one you are fixing.\n\nThanks.\n\n"},{"id":"558751","messageId":"20261118-master-v2-0-3f2aaec15d31@gouders.net","threadId":"66858","inReplyTo":"20261116-master-v1-0-dc2a50ce0aa0@gouders.net","subject":"[PATCH v2 0/4] utf8: bounds-related fixes and a doc fix","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-18T21:08:49Z","receivedAt":"2026-11-18T21:08:49Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"A patch here on the list made me look at\ndisplay_mode_esc_sequence_len() in utf8.c and I found two details in\nthe file that are perhaps worth a fix:\n\n1) utf8_strnwidth() disregards the given len argument when fed\n   with proper string and len.\n\n2) The comment for utf8_strnwidth() is incorrect: it states the\n   function returns strlen() for cases when it actually returns its\n   len argument.\n\nSigned-off-by: Dirk Gouders <dirk@gouders.net>\n---\nChanges in v2:\n- Add a patch by Junio that introduces a bounded function display_mode_esc_sequence_len_counted()\n  I added some tests for this new function.\n- 2/2 became 2/4\n- 3/4 is the modified 1/2 and uses the new\n  display_mode_esc_sequence_len_counted() so that the while loop stays\n  within the given bounds.\n  The commit message was adjusted.\n  One of the tests was wrong: \"\\033\" has width 0, so the sum must be 3 instead of 4.\n- 4/4 is an addition to fix a similar problem with utf8_width().\n  That function is used without its optional bounds argument and\n  can violate boundaries as well.  Test for such cases are added.\n- Link to v1: https://patch.msgid.link/20261116-master-v1-0-dc2a50ce0aa0@gouders.net\n\n---\nDirk Gouders (3):\n      utf8: fix comment for utf8_strnwidth()\n      utf8: respect string boundaries after skipping ANSI escape sequences\n      utf8: respect boundaries in utf8_strnwidth()\n\nJunio C Hamano (1):\n      utf8: add display_mode_esc_sequence_len_counted()\n\n Makefile                    |  1 +\n t/meson.build               |  1 +\n t/unit-tests/u-utf8-ansi.c  | 14 +++++++++++++\n t/unit-tests/u-utf8-width.c | 20 ++++++++++++++++++\n utf8.c                      | 50 +++++++++++++++++++++++++++++++++------------\n utf8.h                      |  1 +\n 6 files changed, 74 insertions(+), 13 deletions(-)\n\nRange-diff versus v1:\n\n-:  ---------- > 1:  3b2ab53858 utf8: add display_mode_esc_sequence_len_counted()\n2:  aab04775f6 = 2:  efeff5cfd7 utf8: fix comment for utf8_strnwidth()\n1:  21baf82731 ! 3:  152210dd15 utf8: respect bounds in utf8_strnwidth()\n    @@ Metadata\n     Author: Dirk Gouders <dirk@gouders.net>\n     \n      ## Commit message ##\n    -    utf8: respect bounds in utf8_strnwidth()\n    +    utf8: respect string boundaries after skipping ANSI escape sequences\n     \n    -    utf8_strnwidth() takes a len argument, but because\n    -    display_mode_esc_sequence_len() works without an explicit boundary,\n    -    there are cases when utf8_strnwidth() operates beyond the given len\n    -    boundary.\n    +    utf8_strnwidth() can skip ANSI escape sequences and it is possible\n    +    that such sequences end the string within the given boundary.\n     \n    -    Fix this by adding a boundary-check after looping with\n    -    display_mode_esc_sequence_len() to ensure we operate strictly within\n    -    the given len.\n    +    Check for such situations before proceeding and calling utf8_width().\n     \n    -    Further, add three corner case tests of which two will fail without\n    -    the fix:\n    -    ------------------------------------------------------------------------\n    -    not ok 257 - utf8_width::strnwidth_ansi\n    -        ---\n    -        reason: |\n    -          Expected comparison to hold: 0 == utf8_strnwidth(\"\\033[31mhello\", 4, 1)\n    -          0 != 1\n    -        at:\n    -          file: 't/unit-tests/u-utf8-width.c'\n    -          line: 11\n    -          function: 'test_utf8_width__strnwidth_ansi'\n    -        ...\n    -    ------------------------------------------------------------------------\n    -\n    -    With the fix:\n    -    ------------------------------------------------------------------------\n    -    ok 257 - utf8_width::strnwidth_ansi\n    -    ------------------------------------------------------------------------\n    +    Add tests that test such corner cases.\n     \n         Signed-off-by: Dirk Gouders <dirk@gouders.net>\n     \n    @@ t/unit-tests/u-utf8-width.c\n     + */\n     +void test_utf8_width__strnwidth_ansi(void)\n     +{\n    -+\tcl_assert_equal_i(0, utf8_strnwidth(\"\\033[31mhello\", 4, 1));\n    ++\tcl_assert_equal_i(3, utf8_strnwidth(\"\\033[31mhello\", 4, 1));\n     +\tcl_assert_equal_i(0, utf8_strnwidth(\"\\033[31mhello\", 5, 1));\n     +\tcl_assert_equal_i(1, utf8_strnwidth(\"\\033[31mhello\", 6, 1));\n     +}\n    @@ utf8.c: int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n      \t\tsize_t skip;\n      \n     -\t\twhile (skip_ansi &&\n    --\t\t       (skip = display_mode_esc_sequence_len(string)) != 0)\n    +-\t\t       (skip = display_mode_esc_sequence_len_counted(string,\n    +-\t\t\t\t\t\t\torig + len - string)) != 0)\n     -\t\t\tstring += skip;\n    -+\t\tif (skip_ansi) {\n    -+\t\t\twhile ((skip = display_mode_esc_sequence_len(string)) != 0)\n    ++\t\tif (skip_ansi ) {\n    ++\t\t\twhile ((skip = display_mode_esc_sequence_len_counted(string,\n    ++\t\t\t\t\t\t\t\t\t     orig + len - string)) != 0)\n     +\t\t\t\tstring += skip;\n     +\n     +\t\t\tif (string >= orig + len)\n-:  ---------- > 4:  044d188fa8 utf8: respect boundaries in utf8_strnwidth()\n\n---\nbase-commit: 7fb6055e678f800320d8ab8cd8f201d9493d1ba0\nchange-id: 20261116-master-69b171612823\n\n\n"},{"id":"558752","messageId":"20261118-master-v2-1-3f2aaec15d31@gouders.net","threadId":"66858","inReplyTo":"20261118-master-v2-0-3f2aaec15d31@gouders.net","subject":"[PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-18T21:08:50Z","receivedAt":"2026-11-18T21:08:50Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\ndisplay_mode_esc_sequence_len() scans for an ANSI display mode\nescape sequence (e.g. \"ESC [ 1 ; 31 m\") at the start of a string,\nrelying on NUL termination to stop.  Some callers, however, work on\na counted buffer: utf8_strnwidth() takes <string, len> and must not\nscan past it, while strbuf_utf8_replace() and\nstrbuf_add_wrapped_text() know (or can cheaply learn) the end.\n\nAdd display_mode_esc_sequence_len_counted() taking <ptr, len>,\nwhich never reads at or beyond ptr[len], and make\ndisplay_mode_esc_sequence_len() a thin wrapper for NUL-terminated\nstrings.\n\nUse the counted variant in utf8_strnwidth() for correctness, and in\nstrbuf_utf8_replace() and strbuf_add_wrapped_text() (computing the\nend once upfront in the latter) to avoid measuring the remaining\ntext in each iteration.\n\nAlso, add some tests for the new function.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Dirk Gouders <dirk@gouders.net>\n---\n Makefile                   |  1 +\n t/meson.build              |  1 +\n t/unit-tests/u-utf8-ansi.c | 14 ++++++++++++++\n utf8.c                     | 35 ++++++++++++++++++++++++++---------\n utf8.h                     |  1 +\n 5 files changed, 43 insertions(+), 9 deletions(-)\n\ndiff --git a/Makefile b/Makefile\nindex 4c84eea6e2..3c8dc9ca40 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -1564,6 +1564,7 @@ CLAR_TEST_SUITES += u-strvec\n CLAR_TEST_SUITES += u-trailer\n CLAR_TEST_SUITES += u-urlmatch-normalization\n CLAR_TEST_SUITES += u-utf8-width\n+CLAR_TEST_SUITES += u-utf8-ansi\n CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)\n CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))\n CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o\ndiff --git a/t/meson.build b/t/meson.build\nindex 1cf1d95f70..e224df58c8 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -27,6 +27,7 @@ clar_test_suites = [\n   'unit-tests/u-trailer.c',\n   'unit-tests/u-urlmatch-normalization.c',\n   'unit-tests/u-utf8-width.c',\n+  'unit-tests/u-utf8-ansi.c',\n ]\n \n clar_sources = [\ndiff --git a/t/unit-tests/u-utf8-ansi.c b/t/unit-tests/u-utf8-ansi.c\nnew file mode 100644\nindex 0000000000..c19d8bce78\n--- /dev/null\n+++ b/t/unit-tests/u-utf8-ansi.c\n@@ -0,0 +1,14 @@\n+#include \"unit-test.h\"\n+#include \"utf8.h\"\n+#include \"strbuf.h\"\n+\n+/*\n+ * Test len limited functions for ANSI SGR lengths.\n+ */\n+void test_utf8_ansi__sgr_len(void)\n+{\n+\tcl_assert_equal_i(0, display_mode_esc_sequence_len_counted(\"\\033[m\", 2));\n+\tcl_assert_equal_i(3, display_mode_esc_sequence_len_counted(\"\\033[m\", 3));\n+\tcl_assert_equal_i(5, display_mode_esc_sequence_len_counted(\"\\033[31m\", 5));\n+\tcl_assert_equal_i(7, display_mode_esc_sequence_len_counted(\"\\033[0;31m\", 7));\n+}\ndiff --git a/utf8.c b/utf8.c\nindex 96460cc414..d0546ab31f 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -16,20 +16,33 @@ struct interval {\n \tucs_char_t last;\n };\n \n-size_t display_mode_esc_sequence_len(const char *s)\n+/*\n+ * Return the length of the ANSI display mode escape sequence (e.g.,\n+ * \"\\033[1;31m\") that appears at the beginning of the \"len\" bytes\n+ * of string \"s\", or 0 if the bytes do not begin with a complete\n+ * such sequence.  Never looks at s[len] or beyond.\n+ */\n+size_t display_mode_esc_sequence_len_counted(const char *s, size_t len)\n {\n-\tconst char *p = s;\n-\tif (*p++ != '\\033')\n+\tconst char *p = s, *end = s + len;\n+\n+\tif (end <= p || *p++ != '\\033')\n \t\treturn 0;\n-\tif (*p++ != '[')\n+\tif (end <= p || *p++ != '[')\n \t\treturn 0;\n-\twhile (isdigit(*p) || *p == ';')\n+\twhile (p < end && (isdigit(*p) || *p == ';'))\n \t\tp++;\n-\tif (*p++ != 'm')\n+\tif (end <= p || *p++ != 'm')\n \t\treturn 0;\n \treturn p - s;\n }\n \n+/* Same as above, but for a NUL-terminated string \"s\". */\n+size_t display_mode_esc_sequence_len(const char *s)\n+{\n+\treturn display_mode_esc_sequence_len_counted(s, strlen(s));\n+}\n+\n /* auxiliary function for binary search in interval table */\n static int bisearch(ucs_char_t ucs, const struct interval *table, int max)\n {\n@@ -218,7 +231,8 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n \t\tsize_t skip;\n \n \t\twhile (skip_ansi &&\n-\t\t       (skip = display_mode_esc_sequence_len(string)) != 0)\n+\t\t       (skip = display_mode_esc_sequence_len_counted(string,\n+\t\t\t\t\t\t\torig + len - string)) != 0)\n \t\t\tstring += skip;\n \n \t\tglyph_width = utf8_width(&string, NULL);\n@@ -279,6 +293,7 @@ void strbuf_add_wrapped_text(struct strbuf *buf,\n {\n \tint indent, w, assume_utf8 = 1;\n \tconst char *bol, *space, *start = text;\n+\tconst char *end = text + strlen(text);\n \tsize_t orig_len = buf->len;\n \n \tif (width <= 0) {\n@@ -299,7 +314,8 @@ void strbuf_add_wrapped_text(struct strbuf *buf,\n \t\tchar c;\n \t\tsize_t skip;\n \n-\t\twhile ((skip = display_mode_esc_sequence_len(text)))\n+\t\twhile ((skip = display_mode_esc_sequence_len_counted(text,\n+\t\t\t\t\t\t\t\tend - text)))\n \t\t\ttext += skip;\n \n \t\tc = *text;\n@@ -378,7 +394,8 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,\n \t\tint glyph_width;\n \t\tsize_t n;\n \n-\t\twhile ((n = display_mode_esc_sequence_len(src))) {\n+\t\twhile ((n = display_mode_esc_sequence_len_counted(src,\n+\t\t\t\t\t\t\t\t  end - src))) {\n \t\t\tstrbuf_add(&dst, src, n);\n \t\t\tsrc += n;\n \t\t}\ndiff --git a/utf8.h b/utf8.h\nindex cf8ecb0f21..57ca2ab270 100644\n--- a/utf8.h\n+++ b/utf8.h\n@@ -6,6 +6,7 @@ struct strbuf;\n typedef unsigned int ucs_char_t;  /* assuming 32bit int */\n \n size_t display_mode_esc_sequence_len(const char *s);\n+size_t display_mode_esc_sequence_len_counted(const char *s, size_t len);\n int utf8_width(const char **start, size_t *remainder_p);\n int utf8_strnwidth(const char *string, size_t len, int skip_ansi);\n int utf8_strwidth(const char *string);\n\n-- \n2.55.0\n\n\n"},{"id":"558753","messageId":"20261118-master-v2-2-3f2aaec15d31@gouders.net","threadId":"66858","inReplyTo":"20261118-master-v2-0-3f2aaec15d31@gouders.net","subject":"[PATCH v2 2/4] utf8: fix comment for utf8_strnwidth()","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-18T21:08:51Z","receivedAt":"2026-11-18T21:08:51Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"The comment for utf8_strnwidth() states it returns strlen() for cases\nwhen the given string doesn't look like valid utf8, but it returns its\nargument len in such cases.\n\nFix the comment accordingly.\n\nSigned-off-by: Dirk Gouders <dirk@gouders.net>\n---\n utf8.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/utf8.c b/utf8.c\nindex d0546ab31f..b43f84a03d 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -218,7 +218,7 @@ int utf8_width(const char **start, size_t *remainder_p)\n \n /*\n  * Returns the total number of columns required by a null-terminated\n- * string, assuming that the string is utf8.  Returns strlen() instead\n+ * string, assuming that the string is utf8.  Returns len instead\n  * if the string does not look like a valid utf8 string.\n  */\n int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n\n-- \n2.55.0\n\n\n"},{"id":"558754","messageId":"20261118-master-v2-3-3f2aaec15d31@gouders.net","threadId":"66858","inReplyTo":"20261118-master-v2-0-3f2aaec15d31@gouders.net","subject":"[PATCH v2 3/4] utf8: respect string boundaries after skipping ANSI escape sequences","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-18T21:08:52Z","receivedAt":"2026-11-18T21:08:52Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"utf8_strnwidth() can skip ANSI escape sequences and it is possible\nthat such sequences end the string within the given boundary.\n\nCheck for such situations before proceeding and calling utf8_width().\n\nAdd tests that test such corner cases.\n\nSigned-off-by: Dirk Gouders <dirk@gouders.net>\n---\n t/unit-tests/u-utf8-width.c | 11 +++++++++++\n utf8.c                      | 12 ++++++++----\n 2 files changed, 19 insertions(+), 4 deletions(-)\n\ndiff --git a/t/unit-tests/u-utf8-width.c b/t/unit-tests/u-utf8-width.c\nindex 86e09c3574..5e2b636db9 100644\n--- a/t/unit-tests/u-utf8-width.c\n+++ b/t/unit-tests/u-utf8-width.c\n@@ -2,6 +2,17 @@\n #include \"utf8.h\"\n #include \"strbuf.h\"\n \n+/*\n+ * Test utf8_strnwidth for strings with SGR sequences and corner case length\n+ * arguments.\n+ */\n+void test_utf8_width__strnwidth_ansi(void)\n+{\n+\tcl_assert_equal_i(3, utf8_strnwidth(\"\\033[31mhello\", 4, 1));\n+\tcl_assert_equal_i(0, utf8_strnwidth(\"\\033[31mhello\", 5, 1));\n+\tcl_assert_equal_i(1, utf8_strnwidth(\"\\033[31mhello\", 6, 1));\n+}\n+\n /*\n  * Test utf8_strnwidth with various Chinese strings\n  * Chinese characters typically have a width of 2 columns when displayed\ndiff --git a/utf8.c b/utf8.c\nindex b43f84a03d..1b88324ea3 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -230,10 +230,14 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n \t\tint glyph_width;\n \t\tsize_t skip;\n \n-\t\twhile (skip_ansi &&\n-\t\t       (skip = display_mode_esc_sequence_len_counted(string,\n-\t\t\t\t\t\t\torig + len - string)) != 0)\n-\t\t\tstring += skip;\n+\t\tif (skip_ansi ) {\n+\t\t\twhile ((skip = display_mode_esc_sequence_len_counted(string,\n+\t\t\t\t\t\t\t\t\t     orig + len - string)) != 0)\n+\t\t\t\tstring += skip;\n+\n+\t\t\tif (string >= orig + len)\n+\t\t\t\tbreak;\n+\t\t}\n \n \t\tglyph_width = utf8_width(&string, NULL);\n \t\tif (glyph_width > 0)\n\n-- \n2.55.0\n\n\n"},{"id":"558755","messageId":"20261118-master-v2-4-3f2aaec15d31@gouders.net","threadId":"66858","inReplyTo":"20261118-master-v2-0-3f2aaec15d31@gouders.net","subject":"[PATCH v2 4/4] utf8: respect boundaries in utf8_strnwidth()","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-18T21:08:53Z","receivedAt":"2026-11-18T21:08:53Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"utf8_strnwidth() can be called with boundaries that effectively cause\nincomplete multibyte sequences and we should return \"len\" in such\ncases.\n\nBut, utf8_width is used without its optional boundary argument and\nwill thus work beyond the given boundary.\n\nFix this by using the boundary argument for utf8_width.\nAdd some tests that cover such situations.\n\nSigned-off-by: Dirk Gouders <dirk@gouders.net>\n---\n t/unit-tests/u-utf8-width.c | 9 +++++++++\n utf8.c                      | 5 ++++-\n 2 files changed, 13 insertions(+), 1 deletion(-)\n\ndiff --git a/t/unit-tests/u-utf8-width.c b/t/unit-tests/u-utf8-width.c\nindex 5e2b636db9..c4df4dd6a6 100644\n--- a/t/unit-tests/u-utf8-width.c\n+++ b/t/unit-tests/u-utf8-width.c\n@@ -2,6 +2,15 @@\n #include \"utf8.h\"\n #include \"strbuf.h\"\n \n+/*\n+ * Test utf8_strnwidth for cjk characters invalidated by len argument.\n+ */\n+void test_utf8_width__strnwidth_cjk_chopped(void)\n+{\n+\tcl_assert_equal_i(1, utf8_strnwidth(\"三bytes long, 二columns wide\", 1, 1));\n+\tcl_assert_equal_i(2, utf8_strnwidth(\"三bytes long, 二columns wide\", 3, 1));\n+}\n+\n /*\n  * Test utf8_strnwidth for strings with SGR sequences and corner case length\n  * arguments.\ndiff --git a/utf8.c b/utf8.c\nindex 1b88324ea3..8d4b159e1d 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -229,6 +229,7 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n \twhile (string && string < orig + len) {\n \t\tint glyph_width;\n \t\tsize_t skip;\n+\t\tsize_t remainder;\n \n \t\tif (skip_ansi ) {\n \t\t\twhile ((skip = display_mode_esc_sequence_len_counted(string,\n@@ -239,7 +240,9 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n \t\t\t\tbreak;\n \t\t}\n \n-\t\tglyph_width = utf8_width(&string, NULL);\n+\t\tremainder = orig + len - string;\n+\n+\t\tglyph_width = utf8_width(&string, &remainder);\n \t\tif (glyph_width > 0)\n \t\t\twidth += glyph_width;\n \t}\n\n-- \n2.55.0\n\n\n"},{"id":"558756","messageId":"xmqqse0x3j57.fsf@gitster.g","threadId":"66858","inReplyTo":"20261118-master-v2-3-3f2aaec15d31@gouders.net","subject":"Re: [PATCH v2 3/4] utf8: respect string boundaries after skipping ANSI escape sequences","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-18T21:49:08Z","receivedAt":"2026-11-18T21:49:08Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Dirk Gouders <dirk@gouders.net> writes:\n\n> -\t\twhile (skip_ansi &&\n> -\t\t       (skip = display_mode_esc_sequence_len_counted(string,\n> -\t\t\t\t\t\t\torig + len - string)) != 0)\n> -\t\t\tstring += skip;\n> +\t\tif (skip_ansi ) {\n\nDrop excess SP after the variable.\n\n> +\t\t\twhile ((skip = display_mode_esc_sequence_len_counted(string,\n> +\t\t\t\t\t\t\t\t\t     orig + len - string)) != 0)\n> +\t\t\t\tstring += skip;\n\nOverlong line.  You can (and are encouraged to) omit comparison with\nzero \"!= 0\" to lose 5 columns but it still is overly long.\n\n\t\tif (skip_ansi) {\n\t\t\twhile ((skip = display_mode_esc_sequence_len_counted(\n\t\t\t\t\t\tstring,\torig + len - string)))\n\t\t\t\tstring += skip;\n\nor something, perhaps.\n\n> +\t\t\tif (string >= orig + len)\n> +\t\t\t\tbreak;\n> +\t\t}\n>  \n>  \t\tglyph_width = utf8_width(&string, NULL);\n>  \t\tif (glyph_width > 0)\n\n"},{"id":"558757","messageId":"xmqqo6bl3j32.fsf@gitster.g","threadId":"66858","inReplyTo":"20261118-master-v2-4-3f2aaec15d31@gouders.net","subject":"Re: [PATCH v2 4/4] utf8: respect boundaries in utf8_strnwidth()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-18T21:50:25Z","receivedAt":"2026-11-18T21:50:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Dirk Gouders <dirk@gouders.net> writes:\n\n> utf8_strnwidth() can be called with boundaries that effectively cause\n> incomplete multibyte sequences and we should return \"len\" in such\n> cases.\n>\n> But, utf8_width is used without its optional boundary argument and\n> will thus work beyond the given boundary.\n>\n> Fix this by using the boundary argument for utf8_width.\n> Add some tests that cover such situations.\n>\n> Signed-off-by: Dirk Gouders <dirk@gouders.net>\n> ---\n>  t/unit-tests/u-utf8-width.c | 9 +++++++++\n>  utf8.c                      | 5 ++++-\n>  2 files changed, 13 insertions(+), 1 deletion(-)\n\nWell spotted, and nicely done.\n\n>\n> diff --git a/t/unit-tests/u-utf8-width.c b/t/unit-tests/u-utf8-width.c\n> index 5e2b636db9..c4df4dd6a6 100644\n> --- a/t/unit-tests/u-utf8-width.c\n> +++ b/t/unit-tests/u-utf8-width.c\n> @@ -2,6 +2,15 @@\n>  #include \"utf8.h\"\n>  #include \"strbuf.h\"\n>  \n> +/*\n> + * Test utf8_strnwidth for cjk characters invalidated by len argument.\n> + */\n> +void test_utf8_width__strnwidth_cjk_chopped(void)\n> +{\n> +\tcl_assert_equal_i(1, utf8_strnwidth(\"三bytes long, 二columns wide\", 1, 1));\n> +\tcl_assert_equal_i(2, utf8_strnwidth(\"三bytes long, 二columns wide\", 3, 1));\n> +}\n> +\n>  /*\n>   * Test utf8_strnwidth for strings with SGR sequences and corner case length\n>   * arguments.\n> diff --git a/utf8.c b/utf8.c\n> index 1b88324ea3..8d4b159e1d 100644\n> --- a/utf8.c\n> +++ b/utf8.c\n> @@ -229,6 +229,7 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n>  \twhile (string && string < orig + len) {\n>  \t\tint glyph_width;\n>  \t\tsize_t skip;\n> +\t\tsize_t remainder;\n>  \n>  \t\tif (skip_ansi ) {\n>  \t\t\twhile ((skip = display_mode_esc_sequence_len_counted(string,\n> @@ -239,7 +240,9 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n>  \t\t\t\tbreak;\n>  \t\t}\n>  \n> -\t\tglyph_width = utf8_width(&string, NULL);\n> +\t\tremainder = orig + len - string;\n> +\n> +\t\tglyph_width = utf8_width(&string, &remainder);\n>  \t\tif (glyph_width > 0)\n>  \t\t\twidth += glyph_width;\n>  \t}\n\n"},{"id":"558765","messageId":"CAHWeT-Zzvjx5knLdCdxUmn8zmkAk2qrzzvXkbSjhCibWy7hAeQ@mail.gmail.com","threadId":"66858","inReplyTo":"20261118-master-v2-2-3f2aaec15d31@gouders.net","subject":"Re: [PATCH v2 2/4] utf8: fix comment for utf8_strnwidth()","fromName":"Andy Koppe","fromEmail":"andy.koppe@gmail.com","sentAt":"2026-11-18T22:40:56Z","receivedAt":"2026-11-18T22:40:56Z","isPatch":true,"sender":{"key":"andy.koppe@gmail.com","avatar":"https://avatars.githubusercontent.com/u/223411?v=4"},"body":"Dirk Gouders wrote:\n>\n> The comment for utf8_strnwidth() states it returns strlen() for cases\n> when the given string doesn't look like valid utf8, but it returns its\n> argument len in such cases.\n>\n> Fix the comment accordingly.\n…\n>  /*\n>   * Returns the total number of columns required by a null-terminated\n> - * string, assuming that the string is utf8.  Returns strlen() instead\n> + * string, assuming that the string is utf8.  Returns len instead\n>   * if the string does not look like a valid utf8 string.\n>   */\n>  int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n\n\"null-terminated\" is wrong too.\n\n"},{"id":"558785","messageId":"CAHWeT-aWx1VwUjM+hZ--jjSAbcMenz5Mw11y9ouFavGndUMrSA@mail.gmail.com","threadId":"66858","inReplyTo":"20261118-master-v2-1-3f2aaec15d31@gouders.net","subject":"Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()","fromName":"Andy Koppe","fromEmail":"andy.koppe@gmail.com","sentAt":"2026-11-19T05:54:50Z","receivedAt":"2026-11-19T05:54:50Z","isPatch":true,"sender":{"key":"andy.koppe@gmail.com","avatar":"https://avatars.githubusercontent.com/u/223411?v=4"},"body":"Dirk Gouders wrote:\n> +/* Same as above, but for a NUL-terminated string \"s\". */\n> +size_t display_mode_esc_sequence_len(const char *s)\n> +{\n> +       return display_mode_esc_sequence_len_counted(s, strlen(s));\n> +}\n\nThis is a performance regression compared to the original function.\nCalls that previously returned immediately because *s != '\\033', or\nafter a few bytes of scanning, now scan the entire string with strlen.\nIt turns scanning a whole string for SGRs with repeated calls to the\nfunction from linear to quadratic.\n\nTherefore I think that if the original implementation isn't going to be\nkept, it would be better not to provide the wrapper, and thereby make\ncallers think about what they want to pass as the length.\n\nUses on strbufs can of course compute it with `sb.buf + sb.len - s`,\nbut they don't actually need this safety net, because strbufs are\nguaranteed to be NUL-terminated and the function stops at NULs.\nAnd the length calculation itself adds a source of errors.\n\n"},{"id":"558787","messageId":"gh1p8hbbvv.fsf@gouders.net","threadId":"66858","inReplyTo":"CAHWeT-Zzvjx5knLdCdxUmn8zmkAk2qrzzvXkbSjhCibWy7hAeQ@mail.gmail.com","subject":"Re: [PATCH v2 2/4] utf8: fix comment for utf8_strnwidth()","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-19T05:58:44Z","receivedAt":"2026-11-19T05:58:44Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"On Wed, Nov 18 2026, Andy Koppe wrote:\n\n> Dirk Gouders wrote:\n>>\n>> The comment for utf8_strnwidth() states it returns strlen() for cases\n>> when the given string doesn't look like valid utf8, but it returns its\n>> argument len in such cases.\n>>\n>> Fix the comment accordingly.\n> …\n>>  /*\n>>   * Returns the total number of columns required by a null-terminated\n>> - * string, assuming that the string is utf8.  Returns strlen() instead\n>> + * string, assuming that the string is utf8.  Returns len instead\n>>   * if the string does not look like a valid utf8 string.\n>>   */\n>>  int utf8_strnwidth(const char *string, size_t len, int skip_ansi)\n>\n> \"null-terminated\" is wrong too.\n\nYes, I will fix that, too.  Thank you.\n\nDirk\n\n"},{"id":"558807","messageId":"ghse0x9sgm.fsf@gouders.net","threadId":"66858","inReplyTo":"CAHWeT-aWx1VwUjM+hZ--jjSAbcMenz5Mw11y9ouFavGndUMrSA@mail.gmail.com","subject":"Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-19T07:43:37Z","receivedAt":"2026-11-19T07:43:37Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"On Thu, Nov 19 2026, Andy Koppe wrote:\n\n> Dirk Gouders wrote:\n>> +/* Same as above, but for a NUL-terminated string \"s\". */\n>> +size_t display_mode_esc_sequence_len(const char *s)\n>> +{\n>> +       return display_mode_esc_sequence_len_counted(s, strlen(s));\n>> +}\n>\n> This is a performance regression compared to the original function.\n> Calls that previously returned immediately because *s != '\\033', or\n> after a few bytes of scanning, now scan the entire string with strlen.\n> It turns scanning a whole string for SGRs with repeated calls to the\n> function from linear to quadratic.\n>\n> Therefore I think that if the original implementation isn't going to be\n> kept, it would be better not to provide the wrapper, and thereby make\n> callers think about what they want to pass as the length.\n>\n> Uses on strbufs can of course compute it with `sb.buf + sb.len - s`,\n> but they don't actually need this safety net, because strbufs are\n> guaranteed to be NUL-terminated and the function stops at NULs.\n> And the length calculation itself adds a source of errors.\n\nThere is only one user in pretty.c that uses the wrapper.\nI can change it to use the _counted version and we could then drop the\nwrapper.  Junio's v1 already included such a change and I could steal it\nfrom there.\n\nWould that be OK?\n\nDirk\n\n"},{"id":"558885","messageId":"xmqqik1swq20.fsf@gitster.g","threadId":"66858","inReplyTo":"20261118-master-v2-1-3f2aaec15d31@gouders.net","subject":"Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-19T19:59:19Z","receivedAt":"2026-11-19T19:59:19Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Dirk Gouders <dirk@gouders.net> writes:\n\n> diff --git a/t/meson.build b/t/meson.build\n> index 1cf1d95f70..e224df58c8 100644\n> --- a/t/meson.build\n> +++ b/t/meson.build\n> @@ -27,6 +27,7 @@ clar_test_suites = [\n>    'unit-tests/u-trailer.c',\n>    'unit-tests/u-urlmatch-normalization.c',\n>    'unit-tests/u-utf8-width.c',\n> +  'unit-tests/u-utf8-ansi.c',\n>  ]\n\nShould this list be reordered?  \"make -C t check-meson\" complains.\n\n\n"},{"id":"558887","messageId":"ghv75s1s84.fsf@gouders.net","threadId":"66858","inReplyTo":"xmqqik1swq20.fsf@gitster.g","subject":"Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-19T20:28:11Z","receivedAt":"2026-11-19T20:28:11Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"On Thu, Nov 19 2026, Junio C Hamano wrote:\n\n> Dirk Gouders <dirk@gouders.net> writes:\n>\n>> diff --git a/t/meson.build b/t/meson.build\n>> index 1cf1d95f70..e224df58c8 100644\n>> --- a/t/meson.build\n>> +++ b/t/meson.build\n>> @@ -27,6 +27,7 @@ clar_test_suites = [\n>>    'unit-tests/u-trailer.c',\n>>    'unit-tests/u-urlmatch-normalization.c',\n>>    'unit-tests/u-utf8-width.c',\n>> +  'unit-tests/u-utf8-ansi.c',\n>>  ]\n>\n> Should this list be reordered?  \"make -C t check-meson\" complains.\n\nAh yes, thank you. The list in meson.txt must be sorted (check-meson\ndiff's the output of ls(1) and the list in meson.build).\n\nI will fix the sorting order with v3.\n\nDirk\n\nPS: The purpose of 154ce05cce (Makefile: detect missing Meson tests,\n2024-12-13) wasn't to enforce a sorted order and maybe it would help to\nput a comment in meson.build.\n\n"},{"id":"558890","messageId":"CAHWeT-ZNoDWqTMfCdQg3PZ2bbLD9PTVqeFMms_1dFj8dbReSCg@mail.gmail.com","threadId":"66858","inReplyTo":"ghse0x9sgm.fsf@gouders.net","subject":"Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()","fromName":"Andy Koppe","fromEmail":"andy.koppe@gmail.com","sentAt":"2026-11-19T20:58:45Z","receivedAt":"2026-11-19T20:58:45Z","isPatch":true,"sender":{"key":"andy.koppe@gmail.com","avatar":"https://avatars.githubusercontent.com/u/223411?v=4"},"body":"Dirk Gouders wrote:\n>\n> On Thu, Nov 19 2026, Andy Koppe wrote:\n>\n> > Dirk Gouders wrote:\n> >> +/* Same as above, but for a NUL-terminated string \"s\". */\n> >> +size_t display_mode_esc_sequence_len(const char *s)\n> >> +{\n> >> +       return display_mode_esc_sequence_len_counted(s, strlen(s));\n> >> +}\n> >\n> > This is a performance regression compared to the original function.\n> > Calls that previously returned immediately because *s != '\\033', or\n> > after a few bytes of scanning, now scan the entire string with strlen.\n> > It turns scanning a whole string for SGRs with repeated calls to the\n> > function from linear to quadratic.\n> >\n> > Therefore I think that if the original implementation isn't going to be\n> > kept, it would be better not to provide the wrapper, and thereby make\n> > callers think about what they want to pass as the length.\n> >\n> > Uses on strbufs can of course compute it with `sb.buf + sb.len - s`,\n> > but they don't actually need this safety net, because strbufs are\n> > guaranteed to be NUL-terminated and the function stops at NULs.\n> > And the length calculation itself adds a source of errors.\n>\n> There is only one user in pretty.c that uses the wrapper.\n> I can change it to use the _counted version and we could then drop the\n> wrapper.  Junio's v1 already included such a change and I could steal it\n> from there.\n>\n> Would that be OK?\n\nNo objection from me. I would change the make_line_colors_self_contained()\nfunction I'm proposing in [1] to the _counted variant anyway if the original\ndisplay_mode_esc_sequence_len() is going away, to avoid the performance issue\nwith the wrapper.\n\n[1] https://lore.kernel.org/git/e9f88b533dfbd10528c9151e2e87207419d0253d.1794905511.git.andy.koppe@gmail.com\n\n"},{"id":"558891","messageId":"CAHWeT-YX8FOacV7YypyZ-qmr3Q2T0TEF=_Qy2bVE7jcS0OWvNA@mail.gmail.com","threadId":"66858","inReplyTo":"CAHWeT-ZNoDWqTMfCdQg3PZ2bbLD9PTVqeFMms_1dFj8dbReSCg@mail.gmail.com","subject":"Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()","fromName":"Andy Koppe","fromEmail":"andy.koppe@gmail.com","sentAt":"2026-11-19T21:04:09Z","receivedAt":"2026-11-19T21:04:09Z","isPatch":true,"sender":{"key":"andy.koppe@gmail.com","avatar":"https://avatars.githubusercontent.com/u/223411?v=4"},"body":"Andy Koppe wrote:\n> Dirk Gouders wrote:\n> > There is only one user in pretty.c that uses the wrapper.\n> > I can change it to use the _counted version and we could then drop the\n> > wrapper.  Junio's v1 already included such a change and I could steal it\n> > from there.\n> >\n> > Would that be OK?\n>\n> No objection from me. I would change the make_line_colors_self_contained()\n> function I'm proposing in [1] to the _counted variant anyway if the original\n> display_mode_esc_sequence_len() is going away, to avoid the performance issue\n> with the wrapper.\n>\n> [1] https://lore.kernel.org/git/e9f88b533dfbd10528c9151e2e87207419d0253d.1794905511.git.andy.koppe@gmail.com\n\nps: I guess you might as well just drop the _counted suffix then, and\nbasically just add the len parameter to display_mode_esc_sequence_len().\n\n"},{"id":"558892","messageId":"ghld6o1p6x.fsf@gouders.net","threadId":"66858","inReplyTo":"CAHWeT-YX8FOacV7YypyZ-qmr3Q2T0TEF=_Qy2bVE7jcS0OWvNA@mail.gmail.com","subject":"Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-19T21:33:42Z","receivedAt":"2026-11-19T21:33:42Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"On Thu, Nov 19 2026, Andy Koppe wrote:\n\n> Andy Koppe wrote:\n>> Dirk Gouders wrote:\n>> > There is only one user in pretty.c that uses the wrapper.\n>> > I can change it to use the _counted version and we could then drop the\n>> > wrapper.  Junio's v1 already included such a change and I could steal it\n>> > from there.\n>> >\n>> > Would that be OK?\n>>\n>> No objection from me. I would change the make_line_colors_self_contained()\n>> function I'm proposing in [1] to the _counted variant anyway if the original\n>> display_mode_esc_sequence_len() is going away, to avoid the performance issue\n>> with the wrapper.\n>>\n>> [1] https://lore.kernel.org/git/e9f88b533dfbd10528c9151e2e87207419d0253d.1794905511.git.andy.koppe@gmail.com\n>\n> ps: I guess you might as well just drop the _counted suffix then, and\n> basically just add the len parameter to display_mode_esc_sequence_len().\n\nThat was the plan.  I just checked v1 and all this would actually mean to use\nJunio's v1 instead of v2.\n\nDirk\n\n"},{"id":"558898","messageId":"ghbj7k1mg5.fsf@gouders.net","threadId":"66858","inReplyTo":"CAHWeT-aWx1VwUjM+hZ--jjSAbcMenz5Mw11y9ouFavGndUMrSA@mail.gmail.com","subject":"Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-19T22:32:58Z","receivedAt":"2026-11-19T22:32:58Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"On Thu, Nov 19 2026, Andy Koppe wrote:\n\n> Dirk Gouders wrote:\n>> +/* Same as above, but for a NUL-terminated string \"s\". */\n>> +size_t display_mode_esc_sequence_len(const char *s)\n>> +{\n>> +       return display_mode_esc_sequence_len_counted(s, strlen(s));\n>> +}\n>\n> This is a performance regression compared to the original function.\n> Calls that previously returned immediately because *s != '\\033', or\n> after a few bytes of scanning, now scan the entire string with strlen.\n> It turns scanning a whole string for SGRs with repeated calls to the\n> function from linear to quadratic.\n\nHow about creating a v3 for display_mode_esc_sequence_len() similar to\nutf8_width() to accept an optional remainder argument?\n\nIf NULL is given, display_mode_esc_sequence_len() assumes a\nnull-terminated string, otherwise the length is given in *remainder.\nThe function can then update the remainder and return the length.\n\nJust an idea.\n\nDirk\n\n> Therefore I think that if the original implementation isn't going to be\n> kept, it would be better not to provide the wrapper, and thereby make\n> callers think about what they want to pass as the length.\n>\n> Uses on strbufs can of course compute it with `sb.buf + sb.len - s`,\n> but they don't actually need this safety net, because strbufs are\n> guaranteed to be NUL-terminated and the function stops at NULs.\n> And the length calculation itself adds a source of errors.\n\n"},{"id":"558900","messageId":"gh7bi81l8f.fsf@gouders.net","threadId":"66858","inReplyTo":"ghbj7k1mg5.fsf@gouders.net","subject":"Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()","fromName":"Dirk Gouders","fromEmail":"dirk@gouders.net","sentAt":"2026-11-19T22:59:12Z","receivedAt":"2026-11-19T22:59:12Z","isPatch":true,"sender":{"key":"dirk@gouders.net","avatar":"https://avatars.githubusercontent.com/u/81326422?v=4"},"body":"On Thu, Nov 19 2026, Dirk Gouders wrote:\n\n> On Thu, Nov 19 2026, Andy Koppe wrote:\n>\n>> Dirk Gouders wrote:\n>>> +/* Same as above, but for a NUL-terminated string \"s\". */\n>>> +size_t display_mode_esc_sequence_len(const char *s)\n>>> +{\n>>> +       return display_mode_esc_sequence_len_counted(s, strlen(s));\n>>> +}\n>>\n>> This is a performance regression compared to the original function.\n>> Calls that previously returned immediately because *s != '\\033', or\n>> after a few bytes of scanning, now scan the entire string with strlen.\n>> It turns scanning a whole string for SGRs with repeated calls to the\n>> function from linear to quadratic.\n>\n> How about creating a v3 for display_mode_esc_sequence_len() similar to\n> utf8_width() to accept an optional remainder argument?\n>\n> If NULL is given, display_mode_esc_sequence_len() assumes a\n> null-terminated string, otherwise the length is given in *remainder.\n> The function can then update the remainder and return the length.\n>\n> Just an idea.\n\nSomething like this untested example (please ignore the _counted):\n\ndiff --git a/utf8.c b/utf8.c\nindex 8d4b159e1d..fe6f288179 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -22,18 +22,24 @@ struct interval {\n  * of string \"s\", or 0 if the bytes do not begin with a complete\n  * such sequence.  Never looks at s[len] or beyond.\n  */\n-size_t display_mode_esc_sequence_len_counted(const char *s, size_t len)\n+size_t display_mode_esc_sequence_len_counted(const char *s, size_t *len)\n {\n-\tconst char *p = s, *end = s + len;\n+\tconst char *p = s, *end = NULL;\n \n-\tif (end <= p || *p++ != '\\033')\n+\tif (len)\n+\t\tend = s + *len;\n+\n+\tif ((end && end <= p) || *p++ != '\\033')\n \t\treturn 0;\n-\tif (end <= p || *p++ != '[')\n+\tif ((end && end <= p) || *p++ != '[')\n \t\treturn 0;\n-\twhile (p < end && (isdigit(*p) || *p == ';'))\n+\twhile (((end && p < end) || (!end && *p)) && (isdigit(*p) || *p == ';'))\n \t\tp++;\n-\tif (end <= p || *p++ != 'm')\n+\tif ((end && end <= p) || (!end && !*p) || *p++ != 'm')\n \t\treturn 0;\n+\n+\tif (len)\n+\t\t*len -= p - s;\n \treturn p - s;\n }\n \n\n"}]}