Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 7711efb

Browse files
Return an empty mapping for an empty file asset (#10815)
Fixes flutter/flutter#36574
1 parent 663f9a9 commit 7711efb

File tree

6 files changed

+37
-4
lines changed

6 files changed

+37
-4
lines changed

assets/directory_asset_bundle.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ std::unique_ptr<fml::Mapping> DirectoryAssetBundle::GetAsMapping(
3838
auto mapping = std::make_unique<fml::FileMapping>(fml::OpenFile(
3939
descriptor_, asset_name.c_str(), false, fml::FilePermission::kRead));
4040

41-
if (mapping->GetMapping() == nullptr) {
41+
if (!mapping->IsValid()) {
4242
return nullptr;
4343
}
4444

fml/file_unittest.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,17 @@ TEST(FileTest, AtomicWriteTest) {
155155
// Cleanup.
156156
ASSERT_TRUE(fml::UnlinkFile(dir.fd(), "precious_data"));
157157
}
158+
159+
TEST(FileTest, EmptyMappingTest) {
160+
fml::ScopedTemporaryDirectory dir;
161+
162+
auto file = fml::OpenFile(dir.fd(), "my_contents", true,
163+
fml::FilePermission::kReadWrite);
164+
165+
fml::FileMapping mapping(file);
166+
ASSERT_TRUE(mapping.IsValid());
167+
ASSERT_EQ(mapping.GetSize(), 0ul);
168+
ASSERT_EQ(mapping.GetMapping(), nullptr);
169+
170+
ASSERT_TRUE(fml::UnlinkFile(dir.fd(), "my_contents"));
171+
}

fml/mapping.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
3131
auto mapping = std::make_unique<FileMapping>(
3232
base_fd, std::initializer_list<Protection>{Protection::kRead});
3333

34-
if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
34+
if (!mapping->IsValid()) {
3535
return nullptr;
3636
}
3737

@@ -56,7 +56,7 @@ std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
5656
base_fd, std::initializer_list<Protection>{Protection::kRead,
5757
Protection::kExecute});
5858

59-
if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
59+
if (!mapping->IsValid()) {
6060
return nullptr;
6161
}
6262

fml/mapping.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ class FileMapping final : public Mapping {
6767

6868
uint8_t* GetMutableMapping();
6969

70+
bool IsValid() const;
71+
7072
private:
73+
bool valid_ = false;
7174
size_t size_ = 0;
7275
uint8_t* mapping_ = nullptr;
7376
uint8_t* mutable_mapping_ = nullptr;

fml/platform/posix/mapping_posix.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ FileMapping::FileMapping(const fml::UniqueFD& handle,
6363
return;
6464
}
6565

66-
if (stat_buffer.st_size <= 0) {
66+
if (stat_buffer.st_size == 0) {
67+
valid_ = true;
6768
return;
6869
}
6970

@@ -79,6 +80,7 @@ FileMapping::FileMapping(const fml::UniqueFD& handle,
7980

8081
mapping_ = static_cast<uint8_t*>(mapping);
8182
size_ = stat_buffer.st_size;
83+
valid_ = true;
8284
if (is_writable) {
8385
mutable_mapping_ = mapping_;
8486
}
@@ -98,4 +100,8 @@ const uint8_t* FileMapping::GetMapping() const {
98100
return mapping_;
99101
}
100102

103+
bool FileMapping::IsValid() const {
104+
return valid_;
105+
}
106+
101107
} // namespace fml

fml/platform/win/mapping_win.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ FileMapping::FileMapping(const fml::UniqueFD& fd,
5454
return;
5555
}
5656

57+
if (mapping_size == 0) {
58+
valid_ = true;
59+
return;
60+
}
61+
5762
DWORD protect_flags = 0;
5863
bool read_only = !IsWritable(protections);
5964

@@ -90,6 +95,7 @@ FileMapping::FileMapping(const fml::UniqueFD& fd,
9095

9196
mapping_ = mapping;
9297
size_ = mapping_size;
98+
valid_ = true;
9399
if (IsWritable(protections)) {
94100
mutable_mapping_ = mapping_;
95101
}
@@ -109,4 +115,8 @@ const uint8_t* FileMapping::GetMapping() const {
109115
return mapping_;
110116
}
111117

118+
bool FileMapping::IsValid() const {
119+
return valid_;
120+
}
121+
112122
} // namespace fml

0 commit comments

Comments
 (0)