GetCurrentDirectory() returns the number of characters copied; 0 is a failure, not a success.
Add implementation for chdir().
Differential D9300
Windows: fix bug in getcwd() and add chdir(). ted on Apr 27 2015, 10:28 AM. Authored by
Details GetCurrentDirectory() returns the number of characters copied; 0 is a failure, not a success. Add implementation for chdir().
Diff Detail Event Timeline
Comment Actions Changed getcwd() and chdir() to use _getcwd() and _chdir(). Removed #ifdef _WIN32 disabling calls to chdir() in Platform.cpp and GDBRemoteCommunicationServerPlatform.cpp.
Comment Actions llvm::sys::fs::current_path() doesn't handle paths > MAX_PATH-1: std::error_code current_path(SmallVectorImpl<char> &result) { SmallVector<wchar_t, MAX_PATH> cur_path; DWORD len = MAX_PATH; do { cur_path.reserve(len); len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data()); // A zero return value indicates a failure other than insufficient space. if (len == 0) return windows_error(::GetLastError()); // If there's insufficient space, the len returned is larger than the len // given. } while (len > cur_path.capacity()); // On success, GetCurrentDirectoryW returns the number of characters not // including the null-terminator. cur_path.set_size(len); return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result); } The MSDN page for _getcwd() says that it supports paths > MAX_PATH if you pass NULL for the buffer. I'm not sure what happens if you pass a size > MAX_PATH. What should getcwd() do if the path is > MAX_PATH? I could change it so if the first call returns an error, errno is ERANGE and max > MAX_PATH, call _getcwd() again with a NULL, then check that strlen(new buffer) < max, strcpy the new buffer into path and free it. Comment Actions Whoops - missed the do loop upping the size of the buffer to the return value. But I can't find any documentation saying that GetCurrentDirectory() supports paths > MAX_PATH. Are we sure it does? Comment Actions The documentation for SetCurrentDirectory actually says it can't be longer than MAX_PATH. So maybe we can assume GetCurrentDirectory has the same issue. In any case, I guess it's not so important to change this. It still seems like a nice cleanup, but not very high priority, so feel free to commit as is. |
This is fine for now, but it's worth pointing out this is still broken for paths longer than MAX_PATH. So if you ever run LLDB from a directory longer than 256 characters, things will get messed up. If you don't want to change this to use llvm::sys::fs::current_path(), can you add a TODO mentioning the problem?