From 56772ebe9a5ce3585d2d5cdca3fa1e7fcb29405d Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Tue, 12 Mar 2013 22:32:39 +0000 Subject: [Support][Path] Don't inf loop if creating the parent directory fails. Patch by Paul Robinson. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@176908 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/Windows/PathV2.inc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lib/Support/Windows/PathV2.inc') diff --git a/lib/Support/Windows/PathV2.inc b/lib/Support/Windows/PathV2.inc index 2e6cc96e7f..823f7583da 100644 --- a/lib/Support/Windows/PathV2.inc +++ b/lib/Support/Windows/PathV2.inc @@ -593,6 +593,10 @@ retry_random_path: random_path_utf16.push_back(0); random_path_utf16.pop_back(); + // Make sure we don't fall into an infinite loop by constantly trying + // to create the parent path. + bool TriedToCreateParent = false; + // Try to create + open the path. retry_create_file: HANDLE TempFileHandle = ::CreateFileW(random_path_utf16.begin(), @@ -610,7 +614,9 @@ retry_create_file: if (ec == windows_error::file_exists) goto retry_random_path; // Check for non-existing parent directories. - if (ec == windows_error::path_not_found) { + if (ec == windows_error::path_not_found && !TriedToCreateParent) { + TriedToCreateParent = true; + // Create the directories using result_path as temp storage. if (error_code ec = UTF16ToUTF8(random_path_utf16.begin(), random_path_utf16.size(), result_path)) -- cgit v1.2.3-18-g5258 From cc3a595ab938352f3acf8652c5858ddf879524a5 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Thu, 14 Mar 2013 00:20:10 +0000 Subject: [Support] Fix lifetime of file descriptors when using MemoryBuffer. Clients of MemoryBuffer::getOpenFile expect it not to take ownership of the file descriptor passed in. So don't. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@176995 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/Windows/PathV2.inc | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'lib/Support/Windows/PathV2.inc') diff --git a/lib/Support/Windows/PathV2.inc b/lib/Support/Windows/PathV2.inc index 823f7583da..0f657bf3b9 100644 --- a/lib/Support/Windows/PathV2.inc +++ b/lib/Support/Windows/PathV2.inc @@ -711,12 +711,13 @@ error_code get_magic(const Twine &path, uint32_t len, return error_code::success(); } -error_code mapped_file_region::init(int FD, uint64_t Offset) { +error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) { FileDescriptor = FD; // Make sure that the requested size fits within SIZE_T. if (Size > std::numeric_limits::max()) { if (FileDescriptor) - _close(FileDescriptor); + if (CloseFD) + _close(FileDescriptor); else ::CloseHandle(FileHandle); return make_error_code(errc::invalid_argument); @@ -739,7 +740,8 @@ error_code mapped_file_region::init(int FD, uint64_t Offset) { if (FileMappingHandle == NULL) { error_code ec = windows_error(GetLastError()); if (FileDescriptor) - _close(FileDescriptor); + if (CloseFD) + _close(FileDescriptor); else ::CloseHandle(FileHandle); return ec; @@ -761,7 +763,8 @@ error_code mapped_file_region::init(int FD, uint64_t Offset) { error_code ec = windows_error(GetLastError()); ::CloseHandle(FileMappingHandle); if (FileDescriptor) - _close(FileDescriptor); + if (CloseFD) + _close(FileDescriptor); else ::CloseHandle(FileHandle); return ec; @@ -775,13 +778,23 @@ error_code mapped_file_region::init(int FD, uint64_t Offset) { ::UnmapViewOfFile(Mapping); ::CloseHandle(FileMappingHandle); if (FileDescriptor) - _close(FileDescriptor); + if (CloseFD) + _close(FileDescriptor); else ::CloseHandle(FileHandle); return ec; } Size = mbi.RegionSize; } + + // Close all the handles except for the view. It will keep the other handles + // alive. + ::CloseHandle(FileMappingHandle); + if (FileDescriptor) + if (CloseFD) + _close(FileDescriptor); // Also closes FileHandle. + else + ::CloseHandle(FileHandle); return error_code::success(); } @@ -821,7 +834,7 @@ mapped_file_region::mapped_file_region(const Twine &path, } FileDescriptor = 0; - ec = init(FileDescriptor, offset); + ec = init(FileDescriptor, true, offset); if (ec) { Mapping = FileMappingHandle = 0; FileHandle = INVALID_HANDLE_VALUE; @@ -830,6 +843,7 @@ mapped_file_region::mapped_file_region(const Twine &path, } mapped_file_region::mapped_file_region(int fd, + bool closefd, mapmode mode, uint64_t length, uint64_t offset, @@ -842,13 +856,14 @@ mapped_file_region::mapped_file_region(int fd, , FileMappingHandle() { FileHandle = reinterpret_cast(_get_osfhandle(fd)); if (FileHandle == INVALID_HANDLE_VALUE) { - _close(FileDescriptor); + if (closefd) + _close(FileDescriptor); FileDescriptor = 0; ec = make_error_code(errc::bad_file_descriptor); return; } - ec = init(FileDescriptor, offset); + ec = init(FileDescriptor, closefd, offset); if (ec) { Mapping = FileMappingHandle = 0; FileHandle = INVALID_HANDLE_VALUE; @@ -859,12 +874,6 @@ mapped_file_region::mapped_file_region(int fd, mapped_file_region::~mapped_file_region() { if (Mapping) ::UnmapViewOfFile(Mapping); - if (FileMappingHandle) - ::CloseHandle(FileMappingHandle); - if (FileDescriptor) - _close(FileDescriptor); - else if (FileHandle != INVALID_HANDLE_VALUE) - ::CloseHandle(FileHandle); } #if LLVM_HAS_RVALUE_REFERENCES -- cgit v1.2.3-18-g5258 From 1f81ebcbae0a319a9a62c3a29b4519e4ea40333e Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Fri, 15 Mar 2013 19:25:47 +0000 Subject: [Support][Path][Windows] Fix dangling else. Don't call CloseHandle when CloseFD is false. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@177175 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/Windows/PathV2.inc | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'lib/Support/Windows/PathV2.inc') diff --git a/lib/Support/Windows/PathV2.inc b/lib/Support/Windows/PathV2.inc index 0f657bf3b9..23f3d14f91 100644 --- a/lib/Support/Windows/PathV2.inc +++ b/lib/Support/Windows/PathV2.inc @@ -715,10 +715,10 @@ error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) { FileDescriptor = FD; // Make sure that the requested size fits within SIZE_T. if (Size > std::numeric_limits::max()) { - if (FileDescriptor) + if (FileDescriptor) { if (CloseFD) _close(FileDescriptor); - else + } else ::CloseHandle(FileHandle); return make_error_code(errc::invalid_argument); } @@ -739,10 +739,10 @@ error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) { 0); if (FileMappingHandle == NULL) { error_code ec = windows_error(GetLastError()); - if (FileDescriptor) + if (FileDescriptor) { if (CloseFD) _close(FileDescriptor); - else + } else ::CloseHandle(FileHandle); return ec; } @@ -762,10 +762,10 @@ error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) { if (Mapping == NULL) { error_code ec = windows_error(GetLastError()); ::CloseHandle(FileMappingHandle); - if (FileDescriptor) + if (FileDescriptor) { if (CloseFD) _close(FileDescriptor); - else + } else ::CloseHandle(FileHandle); return ec; } @@ -777,10 +777,10 @@ error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) { error_code ec = windows_error(GetLastError()); ::UnmapViewOfFile(Mapping); ::CloseHandle(FileMappingHandle); - if (FileDescriptor) + if (FileDescriptor) { if (CloseFD) _close(FileDescriptor); - else + } else ::CloseHandle(FileHandle); return ec; } @@ -790,10 +790,10 @@ error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) { // Close all the handles except for the view. It will keep the other handles // alive. ::CloseHandle(FileMappingHandle); - if (FileDescriptor) + if (FileDescriptor) { if (CloseFD) _close(FileDescriptor); // Also closes FileHandle. - else + } else ::CloseHandle(FileHandle); return error_code::success(); } -- cgit v1.2.3-18-g5258