From: Ihar Hrachyshka <ihar.hrachyshka@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [PATCH v2] precompose_utf8: use a flex array for d_name
Date: Sat, 4 Jul 2026 19:37:24 -0400 [thread overview]
Message-ID: <20260704233724.16928-1-ihar.hrachyshka@gmail.com> (raw)
In-Reply-To: <20260703023554.36577-1-ihar.hrachyshka@gmail.com>
On macOS, git status may abort while reading a directory entry
whose UTF-8 name grows past NAME_MAX bytes:
__chk_fail_overflow
__strlcpy_chk
precompose_utf8_readdir
read_directory_recursive
wt_status_collect
cmd_status
The precompose wrapper already reallocates dirent_prec_psx for
long names, but d_name is declared as char[NAME_MAX + 1]. A
fortified libc can still see that declared object size and reject a
larger strlcpy bound, even though the allocation was grown.
Make d_name a FLEX_ARRAY and size allocations from offsetof(). That
matches the actual object layout with the dynamic allocation, so the
fortified copy sees a destination whose size can grow with max_name_len.
Add a regression test that creates an over-NAME_MAX non-ASCII basename
and runs status with core.precomposeunicode enabled.
Signed-off-by: Ihar Hrachyshka <ihar.hrachyshka@gmail.com>
---
Changes in v2:
- Drop perl from the regression test and use printf/tr instead.
- Use the minimal 256-byte filename that reproduces the crash.
compat/precompose_utf8.c | 12 ++++++++----
compat/precompose_utf8.h | 9 +++++----
t/t3910-mac-os-precompose.sh | 16 ++++++++++++++++
3 files changed, 29 insertions(+), 8 deletions(-)
diff --git a/compat/precompose_utf8.c b/compat/precompose_utf8.c
index 1711794..8077f62 100644
--- a/compat/precompose_utf8.c
+++ b/compat/precompose_utf8.c
@@ -19,6 +19,11 @@ typedef char *iconv_ibp;
static const char *repo_encoding = "UTF-8";
static const char *path_encoding = "UTF-8-MAC";
+static size_t dirent_prec_psx_size(size_t max_name_len)
+{
+ return st_add(offsetof(dirent_prec_psx, d_name), max_name_len);
+}
+
static size_t has_non_ascii(const char *s, size_t maxlen, size_t *strlen_c)
{
const uint8_t *ptr = (const uint8_t *)s;
@@ -114,8 +119,8 @@ const char *precompose_argv_prefix(int argc, const char **argv, const char *pref
PREC_DIR *precompose_utf8_opendir(const char *dirname)
{
PREC_DIR *prec_dir = xmalloc(sizeof(PREC_DIR));
- prec_dir->dirent_nfc = xmalloc(sizeof(dirent_prec_psx));
- prec_dir->dirent_nfc->max_name_len = sizeof(prec_dir->dirent_nfc->d_name);
+ prec_dir->dirent_nfc = xmalloc(dirent_prec_psx_size(NAME_MAX + 1));
+ prec_dir->dirent_nfc->max_name_len = NAME_MAX + 1;
prec_dir->dirp = opendir(dirname);
if (!prec_dir->dirp) {
@@ -145,8 +150,7 @@ struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *prec_dir)
int ret_errno = errno;
if (new_maxlen > prec_dir->dirent_nfc->max_name_len) {
- size_t new_len = sizeof(dirent_prec_psx) + new_maxlen -
- sizeof(prec_dir->dirent_nfc->d_name);
+ size_t new_len = dirent_prec_psx_size(new_maxlen);
prec_dir->dirent_nfc = xrealloc(prec_dir->dirent_nfc, new_len);
prec_dir->dirent_nfc->max_name_len = new_maxlen;
diff --git a/compat/precompose_utf8.h b/compat/precompose_utf8.h
index fea06cf..c7c3cc2 100644
--- a/compat/precompose_utf8.h
+++ b/compat/precompose_utf8.h
@@ -14,11 +14,12 @@ typedef struct dirent_prec_psx {
/*
* See http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/dirent.h.html
- * NAME_MAX + 1 should be enough, but some systems have
- * NAME_MAX=255 and strlen(d_name) may return 508 or 510
- * Solution: allocate more when needed, see precompose_utf8_readdir()
+ * Start with room for NAME_MAX + 1 bytes, but keep d_name as a
+ * flexible array. Some systems have NAME_MAX=255 while strlen(d_name)
+ * from readdir() may return 508 or 510 bytes. Grow the allocation as
+ * needed in precompose_utf8_readdir().
*/
- char d_name[NAME_MAX+1];
+ char d_name[FLEX_ARRAY];
} dirent_prec_psx;
diff --git a/t/t3910-mac-os-precompose.sh b/t/t3910-mac-os-precompose.sh
index 6d5918c..ea75fb4 100755
--- a/t/t3910-mac-os-precompose.sh
+++ b/t/t3910-mac-os-precompose.sh
@@ -207,6 +207,22 @@ test_expect_success "Add long precomposed filename" '
git commit -m "Long filename"
'
+test_expect_success "status with long non-ASCII filename" '
+ test_when_finished "rm -rf long-utf8-status" &&
+ git init long-utf8-status &&
+ (
+ cd long-utf8-status &&
+ test "$(git config --bool core.precomposeunicode)" = true &&
+ long_utf8_name=$(
+ printf "%253s\342\200\224" "" |
+ tr " " a
+ ) &&
+ test "$(printf "%s" "$long_utf8_name" | wc -c | tr -d " ")" = 256 &&
+ printf "content\n" >"$long_utf8_name" &&
+ git status --porcelain=v1 >actual
+ )
+'
+
test_expect_failure 'handle existing decomposed filenames' '
echo content >"verbatim.$Adiarnfd" &&
git -c core.precomposeunicode=false add "verbatim.$Adiarnfd" &&
--
2.54.0
prev parent reply other threads:[~2026-07-04 23:37 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-03 2:35 [PATCH] precompose_utf8: use a flex array for d_name Ihar Hrachyshka
2026-07-03 5:08 ` Torsten Bögershausen
2026-07-03 8:39 ` Junio C Hamano
2026-07-03 8:40 ` Patrick Steinhardt
2026-07-03 20:20 ` Ihar Hrachyshka
2026-07-04 23:37 ` Ihar Hrachyshka [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=20260704233724.16928-1-ihar.hrachyshka@gmail.com \
--to=ihar.hrachyshka@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