diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc index 5e5e7afd1..9536fc7ae 100644 --- a/libziparchive/zip_archive.cc +++ b/libziparchive/zip_archive.cc @@ -33,6 +33,10 @@ #include #include +#if defined(__BIONIC__) +#include +#endif + #include #include #include // TEMP_FAILURE_RETRY may or may not be in unistd @@ -165,6 +169,44 @@ static int32_t AddToHash(ZipString* hash_table, const uint64_t hash_table_size, return 0; } +ZipArchive::ZipArchive(const int fd, bool assume_ownership) + : mapped_zip(fd), + close_file(assume_ownership), + directory_offset(0), + central_directory(), + directory_map(new android::FileMap()), + num_entries(0), + hash_table_size(0), + hash_table(nullptr) { +#if defined(__BIONIC__) + if (assume_ownership) { + android_fdsan_exchange_owner_tag(fd, 0, reinterpret_cast(this)); + } +#endif +} + +ZipArchive::ZipArchive(void* address, size_t length) + : mapped_zip(address, length), + close_file(false), + directory_offset(0), + central_directory(), + directory_map(new android::FileMap()), + num_entries(0), + hash_table_size(0), + hash_table(nullptr) {} + +ZipArchive::~ZipArchive() { + if (close_file && mapped_zip.GetFileDescriptor() >= 0) { +#if defined(__BIONIC__) + android_fdsan_close_with_tag(mapped_zip.GetFileDescriptor(), reinterpret_cast(this)); +#else + close(mapped_zip.GetFileDescriptor()); +#endif + } + + free(hash_table); +} + static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* archive, off64_t file_length, off64_t read_amount, uint8_t* scan_buffer) { const off64_t search_start = file_length - read_amount; diff --git a/libziparchive/zip_archive_private.h b/libziparchive/zip_archive_private.h index 18e02291d..0a73300eb 100644 --- a/libziparchive/zip_archive_private.h +++ b/libziparchive/zip_archive_private.h @@ -156,33 +156,9 @@ struct ZipArchive { uint32_t hash_table_size; ZipString* hash_table; - ZipArchive(const int fd, bool assume_ownership) - : mapped_zip(fd), - close_file(assume_ownership), - directory_offset(0), - central_directory(), - directory_map(new android::FileMap()), - num_entries(0), - hash_table_size(0), - hash_table(nullptr) {} - - ZipArchive(void* address, size_t length) - : mapped_zip(address, length), - close_file(false), - directory_offset(0), - central_directory(), - directory_map(new android::FileMap()), - num_entries(0), - hash_table_size(0), - hash_table(nullptr) {} - - ~ZipArchive() { - if (close_file && mapped_zip.GetFileDescriptor() >= 0) { - close(mapped_zip.GetFileDescriptor()); - } - - free(hash_table); - } + ZipArchive(const int fd, bool assume_ownership); + ZipArchive(void* address, size_t length); + ~ZipArchive(); bool InitializeCentralDirectory(const char* debug_file_name, off64_t cd_start_offset, size_t cd_size);