# utf8_strnwidth() bounds fixes move to counted escape-sequence helper

_The series ensures the width function never reads past its length argument, without risking quadratic behavior on NUL-terminated strings._

Section: Patches. Edition of 2026-11-23. Written by an AI editor from the thread "[PATCH 0/2] utf8: two minor fixes" (61 messages): https://gitlist.dev/t/66858

Dirk Gouders began with two fixes in utf8.c. utf8_strnwidth() ignored its len argument when given a proper string, and its comment wrongly said it returns strlen() for invalid input when it actually returns len. Andy Koppe helped with the comment fix.

Early versions added bounds checks for incomplete multibyte sequences and for ANSI escape sequences ending inside the boundary, with unit tests.

The series has since moved closer to a design from Junio C Hamano: a counted function, display_mode_esc_sequence_len_counted(), that never reads at or beyond ptr[len], with display_mode_esc_sequence_len() as a thin wrapper for NUL-terminated strings. Junio's version also uses it in strbuf_utf8_replace() and strbuf_add_wrapped_text(). Gouders's v5 notes describe taking care to avoid O(n²) degradation by not calling strlen() on NUL-terminated strings.

In the latest messages Gouders said he will take Junio's function as suggested, check it for `s + len` problems, and have the wrapper call it with SIZE_MAX. He said the O(n²) risk is also fixed by this, and that he will fix other points raised in review. A new version is expected.
