diff options
author | Neil Fuller <nfuller@google.com> | 2014-07-25 14:43:04 +0100 |
---|---|---|
committer | Neil Fuller <nfuller@google.com> | 2014-07-25 16:06:20 +0100 |
commit | b1a113f618561b274ab793e6401416d449c60449 (patch) | |
tree | dbc12d36b86e2bc5bf1bd57e726e1ba90dc6f026 /libziparchive/zip_archive.cc | |
parent | 73290cd1369422260f1843dc36949c27422c0300 (diff) | |
download | system_core-b1a113f618561b274ab793e6401416d449c60449.tar.gz system_core-b1a113f618561b274ab793e6401416d449c60449.tar.bz2 system_core-b1a113f618561b274ab793e6401416d449c60449.zip |
Prevent the accidental closure of fd[0] for missing zip files.
Bug: 16530747
Change-Id: I3593f2bc4d56a2f91252ea795c90ce3c78e1ec06
Diffstat (limited to 'libziparchive/zip_archive.cc')
-rw-r--r-- | libziparchive/zip_archive.cc | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc index 128bad440..6ec8f0d34 100644 --- a/libziparchive/zip_archive.cc +++ b/libziparchive/zip_archive.cc @@ -287,7 +287,7 @@ static const char kTempMappingFileName[] = "zip: ExtractFileToFile"; */ struct ZipArchive { /* open Zip archive */ - int fd; + const int fd; /* mapped central directory area */ off64_t directory_offset; @@ -304,6 +304,25 @@ struct ZipArchive { */ uint32_t hash_table_size; ZipEntryName* hash_table; + + ZipArchive(const int fd) : + fd(fd), + directory_offset(0), + directory_map(NULL), + num_entries(0), + hash_table_size(0), + hash_table(NULL) {} + + ~ZipArchive() { + if (fd >= 0) { + close(fd); + } + + if (directory_map != NULL) { + directory_map->release(); + } + free(hash_table); + } }; // Returns 0 on success and negative values on failure. @@ -661,28 +680,20 @@ static int32_t OpenArchiveInternal(ZipArchive* archive, int32_t OpenArchiveFd(int fd, const char* debug_file_name, ZipArchiveHandle* handle) { - ZipArchive* archive = (ZipArchive*) malloc(sizeof(ZipArchive)); - memset(archive, 0, sizeof(*archive)); + ZipArchive* archive = new ZipArchive(fd); *handle = archive; - - archive->fd = fd; - return OpenArchiveInternal(archive, debug_file_name); } int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) { - ZipArchive* archive = (ZipArchive*) malloc(sizeof(ZipArchive)); - memset(archive, 0, sizeof(*archive)); + const int fd = open(fileName, O_RDONLY | O_BINARY, 0); + ZipArchive* archive = new ZipArchive(fd); *handle = archive; - const int fd = open(fileName, O_RDONLY | O_BINARY, 0); if (fd < 0) { ALOGW("Unable to open '%s': %s", fileName, strerror(errno)); return kIoError; - } else { - archive->fd = fd; } - return OpenArchiveInternal(archive, fileName); } @@ -692,16 +703,7 @@ int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) { void CloseArchive(ZipArchiveHandle handle) { ZipArchive* archive = (ZipArchive*) handle; ALOGV("Closing archive %p", archive); - - if (archive->fd >= 0) { - close(archive->fd); - } - - if (archive->directory_map != NULL) { - archive->directory_map->release(); - } - free(archive->hash_table); - free(archive); + delete archive; } static int32_t UpdateEntryFromDataDescriptor(int fd, |