Kal Conley
2012-Mar-22 21:11 UTC
[LLVMdev] Infinite recursion in sys::fs::create_directories()
Hi, sys::fs::create_directories() recurses infinitely for relative paths with only one directory or where the first directory in path doesn't exist. This was observed in r153176. Example: #include <llvm/Support/FileSystem.h> using namespace llvm; int main(int argc, char *argv[]) { bool existed; error_code ec = sys::fs::create_directories(Twine("log"), existed); return 0; } recurses infinitely in sys::fs::create_directories(). This happens because the parent of "log" is "" which doesn't exist so the function recurses and looks for the parent or "" which is "" which doesn't exist etc. The function should perhaps check if parent is empty. Here is how I fixed it: //------------------ error_code create_directories(const Twine &path, bool &existed) { SmallString<128> path_storage; StringRef p = path.toStringRef(path_storage); StringRef parent = path::parent_path(p); if (!parent.empty()) { bool parent_exists; if (error_code ec = exists(parent, parent_exists)) return ec; if (!parent_exists) if (error_code ec = create_directories(parent, existed)) return ec; } return create_directory(p, existed); } //------------------ Thanks, Kal
Michael Spencer
2012-Mar-22 21:20 UTC
[LLVMdev] Infinite recursion in sys::fs::create_directories()
On Thu, Mar 22, 2012 at 2:11 PM, Kal Conley <kcconley at gmail.com> wrote:> Hi, > > sys::fs::create_directories() recurses infinitely for relative paths > with only one directory or where the first directory in path doesn't > exist. This was observed in r153176. > > Example: > > #include <llvm/Support/FileSystem.h> > > using namespace llvm; > > int main(int argc, char *argv[]) > { > bool existed; > error_code ec = sys::fs::create_directories(Twine("log"), existed); > return 0; > } > > recurses infinitely in sys::fs::create_directories(). > > This happens because the parent of "log" is "" which doesn't exist so > the function recurses and looks for the parent or "" which is "" which > doesn't exist etc. The function should perhaps check if parent is empty. > > Here is how I fixed it: > > //------------------ > > error_code create_directories(const Twine &path, bool &existed) { > SmallString<128> path_storage; > StringRef p = path.toStringRef(path_storage); > > StringRef parent = path::parent_path(p); > if (!parent.empty()) { > bool parent_exists; > if (error_code ec = exists(parent, parent_exists)) return ec; > > if (!parent_exists) > if (error_code ec = create_directories(parent, existed)) return ec; > } > return create_directory(p, existed); > } > > //------------------ > > Thanks, > KalThanks, fixed in: r153225 - Michael Spencer
Seemingly Similar Threads
- [LLVMdev] Infinite recursion in sys::fs::create_directories()
- Redundant Twine->StringRef->Twine conversions in llvm::sys::fs::make_absolute?
- [LLVMdev] [RFC] llvm/include/Support/FileOutputBuffer.h
- [LLVMdev] [RFC] llvm/include/Support/FileOutputBuffer.h
- [LLVMdev] llvm/include/Support/FileSystem.h