[LLVMdev] Infinite recursion in sys::fs::create_directories()

Kal Conley kcconley at gmail.com
Wed Mar 21 05:20:05 PDT 2012


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




More information about the llvm-dev mailing list