dir: fix malloc of root untracked_cache_dir
commit6347d649bcddf531f82d400103e23d99ea8f2fd4
authorJeff Hostetler <jeffhost@microsoft.com>
Wed, 24 Feb 2021 14:31:57 +0000 (24 14:31 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Feb 2021 20:09:10 +0000 (24 12:09 -0800)
tree974c04458fe124407f7d011b85b753b48b2e7ae0
parent59ec22464f6c2b170b05f287e00740ea2288fe5c
dir: fix malloc of root untracked_cache_dir

Use FLEX_ALLOC_STR() to allocate the `struct untracked_cache_dir`
for the root directory.  Get rid of unsafe code that might fail to
initialize the `name` field (if FLEX_ARRAY is not 1).  This will
make it clear that we intend to have a structure with an empty
string following it.

A problem was observed on Windows where the length of the memset() was
too short, so the first byte of the name field was not zeroed.  This
resulted in the name field having garbage from a previous use of that
area of memory.

The record for the root directory was then written to the untracked-cache
extension in the index.  This garbage would then be visible to future
commands when they reloaded the untracked-cache extension.

Since the directory record for the root directory had garbage in the
`name` field, the `t/helper/test-tool dump-untracked-cache` tool
printed this garbage as the path prefix (rather than '/') for each
directory in the untracked cache as it recursed.

Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
Reviewed-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
dir.c