freebsd / kyua

Port/Package build and test system
https://github.com/freebsd/kyua/wiki
BSD 3-Clause "New" or "Revised" License
149 stars 42 forks source link

Incorrect usage of dirent #173

Open alaviss opened 6 years ago

alaviss commented 6 years ago

From readdir(3)

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.)

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 of d_name, and some OS even define char d_name[1]. This would cause any struct member followed to be overridden when entry name exceeds d_name limitation.