Re: [PATCH v2 6/8] dir: fix checks on common prefix directory
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Dec 16, 2019, 18:13 UTC
- Message-ID
- <xmqqwoawjg09.fsf@gitster-ct.c.googlers.com>
- In-Reply-To
- <CABPp-BGoC_D6LzzMNyf30wFssTU2WA1kTLmFvJ2Do+Tfg4+YQA@mail.gmail.com>
Elijah Newren <newren@gmail.com> writes:
Show 23 quoted lines
>> > > + memset(&cdir, 0, sizeof(cdir));
>> > > + memset(&de, 0, sizeof(de));
>> > > + cdir.de = &de;
>> > > + de.d_type = DT_DIR;
>> >
>> > So here, `de` is zeroed out, and therefore `de.d_name` is `NULL`.
>>
>> Um, yeah...didn't I have an allocation of de.d_name here? It will
>> always have a subset of path copied into it, so an allocation of len+1
>> is plenty long enough.
>
> Actually, it looks like I looked up the definition of dirent
> previously and forgot by the time you emailed. On linux, from
> /usr/include/bits/dirent.h:
>
> struct dirent
> {
> ....
> unsigned char d_type;
> char d_name[256]; /* We must not include limits.h! */
> };
>
> ...Uh, oh. The size of "struct dirent" is unspecified and it is asking for trouble to allocate one yourself (iow, treat it pretty much as something you can only get a pointer to an instance from readdir()). For example, a dirent that comes back readdir() may have a lot longer name than the sizeof(.d_name[]) above may imply.
Do you really need to manufacture a dirent yourself, or can you use a more concrete type you invent yourself?