Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect usage of dirent #173

Open
alaviss opened this issue Jan 1, 2018 · 0 comments
Open

Incorrect usage of dirent #173

alaviss opened this issue Jan 1, 2018 · 0 comments

Comments

@alaviss
Copy link

alaviss commented Jan 1, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant