From 7711efbf062a77f28deac68716f808fba071803a Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Tue, 13 Aug 2019 10:02:59 -0700 Subject: [PATCH] Return an empty mapping for an empty file asset (#10815) Fixes https://github.com/flutter/flutter/issues/36574 --- assets/directory_asset_bundle.cc | 2 +- fml/file_unittest.cc | 14 ++++++++++++++ fml/mapping.cc | 4 ++-- fml/mapping.h | 3 +++ fml/platform/posix/mapping_posix.cc | 8 +++++++- fml/platform/win/mapping_win.cc | 10 ++++++++++ 6 files changed, 37 insertions(+), 4 deletions(-) diff --git a/assets/directory_asset_bundle.cc b/assets/directory_asset_bundle.cc index 734e678bd3904..5ad7297313c99 100644 --- a/assets/directory_asset_bundle.cc +++ b/assets/directory_asset_bundle.cc @@ -38,7 +38,7 @@ std::unique_ptr DirectoryAssetBundle::GetAsMapping( auto mapping = std::make_unique(fml::OpenFile( descriptor_, asset_name.c_str(), false, fml::FilePermission::kRead)); - if (mapping->GetMapping() == nullptr) { + if (!mapping->IsValid()) { return nullptr; } diff --git a/fml/file_unittest.cc b/fml/file_unittest.cc index c1ed8f63731c4..c492a0a36a93e 100644 --- a/fml/file_unittest.cc +++ b/fml/file_unittest.cc @@ -155,3 +155,17 @@ TEST(FileTest, AtomicWriteTest) { // Cleanup. ASSERT_TRUE(fml::UnlinkFile(dir.fd(), "precious_data")); } + +TEST(FileTest, EmptyMappingTest) { + fml::ScopedTemporaryDirectory dir; + + auto file = fml::OpenFile(dir.fd(), "my_contents", true, + fml::FilePermission::kReadWrite); + + fml::FileMapping mapping(file); + ASSERT_TRUE(mapping.IsValid()); + ASSERT_EQ(mapping.GetSize(), 0ul); + ASSERT_EQ(mapping.GetMapping(), nullptr); + + ASSERT_TRUE(fml::UnlinkFile(dir.fd(), "my_contents")); +} diff --git a/fml/mapping.cc b/fml/mapping.cc index 7ffacfb493890..bb69052e1945c 100644 --- a/fml/mapping.cc +++ b/fml/mapping.cc @@ -31,7 +31,7 @@ std::unique_ptr FileMapping::CreateReadOnly( auto mapping = std::make_unique( base_fd, std::initializer_list{Protection::kRead}); - if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) { + if (!mapping->IsValid()) { return nullptr; } @@ -56,7 +56,7 @@ std::unique_ptr FileMapping::CreateReadExecute( base_fd, std::initializer_list{Protection::kRead, Protection::kExecute}); - if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) { + if (!mapping->IsValid()) { return nullptr; } diff --git a/fml/mapping.h b/fml/mapping.h index 90b88cc15840c..ceeb24662e1c3 100644 --- a/fml/mapping.h +++ b/fml/mapping.h @@ -67,7 +67,10 @@ class FileMapping final : public Mapping { uint8_t* GetMutableMapping(); + bool IsValid() const; + private: + bool valid_ = false; size_t size_ = 0; uint8_t* mapping_ = nullptr; uint8_t* mutable_mapping_ = nullptr; diff --git a/fml/platform/posix/mapping_posix.cc b/fml/platform/posix/mapping_posix.cc index bc5adbd107544..535a72ebe4ccf 100644 --- a/fml/platform/posix/mapping_posix.cc +++ b/fml/platform/posix/mapping_posix.cc @@ -63,7 +63,8 @@ FileMapping::FileMapping(const fml::UniqueFD& handle, return; } - if (stat_buffer.st_size <= 0) { + if (stat_buffer.st_size == 0) { + valid_ = true; return; } @@ -79,6 +80,7 @@ FileMapping::FileMapping(const fml::UniqueFD& handle, mapping_ = static_cast(mapping); size_ = stat_buffer.st_size; + valid_ = true; if (is_writable) { mutable_mapping_ = mapping_; } @@ -98,4 +100,8 @@ const uint8_t* FileMapping::GetMapping() const { return mapping_; } +bool FileMapping::IsValid() const { + return valid_; +} + } // namespace fml diff --git a/fml/platform/win/mapping_win.cc b/fml/platform/win/mapping_win.cc index 0b9427451e08e..1497ddc3e4259 100644 --- a/fml/platform/win/mapping_win.cc +++ b/fml/platform/win/mapping_win.cc @@ -54,6 +54,11 @@ FileMapping::FileMapping(const fml::UniqueFD& fd, return; } + if (mapping_size == 0) { + valid_ = true; + return; + } + DWORD protect_flags = 0; bool read_only = !IsWritable(protections); @@ -90,6 +95,7 @@ FileMapping::FileMapping(const fml::UniqueFD& fd, mapping_ = mapping; size_ = mapping_size; + valid_ = true; if (IsWritable(protections)) { mutable_mapping_ = mapping_; } @@ -109,4 +115,8 @@ const uint8_t* FileMapping::GetMapping() const { return mapping_; } +bool FileMapping::IsValid() const { + return valid_; +} + } // namespace fml