summaryrefslogtreecommitdiff
path: root/lib/Support
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2004-05-28 00:34:42 +0000
committerChris Lattner <sabre@nondot.org>2004-05-28 00:34:42 +0000
commite53477e56ce6ead4a17e66c53689c8feb2df7dd5 (patch)
tree42c13da15ffba01af61f1ece0184bfb54319be2e /lib/Support
parent531b802e132af92aac4384206bdc6b8a4d28aa00 (diff)
downloadllvm-e53477e56ce6ead4a17e66c53689c8feb2df7dd5.tar.gz
llvm-e53477e56ce6ead4a17e66c53689c8feb2df7dd5.tar.bz2
llvm-e53477e56ce6ead4a17e66c53689c8feb2df7dd5.tar.xz
Add support for zero length files
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13866 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support')
-rw-r--r--lib/Support/FileUtilities.cpp11
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/Support/FileUtilities.cpp b/lib/Support/FileUtilities.cpp
index 74df8b253c..ed594db96e 100644
--- a/lib/Support/FileUtilities.cpp
+++ b/lib/Support/FileUtilities.cpp
@@ -230,11 +230,17 @@ void *llvm::ReadFileIntoAddressSpace(const std::string &Filename,
FDHandle FD(open(Filename.c_str(), O_RDONLY));
if (FD == -1) return 0;
+ // If the file has a length of zero, mmap might return a null pointer. In
+ // this case, allocate a single byte of memory and return it instead.
+ if (Length == 0)
+ return malloc(1);
+
// mmap in the file all at once...
void *Buffer = (void*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0);
if (Buffer == (void*)MAP_FAILED)
return 0;
+
return Buffer;
#else
// FIXME: implement with read/write
@@ -246,7 +252,10 @@ void *llvm::ReadFileIntoAddressSpace(const std::string &Filename,
/// address space.
void llvm::UnmapFileFromAddressSpace(void *Buffer, unsigned Length) {
#ifdef HAVE_MMAP_FILE
- munmap((char*)Buffer, Length);
+ if (Length)
+ munmap((char*)Buffer, Length);
+ else
+ free(Buffer); // Zero byte files are malloc(1)'s.
#else
free(Buffer);
#endif