diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc index d0ba3d48f4e50f0dc11dd0a00bcea140baf12aee..7cc9ac39df3f835114871d9ce84d95bc21ac3498 100644 --- a/libziparchive/zip_archive.cc +++ b/libziparchive/zip_archive.cc @@ -594,6 +594,8 @@ static int32_t MapCentralDirectory(int fd, const char* debug_file_name, return result; } +static inline ssize_t ReadAtOffset(int fd, uint8_t* buf, size_t len, off64_t off); + /* * Parses the Zip archive's Central Directory. Allocates and populates the * hash table. @@ -672,8 +674,8 @@ static int32_t ParseZipArchive(ZipArchive* archive) { } uint32_t lfh_start_bytes; - if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&lfh_start_bytes), - sizeof(uint32_t), 0)) { + if (ReadAtOffset(archive->fd, reinterpret_cast<uint8_t*>(&lfh_start_bytes), + sizeof(uint32_t), 0) != sizeof(uint32_t)) { ALOGW("Zip: Unable to read header for entry at offset == 0."); return -1; } diff --git a/libziparchive/zip_archive_test.cc b/libziparchive/zip_archive_test.cc index 4088843f941e5987b9a23ba380380500bf1bb178..2ae49a22ba992c2969d65af924a6f539014dc9c4 100644 --- a/libziparchive/zip_archive_test.cc +++ b/libziparchive/zip_archive_test.cc @@ -266,7 +266,7 @@ TEST(ziparchive, ExtractToFile) { // Manual changes : // [2] = 0xff // Corrupt the LFH signature of entry 0. // [3] = 0xff // Corrupt the LFH signature of entry 0. -static const std::vector<uint8_t> kZipFileWithBrokenLfhSignature{ +static const uint8_t kZipFileWithBrokenLfhSignature[] = { //[lfh-sig-----------], [lfh contents--------------------------------- 0x50, 0x4b, 0xff, 0xff, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0x80, //-------------------------------------------------------------------- @@ -297,12 +297,16 @@ static const std::vector<uint8_t> kZipFileWithBrokenLfhSignature{ 0x00, 0x00, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00}; TEST(ziparchive, BrokenLfhSignature) { - TemporaryFile tmp_file; - ASSERT_NE(-1, tmp_file.fd); - ASSERT_TRUE(android::base::WriteFully(tmp_file.fd, &kZipFileWithBrokenLfhSignature[0], - kZipFileWithBrokenLfhSignature.size())); + char kTempFilePattern[] = "zip_archive_input_XXXXXX"; + int fd = make_temporary_file(kTempFilePattern); + ASSERT_NE(-1, fd); + + ASSERT_EQ(static_cast<int32_t>(sizeof(kZipFileWithBrokenLfhSignature)), + TEMP_FAILURE_RETRY(write(fd, kZipFileWithBrokenLfhSignature, + sizeof(kZipFileWithBrokenLfhSignature)))); ZipArchiveHandle handle; - ASSERT_EQ(-1, OpenArchiveFd(tmp_file.fd, "LeadingNonZipBytes", &handle)); + ASSERT_EQ(-1, OpenArchiveFd(fd, "LeadingNonZipBytes", &handle)); + close(fd); } int main(int argc, char** argv) {