8000 Can't open zip with 65535 entries · Issue #596 · zlib-ng/minizip-ng · GitHub
[go: up one dir, main page]

Skip to content

Can't open zip with 65535 entries #596

Closed

Description

@Nekto89

fast "dirty" code for reproducing the issue. I'm not sure that I've used API properly. I'm creating zip files with 65534, 65536 and 65535 empty files inside. 65535 doesn't work for some reason. 7-zip can open result zip without any errors

#include <mz.h>
#include <mz_os.h>
#include <mz_strm.h>
#include <mz_strm_os.h>
#include <mz_zip.h>
#include <mz_zip_rw.h>

#include <cstdio>
#include <ctime>
#include <string>

int write_archive(const char* archive_filename, const std::size_t number_of_files) {
  void* zip_writer = nullptr;
  const char* path = archive_filename;
  mz_zip_writer_create(&zip_writer);
  if (mz_zip_writer_open_file(zip_writer, path, 0, 0) != MZ_OK) {
    mz_zip_writer_delete(&zip_writer);
    std::printf("Couldn't open zip for writing");
    return -1;
  }

  for (std::size_t i = 0; i < number_of_files; ++i) {
    const auto filename = std::to_string(i);

    mz_zip_file file_info = { 0 };
    file_info.filename = filename.c_str();
    file_info.modified_date = std::time(nullptr);
    file_info.version_madeby = MZ_VERSION_MADEBY;
    file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
    file_info.flag = MZ_ZIP_FLAG_UTF8;

    if (mz_zip_writer_entry_open(zip_writer, &file_info) != MZ_OK) {
      std::printf("Couldn't open entry");
      return -2;
    }
    if (mz_zip_writer_entry_close(zip_writer) != MZ_OK) {
      std::printf("Couldn't close entry");
      return -3;
    }
  }

  if (mz_zip_writer_close(zip_writer) != MZ_OK) {
    std::printf("Couldn't close writer");
    return -4;
  }
  mz_zip_writer_delete(&zip_writer);
  return 0;
}

bool can_archive_be_opened(const char* path) {
  void* zip_reader = nullptr;
  void* file_stream = nullptr;

  mz_zip_reader_create(&zip_reader);
  mz_stream_os_create(&file_stream);

  auto err = mz_stream_os_open(file_stream, path, MZ_OPEN_MODE_READ);
  if (err == MZ_OK) {
    err = mz_zip_reader_open(zip_reader, file_stream);
    if (err == MZ_OK) {
      mz_zip_reader_close(zip_reader);
    }
  }
  mz_stream_os_delete(&file_stream);
  mz_zip_reader_delete(&zip_reader);
  return err == MZ_OK;
}

int main() {
  for (std::size_t i : {65534, 65536, 65535})
  {
    const auto filename = "test" + std::to_string(i) + ".zip";
    const auto write_result = write_archive(filename.c_str(), i);
    if (write_result != 0)
      return write_result;
    const auto read_result = can_archive_be_opened(filename.c_str());
    if (!read_result) {
      std::printf("Can't open %s", filename.c_str());
      return -5;
    }
  }
}

Activity

  1. nmoinvaz commented on Nov 13, 2021

    @nmoinvaz
    Member

    if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX) {

    Should probably be:

    if (zip->cd_offset >= UINT32_MAX || zip->number_entry >= UINT16_MAX) {
    
  2. Nekto89 commented on Nov 14, 2021

    @Nekto89
    Author

    Isn't 65535 still valid number of files for ZIP? I thought that the problem is in reading.

  3. nmoinvaz commented on Nov 14, 2021

    @nmoinvaz
    Member

    If the value is 64k then it will read the value in the zip64 header instead.

  4. added a commit that references this issue on Dec 21, 2021
  5. added a commit that references this issue on Aug 31, 2023
  6. added a commit that references this issue on Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0