Since POSIX.1 does not specify the size of the d_name field, and other nonstandard fields may precede that field within the dirent structure, portable applications that use readdir_r() should allocate the buffer whose address is passed in entry as follows:
name_max = pathconf(dirpath, _PC_NAME_MAX);
if (name_max == -1) /* Limit not defined, or error */
name_max = 255; /* Take a guess */
len = offsetof(struct dirent, d_name) + name_max + 1;
entryp = malloc(len);
(POSIX.1 requires that d_name is the last field in a struct dirent.)
From
readdir(3)
The use of
dirent
in https://github.com/jmmv/kyua/blob/e85ee4145b16295a061e6f551402d2bf46e819b7/utils/fs/directory.cpp#L134 is incorrect, as POSIX doesn't specify the size ofd_name
, and some OS even definechar d_name[1]
. This would cause any struct member followed to be overridden when entry name exceedsd_name
limitation.