dir.c: use st_add3() for allocation size

When preparing a manufactured dirent instance, we add a length of
path to the size of struct to decide how many bytes to allocate.
Make sure this addition does not wrap-around to cause us
underallocate.

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano 2019-12-20 09:55:53 -08:00
parent c847dfafee
commit 6836d2fe06

2
dir.c
View File

@ -2154,7 +2154,7 @@ static int treat_leading_path(struct dir_struct *dir,
* For either case, padding with len+1 bytes at the end will ensure
* sufficient storage space.
*/
de = xcalloc(1, sizeof(struct dirent)+len+1);
de = xcalloc(1, st_add3(sizeof(struct dirent), len, 1));
memset(&cdir, 0, sizeof(cdir));
cdir.de = de;
#if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)