Skip to content

Avoid Win32 file source I/O length truncation#541

Open
Alb3e3 wants to merge 1 commit into
nih-at:mainfrom
Alb3e3:harden-win32-io-lengths
Open

Avoid Win32 file source I/O length truncation#541
Alb3e3 wants to merge 1 commit into
nih-at:mainfrom
Alb3e3:harden-win32-io-lengths

Conversation

@Alb3e3

@Alb3e3 Alb3e3 commented Jun 15, 2026

Copy link
Copy Markdown

Summary

  • cap Win32 file-source reads to the maximum DWORD byte count accepted by ReadFile
  • write Win32 file-source output in DWORD-sized chunks instead of casting the full zip_uint64_t length
  • preserve the existing write-all-or-fail behavior while avoiding large-length wraparound/truncation

Rationale

The libzip source API uses zip_uint64_t byte counts, but the Win32 ReadFile and WriteFile APIs accept DWORD byte counts. Passing a larger value through a direct cast can wrap or truncate the transfer size before it reaches the OS.

For reads, returning a bounded chunk lets zip_source_read() continue its existing loop. For writes, the Win32 backend now loops over bounded chunks so large writes either complete the requested length or fail with the underlying write error.

Testing

  • git diff --check

Not run locally: full CMake/tests or Windows compile, because this container does not have cmake, clang-format, cppcheck, or a MinGW/Windows compiler installed.

The Win32 file source passed zip_uint64_t lengths directly to ReadFile and WriteFile, whose byte-count arguments are DWORD values. Very large source operations could therefore wrap or truncate before reaching the OS.

Clamp read requests to the largest DWORD-sized transfer so zip_source_read can continue in chunks, and write large buffers in DWORD-sized chunks so the write path either completes the requested length or reports a real write error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant