Use readdir() instead of deprecated readdir_r() (#5330)

This commit is contained in:
chrchr-github 2023-08-16 10:20:53 +02:00 committed by GitHub
parent 824f89514a
commit 8b309a8829
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 21 deletions

View File

@ -192,29 +192,10 @@ static std::string addFiles2(std::map<std::string, std::size_t> &files,
if (!dir) if (!dir)
return ""; return "";
dirent * dir_result; std::string new_path = path;
// make sure we reserve enough space for the readdir_r() buffer;
// according to POSIX:
// The storage pointed to by entry shall be large enough for a
// dirent with an array of char d_name members containing at
// least {NAME_MAX}+1 elements.
// on some platforms, d_name is not a static sized-array but
// a pointer to space usually reserved right after the dirent
// struct; the union here allows to reserve the space and to
// provide a pointer to the right type that can be passed where
// needed without casts
union {
dirent entry;
char buf[sizeof(*dir_result) + (sizeof(dir_result->d_name) > 1 ? 0 : NAME_MAX + 1)];
} dir_result_buffer;
// TODO: suppress instead?
(void)dir_result_buffer.buf; // do not trigger cppcheck itself on the "unused buf"
std::string new_path;
new_path.reserve(path.length() + 1 + sizeof(dir_result->d_name));// prealloc some memory to avoid constant new/deletes in loop
new_path += path;
new_path += '/'; new_path += '/';
while ((SUPPRESS_DEPRECATED_WARNING(readdir_r(dir, &dir_result_buffer.entry, &dir_result)) == 0) && (dir_result != nullptr)) { while (const struct dirent* dir_result = readdir(dir)) {
if ((std::strcmp(dir_result->d_name, ".") == 0) || if ((std::strcmp(dir_result->d_name, ".") == 0) ||
(std::strcmp(dir_result->d_name, "..") == 0)) (std::strcmp(dir_result->d_name, "..") == 0))
continue; continue;