Index: base/files/memory_mapped_file.cc |
diff --git a/base/files/memory_mapped_file.cc b/base/files/memory_mapped_file.cc |
index 0fd9d6796b10734a1224556ad16ab0d2418662f9..67890d68855cb810a086da788d2f046f78240d25 100644 |
--- a/base/files/memory_mapped_file.cc |
+++ b/base/files/memory_mapped_file.cc |
@@ -30,18 +30,30 @@ MemoryMappedFile::~MemoryMappedFile() { |
} |
#if !defined(OS_NACL) |
-bool MemoryMappedFile::Initialize(const FilePath& file_name) { |
+bool MemoryMappedFile::Initialize(const FilePath& file_name, Access access) { |
if (IsValid()) |
return false; |
- file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); |
+ uint32_t flags = 0; |
+ switch (access) { |
+ case READ_ONLY: |
+ flags = File::FLAG_OPEN | File::FLAG_READ; |
+ break; |
+ case READ_WRITE: |
+ flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; |
+ break; |
+ case READ_WRITE_EXTEND: |
+ // Can't open with "extend" because no maximum size is known. |
+ NOTREACHED(); |
+ } |
+ file_.Initialize(file_name, flags); |
if (!file_.IsValid()) { |
DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); |
return false; |
} |
- if (!MapFileRegionToMemory(Region::kWholeFile)) { |
+ if (!MapFileRegionToMemory(Region::kWholeFile, access)) { |
CloseHandles(); |
return false; |
} |
@@ -49,11 +61,32 @@ bool MemoryMappedFile::Initialize(const FilePath& file_name) { |
return true; |
} |
-bool MemoryMappedFile::Initialize(File file) { |
- return Initialize(std::move(file), Region::kWholeFile); |
+bool MemoryMappedFile::Initialize(File file, Access access) { |
+ DCHECK_NE(READ_WRITE_EXTEND, access); |
+ return Initialize(std::move(file), Region::kWholeFile, access); |
} |
-bool MemoryMappedFile::Initialize(File file, const Region& region) { |
+bool MemoryMappedFile::Initialize(File file, |
+ const Region& region, |
+ Access access) { |
+ switch (access) { |
+ case READ_WRITE_EXTEND: |
+ // Ensure that the extended size is within limits of File. |
+ if (region.size > std::numeric_limits<int64_t>::max() - region.offset) { |
+ DLOG(ERROR) << "Region bounds exceed maximum for base::File."; |
+ return false; |
+ } |
+ // Fall through. |
+ case READ_ONLY: |
+ case READ_WRITE: |
+ // Ensure that the region values are valid. |
+ if (region.offset < 0 || region.size < 0) { |
+ DLOG(ERROR) << "Region bounds are not valid."; |
+ return false; |
+ } |
+ break; |
+ } |
+ |
if (IsValid()) |
return false; |
@@ -64,7 +97,7 @@ bool MemoryMappedFile::Initialize(File file, const Region& region) { |
file_ = std::move(file); |
- if (!MapFileRegionToMemory(region)) { |
+ if (!MapFileRegionToMemory(region, access)) { |
CloseHandles(); |
return false; |
} |