diff options
| author | Elliott Hughes <enh@google.com> | 2019-02-06 14:28:32 -0800 |
|---|---|---|
| committer | Elliott Hughes <enh@google.com> | 2019-02-06 14:29:35 -0800 |
| commit | eb0ef145fd38922617e6ab3d94eaacde93e04c95 (patch) | |
| tree | 63a400421b9bc3526032421fce23b8434c84a8d4 /base | |
| parent | a42af36002914dc2efe9589a334c72eb5225d1c5 (diff) | |
| download | system_core-eb0ef145fd38922617e6ab3d94eaacde93e04c95.tar.gz system_core-eb0ef145fd38922617e6ab3d94eaacde93e04c95.tar.bz2 system_core-eb0ef145fd38922617e6ab3d94eaacde93e04c95.zip | |
MappedFile and FileMap should support zero-length mappings.
Bug: http://b/119818070 "app crashes when reading asset of zero length"
Test: ran tests
Change-Id: Idd2ad6f6e72c8e445aff78a460fac96dea41c950
Diffstat (limited to 'base')
| -rw-r--r-- | base/mapped_file.cpp | 11 | ||||
| -rw-r--r-- | base/mapped_file_test.cpp | 11 |
2 files changed, 20 insertions, 2 deletions
diff --git a/base/mapped_file.cpp b/base/mapped_file.cpp index f7901afa4..faa845d14 100644 --- a/base/mapped_file.cpp +++ b/base/mapped_file.cpp @@ -16,6 +16,8 @@ #include "android-base/mapped_file.h" +#include <errno.h> + namespace android { namespace base { @@ -50,7 +52,14 @@ std::unique_ptr<MappedFile> MappedFile::FromFd(int fd, off64_t offset, size_t le new MappedFile{static_cast<char*>(base), length, slop, handle}); #else void* base = mmap(nullptr, file_length, prot, MAP_SHARED, fd, file_offset); - if (base == MAP_FAILED) return nullptr; + if (base == MAP_FAILED) { + // http://b/119818070 "app crashes when reading asset of zero length". + // mmap fails with EINVAL for a zero length region. + if (errno == EINVAL && length == 0) { + return std::unique_ptr<MappedFile>(new MappedFile{nullptr, 0, 0}); + } + return nullptr; + } return std::unique_ptr<MappedFile>(new MappedFile{static_cast<char*>(base), length, slop}); #endif } diff --git a/base/mapped_file_test.cpp b/base/mapped_file_test.cpp index 7e89723c6..cfde73c74 100644 --- a/base/mapped_file_test.cpp +++ b/base/mapped_file_test.cpp @@ -25,7 +25,6 @@ #include <string> #include "android-base/file.h" -#include "android-base/unique_fd.h" TEST(mapped_file, smoke) { TemporaryFile tf; @@ -37,3 +36,13 @@ TEST(mapped_file, smoke) { ASSERT_EQ('l', m->data()[0]); ASSERT_EQ('o', m->data()[1]); } + +TEST(mapped_file, zero_length_mapping) { + // http://b/119818070 "app crashes when reading asset of zero length". + // mmap fails with EINVAL for a zero length region. + TemporaryFile tf; + ASSERT_TRUE(tf.fd != -1); + + auto m = android::base::MappedFile::FromFd(tf.fd, 4096, 0, PROT_READ); + ASSERT_EQ(0u, m->size()); +} |
