From: Junio C Hamano <gitster@pobox.com>
To: "Elijah Newren via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org,
LorenzoPegorari <lorenzo.pegorari2002@gmail.com>,
Elijah Newren <newren@gmail.com>
Subject: Re: [PATCH v3] diff: fix out-of-bounds reads and NULL deref in diffstat UTF-8 truncation
Date: Mon, 20 Apr 2026 09:41:57 -0700 [thread overview]
Message-ID: <xmqq8qahr3ca.fsf@gitster.g> (raw)
In-Reply-To: <pull.2093.v3.git.1776699778177.gitgitgadget@gmail.com> (Elijah Newren via GitGitGadget's message of "Mon, 20 Apr 2026 15:42:58 +0000")
"Elijah Newren via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Elijah Newren <newren@gmail.com>
>
> f85b49f3d4a (diff: improve scaling of filenames in diffstat to handle
> UTF-8 chars, 2026-01-16) introduced a loop in show_stats() that calls
> utf8_width() repeatedly to skip leading characters until the displayed
> width fits. However, utf8_width() can return problematic values:
>
> - For invalid UTF-8 sequences, pick_one_utf8_char() sets the name
> pointer to NULL and utf8_width() returns 0. Since name_len does
> not change, the loop iterates once more and pick_one_utf8_char()
> dereferences the NULL pointer, crashing.
>
> - For control characters, utf8_width() returns -1, so name_len
> grows when it is expected to shrink. This can cause the loop to
> consume more characters than the string contains, reading past
> the trailing NUL.
>
> By default, fill_print_name() will C-quote filenames which escapes
> control characters and invalid bytes to printable text. That avoids
> this bug from being triggered; however, with core.quotePath=false,
> most characters are no longer escaped (though some control characters
> still are) and raw bytes can reach this code.
>
> Add tests exercising both failure modes with core.quotePath=false and
> a narrow --stat-name-width to force truncation: one with a bare 0xC0
> byte (invalid UTF-8 lead byte, triggers NULL deref) and one with
> several C1 control characters (repeats of 0xC2 0x9F, causing
> the loop to read past the end of the string). The second test
> reliably catches the out-of-bounds read when run under ASan, though
> it may pass silently without sanitizers.
>
> Fix both issues by introducing utf8_ish_width(), a thin wrapper
> around utf8_width() that guarantees the pointer always advances and
> the returned width is never negative:
>
> - On invalid UTF-8 it restores the pointer, advances by one byte,
> and returns width 1 (matching the strlen()-based fallback used
> by utf8_strwidth()).
> - On a control character it returns 0 (matching utf8_strnwidth()
> which skips them).
>
> Also add a "&& *name" guard to the while-loop condition so it
> terminates at end-of-string even when utf8_strwidth()'s strlen()
> fallback causes name_len to exceed the sum of per-character widths.
OK, that does sounds sensible.
If we start from a valid UTF-8 string, chomp a few bytes from the
tail end of it, and feed it into this loop, the initial part of the
last character is fed to utf8_width(), which hopefully is already
prepared to honor the NUL termination to avoid an OOB read while
returning an error. And eventually we would see that NUL that
truncated the last UTF-8 multi-byte letter ourselves in the loop and
that is where this new loop terminating condition would help.
prev parent reply other threads:[~2026-04-20 16:42 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-17 16:26 [PATCH] diff: fix out-of-bounds reads and NULL deref in diffstat UTF-8 truncation Elijah Newren via GitGitGadget
2026-04-17 19:21 ` Junio C Hamano
2026-04-17 22:00 ` Elijah Newren
2026-04-17 22:21 ` Junio C Hamano
2026-04-17 22:45 ` [PATCH v2] " Elijah Newren via GitGitGadget
2026-04-19 23:52 ` Lorenzo Pegorari
2026-04-20 14:51 ` Elijah Newren
2026-04-20 15:42 ` [PATCH v3] " Elijah Newren via GitGitGadget
2026-04-20 16:41 ` Junio C Hamano [this message]
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=xmqq8qahr3ca.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=lorenzo.pegorari2002@gmail.com \
--cc=newren@gmail.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