From 14aef12e244dc5ea1d315acaabb26f33988e44c3 Mon Sep 17 00:00:00 2001 From: Jason Molenda Date: Thu, 4 Apr 2013 03:19:27 +0000 Subject: [PATCH] Change EnumerateDirectory from using readdir() to using readdir_r() so it can be re-entered while iterating over a directory safely. llvm-svn: 178738 --- lldb/source/Host/common/FileSpec.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lldb/source/Host/common/FileSpec.cpp b/lldb/source/Host/common/FileSpec.cpp index 8b986c2a7fbe..c8cb96cc5655 100644 --- a/lldb/source/Host/common/FileSpec.cpp +++ b/lldb/source/Host/common/FileSpec.cpp @@ -891,8 +891,15 @@ FileSpec::EnumerateDirectory lldb_utility::CleanUp dir_path_dir (opendir(dir_path), NULL, closedir); if (dir_path_dir.is_valid()) { - struct dirent* dp; - while ((dp = readdir(dir_path_dir.get())) != NULL) + long path_max = fpathconf (dirfd (dir_path_dir.get()), _PC_NAME_MAX); +#if defined (__APPLE_) && defined (__DARWIN_MAXPATHLEN) + if (path_max < __DARWIN_MAXPATHLEN) + path_max = __DARWIN_MAXPATHLEN; +#endif + struct dirent *buf, *dp; + buf = (struct dirent *) malloc (offsetof (struct dirent, d_name) + path_max + 1); + + while (buf && readdir_r(dir_path_dir.get(), buf, &dp) == 0 && dp) { // Only search directories if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN) @@ -969,6 +976,10 @@ FileSpec::EnumerateDirectory } } } + if (buf) + { + free (buf); + } } } // By default when exiting a directory, we tell the parent enumeration