Skip to content

Commit 8b309a8

Browse files
Use readdir() instead of deprecated readdir_r() (#5330)
1 parent 824f895 commit 8b309a8

1 file changed

Lines changed: 2 additions & 21 deletions

File tree

cli/filelister.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -192,29 +192,10 @@ static std::string addFiles2(std::map<std::string, std::size_t> &files,
192192
if (!dir)
193193
return "";
194194

195-
dirent * dir_result;
196-
// make sure we reserve enough space for the readdir_r() buffer;
197-
// according to POSIX:
198-
// The storage pointed to by entry shall be large enough for a
199-
// dirent with an array of char d_name members containing at
200-
// least {NAME_MAX}+1 elements.
201-
// on some platforms, d_name is not a static sized-array but
202-
// a pointer to space usually reserved right after the dirent
203-
// struct; the union here allows to reserve the space and to
204-
// provide a pointer to the right type that can be passed where
205-
// needed without casts
206-
union {
207-
dirent entry;
208-
char buf[sizeof(*dir_result) + (sizeof(dir_result->d_name) > 1 ? 0 : NAME_MAX + 1)];
209-
} dir_result_buffer;
210-
// TODO: suppress instead?
211-
(void)dir_result_buffer.buf; // do not trigger cppcheck itself on the "unused buf"
212-
std::string new_path;
213-
new_path.reserve(path.length() + 1 + sizeof(dir_result->d_name));// prealloc some memory to avoid constant new/deletes in loop
214-
new_path += path;
195+
std::string new_path = path;
215196
new_path += '/';
216197

217-
while ((SUPPRESS_DEPRECATED_WARNING(readdir_r(dir, &dir_result_buffer.entry, &dir_result)) == 0) && (dir_result != nullptr)) {
198+
while (const struct dirent* dir_result = readdir(dir)) {
218199
if ((std::strcmp(dir_result->d_name, ".") == 0) ||
219200
(std::strcmp(dir_result->d_name, "..") == 0))
220201
continue;

0 commit comments

Comments
 (0)