Skip to content

Commit

Permalink
Merge pull request iovisor#4544 from michalgr/fix_short_zip_parsing
Browse files Browse the repository at this point in the history
bcc_zip_archive_open should not loop indefinitely for bad archives
  • Loading branch information
davemarchevsky committed Mar 31, 2023
2 parents c78ec5c + 7271bfc commit 8856930
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/cc/bcc_zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ static int find_central_directory(struct bcc_zip_archive* archive) {
// Because the end of central directory ends with a variable length array of
// up to 0xFFFF bytes we can't know exactly where it starts and need to
// search for it at the end of the file, scanning the (limit, offset] range.
uint32_t offset =
archive->size - sizeof(struct end_of_central_directory_record);
int64_t limit = (int64_t)offset - (1 << 16);
int64_t offset =
(int64_t)archive->size - sizeof(struct end_of_central_directory_record);
int64_t limit = offset - (1 << 16);
for (; offset >= 0 && offset > limit && rc == -1; offset--) {
rc = try_parse_end_of_central_directory(archive, offset);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/cc/test_zip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#define LIB_ENTRY_NAME "libdebuginfo_test_lib.so"
#define ENTRY_IN_SUBDIR_NAME "zip_subdir/file.txt"
#define NOT_AN_ARCHIVE_PATH CMAKE_CURRENT_BINARY_DIR "/dummy_proc_map.txt"
#define TEST_ARCHIVE_PATH CMAKE_CURRENT_BINARY_DIR "/archive.zip"

namespace {
Expand All @@ -47,6 +48,11 @@ const void* get_uncompressed_data(const bcc_zip_entry& entry) {

} // namespace

TEST_CASE("returns error for non-zip files", "[zip]") {
bcc_zip_archive* archive = bcc_zip_archive_open(NOT_AN_ARCHIVE_PATH);
REQUIRE(archive == nullptr);
}

TEST_CASE("finds entries in a zip archive by name", "[zip]") {
bcc_zip_archive* archive = bcc_zip_archive_open(TEST_ARCHIVE_PATH);
REQUIRE(archive != nullptr);
Expand Down

0 comments on commit 8856930

Please sign in to comment.