Skip to content

Commit 4aaab80

Browse files
RummskartoffelIdanHo
authored andcommitted
unzip: Don't fail from mmap when trying to decompress empty files
Given an empty file, unzip would try to create a zero-size memory mapping of that file, which would fail with EINVAL. With this commit, attempting to unzip an empty file of course still fails, since that's not a valid PKZIP file, but it now fails for the correct reason and with the correct error message.
1 parent 1a2f715 commit 4aaab80

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Userland/Utilities/unzip.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,17 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
100100
return 1;
101101
}
102102

103-
auto mapped_file = TRY(Core::MappedFile::map(zip_file_path));
103+
RefPtr<Core::MappedFile> mapped_file;
104+
ReadonlyBytes input_bytes;
105+
if (st.st_size > 0) {
106+
mapped_file = TRY(Core::MappedFile::map(zip_file_path));
107+
input_bytes = mapped_file->bytes();
108+
}
104109

105110
if (!quiet)
106111
warnln("Archive: {}", zip_file_path);
107112

108-
auto zip_file = Archive::Zip::try_create(mapped_file->bytes());
113+
auto zip_file = Archive::Zip::try_create(input_bytes);
109114
if (!zip_file.has_value()) {
110115
warnln("Invalid zip file {}", zip_file_path);
111116
return 1;

0 commit comments

Comments
 (0)