Merge branch 'ih/precompose-flex-array'

The UTF-8 precomposition wrapper on macOS has been updated to use a
flexible array member to represent the name of a directory entry,
preventing fortified libc checks from failing when the name is
reallocated to be larger than 'NAME_MAX' bytes.

* ih/precompose-flex-array:
  precompose_utf8: use a flex array for d_name
main
Junio C Hamano 2026-07-16 23:05:47 -07:00
commit 09af39fda0
3 changed files with 29 additions and 8 deletions

View File

@ -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;

View File

@ -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;



View File

@ -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" &&