zip icon indicating copy to clipboard operation
zip copied to clipboard

strncpy filename_size

Open kuba-- opened this issue 3 years ago • 6 comments

Fixes https://github.com/kuba--/zip/issues/272

kuba-- avatar Aug 02 '22 20:08 kuba--

This won't solve the problem since the maximum path length is 260 symbols. MAX_PATH restrictions apply until Windows 10 version 1607

as a solution I can suggest this code:

/* Note: MAX_PATH restrictions apply until Windows 10 version 1607
  */
#if defined(_WIN32)
#   define MAX_FILE_PATH 256
#else
#   define MAX_FILE_PATH 512
#endif

enum {
  /* Note: These enums can be reduced as needed to save memory or stack space -
     they are pretty conservative. */
  MZ_ZIP_MAX_IO_BUF_SIZE = 8 * 1024,
  MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = MAX_FILE_PATH,
  MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 512
};

But this is totally not related to the issue which you came across. MAX_PATH on windows < 10 is a one thing, but you got an error related to strncpy which potentially can copy more than array size.

kuba-- avatar Aug 03 '22 21:08 kuba--

...Moreover lets take a look step by step:

we need a minimum of filename_size (which by default is MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE (512)) and MAX_PATH - dirlen.

  if (filename_size > MAX_PATH - dirlen) {
    filename_size = MAX_PATH - dirlen;
  }

MAX_PATH is defined in zip.h as follows:

#ifndef MAX_PATH
#define MAX_PATH 1024 /* # chars in a path name including NULL */
#endif

So if you are on windows where MAX_PATH is defined as 256, then it should work, because dirlen cannot be > MAX_PATH (because function returns ZIP_EINVENTNAME if (dirlen + 1 > MAX_PATH) )

kuba-- avatar Aug 03 '22 21:08 kuba--

MAX_PATH is defined in zip.h as follows:

#ifndef MAX_PATH
#define MAX_PATH 1024 /* # chars in a path name including NULL */
#endif

MAX_PATH is declared inside minwindef.h. I think that's what causes the problem.

Limaaron avatar Aug 03 '22 22:08 Limaaron

MAX_PATH is defined in zip.h as follows:

#ifndef MAX_PATH
#define MAX_PATH 1024 /* # chars in a path name including NULL */
#endif

MAX_PATH is declared inside minwindef.h. I think that's what causes the problem.

What problem exactly? In this case (https://github.com/tpn/winsdk-10/blob/master/Include/10.0.16299.0/shared/minwindef.h#L60), just include zip.h after windows' headers.

kuba-- avatar Aug 03 '22 22:08 kuba--

this did not solve the problem, I tried to build it as an independent library and the ide tells me that MAX_PATH is defined in minwindef.h.

Limaaron avatar Aug 03 '22 23:08 Limaaron

it seems strange to me that when compiling without the -DCMAKE_BUILD_TYPE=Release, it does not give such an error.

Limaaron avatar Aug 03 '22 23:08 Limaaron